adding cs142 code
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user