Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab 7b
- //programmer: Raina Saunders
- //Editor: JNotePad
- //Compiler: XCode
- #include <fstream>
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <ctime>
- using namespace std;
- struct SubjectCode
- {
- string name;
- int count;
- };
- //the linked list node
- struct Node
- {
- SubjectCode data;
- Node *next;
- };
- Node* start = 0;
- #include <cstring>
- int main()
- {
- cout << "programmer: Raina Saunders" << endl;
- cout << "this program will read a file and output the courses and how many there are." << endl;
- //vector
- //variables
- Node *p;
- Node *q;
- Node *prev;
- int address;
- int numCodes = 0;
- int minIndex;
- SubjectCode minValue;
- // for parsing the inputfile
- char* token;
- char buf[1000];
- const char* const tab = "\t";
- //clock start
- clock_t startTime = clock();
- // open the input file
- ifstream fin;
- fin.open("dvc-schedule.txt");
- if (!fin.good()) throw "I/O error";
- // read the input file
- while (fin.good())
- {
- // read the line
- string line = "";
- getline(fin, line);
- strcpy(buf, line.c_str());
- if (buf[0] == 0) continue;
- // parse the line
- const string term(token = strtok(buf, tab));
- const string section(token = strtok(0, tab));
- const string course((token = strtok(0, tab)) ? token : "");
- if (course.find('-') == string::npos) continue; // invalid line
- const string subjectCode(course.begin(), course.begin() + course.find('-'));
- const string instructor((token = strtok(0, tab)) ? token : "");
- const string whenWhere((token = strtok(0, tab)) ? token : "");
- string tempString = subjectCode;
- for(p = start; p; p = p->next)
- {
- if (tempString.compare(p->data.name)==0)
- {
- p->data.count++;
- break;
- }
- else
- {
- Node *node = new Node;
- //find insertion point
- for (prev = 0, p = start; p; prev = p, p = p->next)
- {
- if (tempString.compare(p->data.name) != 0)
- break;
- }
- //insert node here
- node->data.name = subjectCode;
- node->data.count = 1;
- node->next = p;
- if (prev)
- prev->next = node;
- else
- start = node;
- }
- }
- }
- //outputting
- for (p = start; p; p = p->next)
- {
- cout << p->data.name << " " << p->data.count << endl;
- }
- //deallocate memory
- while (start)
- {
- p = start->next;
- delete start;
- start = p;
- }
- fin.close();
- //time
- double elapseSeconds = (double)(clock() - startTime) / CLOCKS_PER_SEC;
- std::cout.setf(std::ios::fixed|std::ios::showpoint); // requires iostream
- std::cout << std::setprecision(2); // requires iostream and iomanip
- cout << "time in seconds: " << elapseSeconds << endl;
- }
Add Comment
Please, Sign In to add comment