adding cs142 code

This commit is contained in:
dm
2016-04-06 20:45:34 -07:00
parent 552d456d53
commit 8e52ce1982
174 changed files with 14064 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
CXXFLAGS= -Wall -g
OBJECTS=main.o employee.o hourly.o salaried.o administrator.o faculty.o util.o campus.o
EXE=main
all: $(EXE)
$(EXE): main.o
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
main.o: main.cpp employee.o hourly.o salaried.o administrator.o faculty.o util.o campus.o
employee.o: employee.h employee.cpp
hourly.o: hourly.cpp hourly.h employee.h
salaried.o: salaried.h salaried.cpp employee.h
administrator.o: administrator.cpp administrator.h employee.h
faculty.o: faculty.cpp faculty.h employee.h
util.o: util.h util.cpp
campus.o: campus.cpp campus.h
run: $(EXE)
@./$(EXE)
clean:
@rm -vf *.o
@rm -vf $(EXE)
debug: $(EXE)
gdb ./$(EXE)
valgrind: $(EXE)
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
+14
View File
@@ -0,0 +1,14 @@
#include "administrator.h"
#include "employee.h"
using namespace std;
administrator::administrator(string name, double pay_rate, int hours, string type) :
employee(name, pay_rate, hours, type) {}
int administrator::get_salary() {
if(hours > 170) {
pay_rate += pay_rate * .1;
}
return pay_rate;
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef __ADMINISTRATOR_H__
#define __ADMINISTRATOR_H__
#include <iostream>
#include "employee.h"
using namespace std;
class administrator : public employee {
public:
administrator(string name, double pay_rate, int hours, string type);
int get_salary();
};
#endif
+263
View File
@@ -0,0 +1,263 @@
#include "campus.h"
#include "salaried.h"
#include "hourly.h"
#include "faculty.h"
#include "administrator.h"
#include "util.h"
#include <iostream>
#include <vector>
#include <cstdlib>
#include <fstream>
bool campus::contains(string name) { //function used to verify if the string is already included
employee * f = NULL;
for(unsigned int i = 0; i < workers.size(); i++) {
f = dynamic_cast<employee *>(workers[i]);
if(f->get_name() == name) {
return true;
}
}
return false;
}
void campus::add_worker_from_parse(string name, string type, double pay_rate) { //add worker for parsed input
bool passed = false;
employee * h = NULL;
int hours = 0;
if(type == "H") {
h = new hourly(name, pay_rate, hours, type);
passed = true;
}
else if(type == "S") {
h = new salaried(name, pay_rate, hours, type);
passed = true;
}
else if(type == "F") {
h = new faculty(name, pay_rate, hours, type);
passed = true;
}
else if(type == "A") {
h = new administrator(name, pay_rate, hours, type);
passed = true;
}
if(passed == true) {
workers.push_back(h);
}
}
void campus::add_worker() { //general add worker function which it gets the input
employee * h = NULL;
bool passed;
string info;
string name, type;
int hours = 0;
double pay_rate = 0;
cout << "Enter info in following format" << endl;
cout << "type pay_rate name" << endl;;
getline(cin, info);
passed = parse_n_load(info, name, type, pay_rate);
if(contains(name) == true) {
cout << "already exists" << endl;
return;
}
if(passed == false) {
cout << "incorrect entry" << endl;
}
if(info[0] == ' ') {
cout << "incorrect entry" << endl;
passed = false;
}
else if(type == "H") {
h = new hourly(name, pay_rate, hours, type);
}
else if(type == "S") {
h = new salaried(name, pay_rate, hours, type);
}
else if(type == "F") {
h = new faculty(name, pay_rate, hours, type);
}
else if(type == "A") {
h = new administrator(name, pay_rate, hours, type);
}
if(passed == true) {
workers.push_back(h);
}
}
void campus::delete_worker() {
string name;
employee * h = NULL;
cout << "Enter name of worker to delete" << endl;
getline(cin, name);
for(unsigned int i = 0; i < workers.size(); i++) {
h = dynamic_cast<employee *>(workers[i]);
if(h->get_name() == name) {
delete h;
workers.erase(workers.begin() + i);
}
}
}
void campus::display_worker() { //allows for display of all employees or selected
string input;
employee * h = NULL;
cout << "(a)ll workers or (o)ne worker?" << endl;
cin >> input;
cin.ignore();
cout << "type // payrate // name // hours // salary" << endl;
if(input == "a") {
for(unsigned int i = 0; i < workers.size(); i++){
h = dynamic_cast<employee *>(workers[i]);
cout << *workers[i] << " $" << h->get_salary() << endl;
}
}
if(input == "o") {
cout << "name: ";
employee * h = NULL;
getline(cin, input);
for(unsigned int i = 0; i < workers.size(); i++) {
h = dynamic_cast<employee *>(workers[i]);
if(h->get_name() == input) {
cout << *h << " $" << h->get_salary() << endl;
}
}
}
}
void campus::sort_name() {
bool ready = true;
employee * h = NULL;
employee * j = NULL;
while(ready) {
for(unsigned int i = 0; i < workers.size() -1; i++) {
h = dynamic_cast<employee *>(workers[i]);
j = dynamic_cast<employee *>(workers[i+1]);
ready = false;
if(h->get_name() > j->get_name()) {
workers.push_back(workers[i]);
workers[i] = workers[i+1];
workers[i+1] = workers[workers.size()-1];
workers.pop_back();
ready = true;
}
}
}
}
void campus::sort_pay() {
bool ready = true;
employee * h = NULL;
employee * j = NULL;
while(ready) {
for(unsigned int i = 0; i < workers.size() -1; i++) {
h = dynamic_cast<employee *>(workers[i]);
j = dynamic_cast<employee *>(workers[i+1]);
ready = false;
if(h->get_payrate() > j->get_payrate()) {
workers.push_back(workers[i]);
workers[i] = workers[i+1];
workers[i+1] = workers[workers.size()-1];
workers.pop_back();
ready = true;
}
}
}
}
void campus::sort_worker() { //allows for either sort by name or by pay
string input;
cout << "sort by (n)ame or by (p)ay?" << endl;
cin >> input;
if(input == "n") {
sort_name();
}
if(input == "p") {
sort_pay();
}
}
void campus::advance_month() {
employee * h = NULL;
for(unsigned int i = 0; i < workers.size(); i++) {
h = dynamic_cast<employee *>(workers[i]);
h->reset_hours();
}
}
void campus::set_hours() {
string name;
employee * h = NULL;
cout << "enter name of employee" << endl;
getline(cin, name);
for(unsigned int i = 0; i < workers.size(); i++) {
h = dynamic_cast<employee *>(workers[i]);
if(h->get_name() == name) {
int input;
cout << "enter hours" << endl;
cin >> input;
cin.ignore();
h->set_hours(input);
}
}
}
void campus::add_files(campus c) { //function for i/o. can either write to a file or take a file in
string add_or_write;
cout << "(a)dd or (w)rite to file?" << endl;
cin >> add_or_write;
cin.ignore();
if(add_or_write == "a") {
bool file_correct = false;
string input, input_file_name;
while(!file_correct) {
cout << "please enter file name: ";
getline(cin, input);
ifstream inputs(input.c_str());
if(inputs.good()) {
input_file_name = input;
file_correct = true;
string lineread;
while(getline(inputs, lineread)) {
bool worked;
string name, type;
double pay_rate;
worked = parse_n_load(lineread, name, type, pay_rate);
if(worked == true) {
add_worker_from_parse(name, type, pay_rate);
}
lineread = "";
}
}
else {
cerr << "incorrect file name" << endl;
}
}
}
if(add_or_write == "w") {
bool file_correct = false;
string input, input_file_name;
string output_file_name;
file_correct = false;
while(!file_correct) {
cout << "please enter file name: ";
getline(cin, output_file_name);
ofstream out(output_file_name.c_str());
if(out.good()) {
out << c;
file_correct = true;
}
else {
cerr << "incorrect file name" << endl;
}
}
}
}
ostream & operator<<(ostream & os, campus & t) { //makes writing out to files much easier
employee * h = NULL;
for(unsigned int i = 0; i < t.workers.size(); i++) {
h = dynamic_cast<employee *>(t.workers[i]);
os << h->get_type() << " " << h->get_payrate() << " " << h->get_name() << "\n";
}
return os;
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef __CAMPUS_H__
#define __CAMPUS_H__
#include "employee.h"
#include <vector>
using namespace std;
class campus {
private:
vector<employee*> workers;
public:
void add_worker();
void add_worker_from_parse(string name, string type, double pay_rate);
void delete_worker();
void display_worker();
void sort_worker();
void advance_month();
void set_hours();
void add_files(campus c);
void sort_name();
void sort_pay();
bool contains(string name);
friend ostream & operator<<(ostream & os, campus & t);
};
#endif
+35
View File
@@ -0,0 +1,35 @@
#include "employee.h"
#include <iostream>
using namespace std;
employee::employee(string name, double pay_rate, int hours, string type) :
name(name), pay_rate(pay_rate), hours(hours), type(type) {}
string employee::get_name(){
return name;
}
double employee::get_payrate() {
return pay_rate;
}
int employee::get_hours() {
return hours;
}
string employee::get_type() {
return type;
}
void employee::reset_hours() {
hours = 0;
}
void employee::set_hours(int input) {
hours += input;
}
ostream & operator<<(ostream & os, employee & t){ //makes printing out employee easy
os << t.type << " " << t.pay_rate << " " << t.name << " " << t.hours;
return os;
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef __EMPLOYEE_H__
#define __EMPLOYEE_H__
#include <iostream>
using namespace std;
class employee {
private:
string name;
protected:
string type;
double pay_rate;
int hours;
public:
employee(string name, double pay_rate, int hours, string type);
string get_name();
double get_payrate();
string get_type();
int get_hours();
void set_hours(int input);
void reset_hours();
virtual int get_salary() = 0;
friend ostream & operator<<(ostream & os, employee & t);
};
#endif
+32
View File
@@ -0,0 +1,32 @@
#include "faculty.h"
#include "employee.h"
#include <cstdlib>
#include <iostream>
using namespace std;
faculty::faculty(string name, double pay_rate, int hours, string type) : employee(name, pay_rate, hours, type),
grad_students(set_grad_students()) {}
int faculty::get_salary() {
double pay_rate_return = 0;
if(grad_students > 0){
double percent;
pay_rate_return = pay_rate;
for(int i = 0; i < grad_students; i++){
percent += 0.05;
}
pay_rate_return += pay_rate*percent;
return pay_rate_return;
}
return pay_rate;
}
int faculty::get_grad_students() {
return grad_students;
}
int faculty::set_grad_students() { //gets random number of grad students for faculty
srand(time(NULL));
int students = rand() % 5;
return students;
}
+19
View File
@@ -0,0 +1,19 @@
#ifndef __FACULTY_H__
#define __FACULTY_H__
#include <iostream>
#include "employee.h"
using namespace std;
class faculty : public employee {
private:
int grad_students;
public:
faculty(string name, double pay_rate, int hours, string type);
int get_grad_students();
int set_grad_students();
int get_salary();
};
#endif
+19
View File
@@ -0,0 +1,19 @@
#include "hourly.h"
#include "employee.h"
using namespace std;
hourly::hourly(string name, double pay_rate, int hours, string type) : employee(name, pay_rate, hours, type) {}
int hourly::get_salary() {
int salary;
int overtime;
if((hours - 170) > 0) {
overtime = (hours - 170);
salary = 170 * pay_rate;
salary += overtime * (pay_rate * 1.5);
return salary;
}
salary = hours * pay_rate;
return salary;
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef __HOURLY_H__
#define __HOURLY_H__
#include <iostream>
#include "employee.h"
using namespace std;
class hourly : public employee {
public:
hourly(string name, double pay_rate, int hours, string type);
int get_salary();
};
#endif
+48
View File
@@ -0,0 +1,48 @@
#include <iostream>
#include <fstream>
#include "employee.h"
#include "salaried.h"
#include "hourly.h"
#include "faculty.h"
#include "administrator.h"
#include "util.h"
#include "campus.h"
using namespace std;
int main() { //driver for program
campus c = campus();
bool keep_going = true;
while(keep_going) {
string input;
cout << "(a)dd worker, (r)emove worker, (d)isplay worker, add (h)ours, \n";
cout << " (s)ort workers, advance (m)onth, add (f)ile, (q)uit" << endl;
cin >> input;
cin.ignore();
if(input == "a") {
c.add_worker();
}
if(input == "r") {
c.delete_worker();
}
if(input == "h") {
c.set_hours();
}
if(input == "d") {
c.display_worker();
}
if(input == "s") {
c.sort_worker();
}
if(input == "m") {
c.advance_month();
}
if(input == "f") {
c.add_files(c); //will either take in a file or write out to one
}
if(input == "q") {
keep_going = false;
}
}
}
+13
View File
@@ -0,0 +1,13 @@
#include "salaried.h"
#include "employee.h"
using namespace std;
salaried::salaried(string name, double pay_rate, int hours, string type) : employee(name, pay_rate, hours, type) {}
int salaried::get_salary() {
if(hours > 170/2) {
return pay_rate;
}
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef __SALARIED_H__
#define __SALARIED_H__
#include <iostream>
#include "employee.h"
using namespace std;
class salaried : public employee {
public:
salaried(string name, double pay_rate, int hours, string type);
int get_salary();
};
#endif
+46
View File
@@ -0,0 +1,46 @@
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
!_TAG_PROGRAM_NAME Exuberant Ctags //
!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
!_TAG_PROGRAM_VERSION 5.9~svn20110310 //
CXXFLAGS Makefile /^CXXFLAGS= -Wall -g$/;" m
EXE Makefile /^EXE=main$/;" m
OBJECTS Makefile /^OBJECTS=main.o employee.o hourly.o salaried.o administrator.o faculty.o util.o$/;" m
__ADMINISTRATOR_H__ administrator.h 2;" d
__EMPLOYEE_H__ employee.h 2;" d
__FACULTY_H__ faculty.h 2;" d
__HOURLY_H__ hourly.h 2;" d
__SALARIED_H__ salaried.h 2;" d
__UTIL_H__ util.h 2;" d
administrator administrator.cpp /^administrator::administrator(string name, double pay_rate, int hours) : employee(name, pay_rate, hours) {}$/;" f class:administrator
administrator administrator.h /^class administrator : public employee {$/;" c
employee employee.cpp /^employee::employee(string name, double pay_rate, int hours) :$/;" f class:employee
employee employee.h /^class employee {$/;" c
faculty faculty.cpp /^faculty::faculty(string name, double pay_rate, int hours) : employee(name, pay_rate, hours),$/;" f class:faculty
faculty faculty.h /^class faculty : public employee {$/;" c
get_grad_students faculty.cpp /^int faculty::get_grad_students() {$/;" f class:faculty
get_hours employee.cpp /^int employee::get_hours() {$/;" f class:employee
get_name employee.cpp /^string employee::get_name(){$/;" f class:employee
get_payrate employee.cpp /^double employee::get_payrate() {$/;" f class:employee
get_salary administrator.cpp /^int administrator::get_salary() {$/;" f class:administrator
get_salary faculty.cpp /^int faculty::get_salary() {$/;" f class:faculty
get_salary hourly.cpp /^int hourly::get_salary() {$/;" f class:hourly
get_salary salaried.cpp /^int salaried::get_salary() {$/;" f class:salaried
grad_students faculty.h /^ int grad_students;$/;" m class:faculty
hourly hourly.cpp /^hourly::hourly(string name, double pay_rate, int hours) : employee(name, pay_rate, hours) {}$/;" f class:hourly
hourly hourly.h /^class hourly : public employee {$/;" c
hours employee.h /^ int hours;$/;" m class:employee
main main.cpp /^int main() {$/;" f
name employee.h /^ string name;$/;" m class:employee
parse_n_load util.cpp /^bool parse_n_load(const string & input, string & name, string & type,$/;" f
pay_rate employee.h /^ double pay_rate;$/;" m class:employee
salaried salaried.cpp /^salaried::salaried(string name, double pay_rate, int hours) : employee(name, pay_rate, hours) {}$/;" f class:salaried
salaried salaried.h /^class salaried : public employee {$/;" c
set_grad_students faculty.cpp /^int faculty::set_grad_students() {$/;" f class:faculty
test_admin main.cpp /^void test_admin() {$/;" f
test_faculty main.cpp /^void test_faculty() {$/;" f
test_hourly main.cpp /^void test_hourly() {$/;" f
test_salaried main.cpp /^void test_salaried() {$/;" f
test_util main.cpp /^void test_util() {$/;" f
tokenize util.cpp /^vector<string> tokenize(const string & str, const string & delimiters) {$/;" f
+4
View File
@@ -0,0 +1,4 @@
A 10.5 zombie
alka alkjsa
F 152 stephen is awesome
H 15.243 cremeballs. creame
+46
View File
@@ -0,0 +1,46 @@
#include <vector>
#include <string>
using namespace std;
#include <cstdlib>
#include "util.h"
#include <iostream>
vector<string> tokenize(const string & str, const string & delimiters) {
//tokenizes a string by the delimiter passed in
vector<string> tokens;
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos or string::npos != lastPos)
{
tokens.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
return tokens;
}
bool parse_n_load(const string & input, string & name, string & type,
double & pay_rate) {
vector<string> tokens = tokenize(input);
if(tokens.size() == 0) {
return false;
}
type = tokens[0];
if (not ( type == "A"
or type == "F"
or type == "H"
or type == "S")) {
return false;
}
pay_rate = atof(tokens[1].c_str());
if(pay_rate == 0) {
return false;
}
for(unsigned int i = 2; i < tokens.size(); i++) {
name += tokens[i];
if(i < tokens.size() - 1) {
name += " ";
}
}
return true;
}
+14
View File
@@ -0,0 +1,14 @@
#ifndef __UTIL_H__
#define __UTIL_H__
#include <string>
#include <vector>
using namespace std;
void print(const string & in);
vector<string> tokenize(const string & str, const string & delimiters=" ");
bool parse_n_load(const string & input, string & name, string & type,
double & pay_rate);
#endif