1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
using namespace std;
int hashCode(const string&);
int main()
{
map<string, set<string>> allClasses;
//for parsing the inputfile
char* token;
char buf[1000];
const char* const tab = "\t";
//open the inout file
ifstream fin;
fin.open("course-schedule.txt");
if(!fin.good()) cout << "I/O error";
//read the input file
while (fin.good())
{
//read the limne
string line;
getline(fin, line);
strcpy(buf, line.c_str());
if(buf[0] == 0) continue; //skip blank lines
//parse the line
string term(token = strtok(buf, tab));
const string section((token = strtok(0, tab))? token : " ");
const string course((token = strtok(0, tab))? token : " ");
const string instructor((token = strtok(0, tab))? token : " ");
const string whenWhere((token = strtok(0, tab))? token : " ");
if(course.find('-') == string::npos)continue; //invalid line: no dash in course name
const string subjectCode(course.begin(), course.begin() + course.find('-'));
const string classCode = term + section;
const string termOrder(term.begin(), term.begin() + term.find(' '));
if(termOrder == "Spring")
{
term.push_back('1');
term.erase(0,7);
}
else if(termOrder == "Summer")
{
term.push_back('2');
term.erase(0, 7);
}
else if(termOrder == "Fall")
{
term.push_back('3');
term.erase(0, 5);
}
allClasses[course].insert(term);
}
///////////////////////
////////DISPLAY////////
string userInput;
while(true)
{
cout << "Enter a course name (like, COMSC-210) to search for the first and last semester the course was offered [X/x to exit]: "<< endl;
cin >> userInput;
if(userInput == "x" || userInput == "X")
break;
set<string>::iterator itr;
set<string>::reverse_iterator ritr;
for(itr = allClasses[userInput].begin(); itr != allClasses[userInput].end(); itr++)
for(ritr = allClasses[userInput].rbegin(); ritr != allClasses[userInput].rend(); ritr++)
{
if(itr == allClasses[userInput].begin() && ritr == allClasses[userInput].rbegin())
{
cout << userInput << " was first offered in " << *itr << endl;
cout << userInput << " was last offered in " << *ritr << endl;
}
}
}
cout << "Program Finished!";
return 0;
}
|