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
+29
View File
@@ -0,0 +1,29 @@
CXX=g++
CPPFLAGS=-Wall -g -std=c++0x
SOURCES=main.cc util.cc person.cc
OBJECTS=$(SOURCES:.cc=.o)
EXE=restaurant
all: $(EXE)
main.o: main.cc util.o person.o
util.o: util.h util.cc
person.o: person.cc person.h
$(EXE): $(OBJECTS)
$(CXX) $(LDFLAGS) $(OBJECTS) -o $@
run: $(EXE)
./$(EXE) people.db /tmp/output.db
cat /tmp/output.db
clean:
@rm -vf *.o
@rm -rvf *.dSYM
@rm -vf $(EXE)
debug: $(EXE)
gdb $(EXE)
valgrind: $(EXE)
valgrind --tool=memcheck --leak-check=yes ./$(EXE) people.db /tmp/output.db
+29
View File
@@ -0,0 +1,29 @@
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;
#include "person.h"
#include "util.h"
const string usage = "usage: app <input file> <output file>";
int main(int argc, char * argv []) {
if(argc != 3) {
cerr << usage << endl;
return 1;
}
string input_filename(argv[1]);
string output_filename(argv[2]);
try {
vector<person> people = parse_file(input_filename);
save_file(output_filename, people);
}
catch(runtime_error e) {
cerr << e.what() << endl;
return 1;
}
return 0;
}
+3
View File
@@ -0,0 +1,3 @@
stephen mcquay , 31
derek mcquay , 21
jimmy joshn shimmershi ne the fourth , 32
+7
View File
@@ -0,0 +1,7 @@
#include <ostream>
#include "person.h"
ostream & operator<<(ostream & os, person & p) {
os << p.name << ", " << p.age;
return os;
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef __PERSON_H__
#define __PERSON_H__
#include <ostream>
#include <string>
using namespace std;
struct person {
string name;
int age;
person(string name, int age): name(name), age(age) {};
friend ostream & operator<<(ostream & os, person & p);
};
#endif
+82
View File
@@ -0,0 +1,82 @@
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <stdexcept>
using namespace std;
#include "util.h"
#include "person.h"
ostream & operator<<(ostream & os, vector<int> & v) {
os << "[";
for(unsigned int i = 0; i < v.size(); i++) {
os << v[i];
if(i != v.size() - 1) {
os << ", ";
}
}
os << "]";
return os;
}
vector<string> tokenize(const string & str, const string & delimiters) {
vector<string> tokens;
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
return tokens;
}
vector<person> parse_file(string filename) {
ifstream inputs(filename.c_str());
if(inputs.good()) {
vector<person> people;
string line;
int age;
while(getline(inputs, line)) {
vector<string> tokens = tokenize(line, ",");
string name = tokens[0];
string age_s = tokens[1];
//clean up name
vector<string> name_tokens = tokenize(name, " ");
name.clear();
for(unsigned int i = 0; i < name_tokens.size(); i++) {
name += name_tokens[i];
if(i != name_tokens.size() - 1) {
name += " ";
}
}
// parse an int
stringstream age_parser(age_s);
age_parser >> age;
people.push_back(person(name, age));
}
return people;
}
else {
throw runtime_error("Input file was not good; please verify.");
}
}
void save_file(string filename, const vector<person> & people) {
ofstream output_file(filename.c_str());
for(auto p: people) {
output_file << p << endl;
}
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef __UTIL_H__
#define __UTIL_H__
#include <vector>
#include <string>
#include <ostream>
using namespace std;
#include "person.h"
ostream & operator<<(ostream &, vector<bool> &);
vector<string> tokenize(const string & str, const string & delimiters=" ");
vector<person> parse_file(string filename);
void save_file(string filename, const vector<person> & people);
#endif