SHARE
TWEET

Untitled

a guest Jan 18th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Lab 7b
  2. //programmer: Raina Saunders
  3. //Editor: JNotePad
  4. //Compiler: XCode
  5.  
  6. #include <fstream>
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <string>
  10. #include <ctime>
  11. using namespace std;
  12.  
  13. struct SubjectCode
  14. {
  15.     string name;
  16.     int count;
  17. };
  18.  
  19. //the linked list node
  20. struct Node
  21. {
  22.     SubjectCode data;
  23.     Node *next;
  24. };
  25.  
  26. Node* start = 0;
  27.  
  28. #include <cstring>
  29.  
  30. int main()
  31. {
  32.    
  33.   cout << "programmer: Raina Saunders" << endl;
  34.   cout << "this program will read a file and output the courses and how many there are." << endl;
  35.   //vector
  36.   //variables
  37.   Node *p;
  38.   Node *q;
  39.   Node *prev;
  40.   int address;
  41.   int numCodes = 0;
  42.   int minIndex;
  43.   SubjectCode minValue;
  44.    
  45.   // for parsing the inputfile
  46.   char* token;
  47.   char buf[1000];
  48.   const char* const tab = "\t";
  49.    
  50.   //clock start
  51.   clock_t startTime = clock();
  52.    
  53.   // open the input file
  54.   ifstream fin;
  55.   fin.open("dvc-schedule.txt");
  56.   if (!fin.good()) throw "I/O error";  
  57.    
  58.   // read the input file
  59.   while (fin.good())
  60.   {
  61.     // read the line
  62.     string line = "";
  63.     getline(fin, line);
  64.     strcpy(buf, line.c_str());
  65.     if (buf[0] == 0) continue;
  66.    
  67.     // parse the line
  68.     const string term(token = strtok(buf, tab));
  69.     const string section(token = strtok(0, tab));
  70.     const string course((token = strtok(0, tab)) ? token : "");
  71.     if (course.find('-') == string::npos) continue; // invalid line
  72.     const string subjectCode(course.begin(), course.begin() + course.find('-'));
  73.     const string instructor((token = strtok(0, tab)) ? token : "");
  74.     const string whenWhere((token = strtok(0, tab)) ? token : "");
  75.        
  76.     string tempString = subjectCode;
  77.     for(p = start; p; p = p->next)
  78.     {
  79.       if (tempString.compare(p->data.name)==0)
  80.       {
  81.         p->data.count++;
  82.         break;
  83.       }
  84.       else
  85.       {
  86.         Node *node = new Node;
  87.        
  88.         //find insertion point
  89.         for (prev = 0, p = start; p; prev = p, p = p->next)
  90.         {
  91.           if (tempString.compare(p->data.name) != 0)
  92.             break;
  93.         }
  94.         //insert node here
  95.         node->data.name = subjectCode;
  96.         node->data.count = 1;
  97.         node->next = p;
  98.         if (prev)
  99.           prev->next = node;
  100.         else
  101.           start = node;
  102.       }
  103.     }  
  104.   }
  105.    
  106.   //outputting
  107.   for (p = start; p; p = p->next)
  108.   {
  109.   cout << p->data.name << " " << p->data.count << endl;
  110.   }
  111.   //deallocate memory
  112.   while (start)
  113.   {
  114.     p = start->next;
  115.     delete start;
  116.     start = p;
  117.   }
  118.   fin.close();
  119.    
  120.   //time
  121.   double elapseSeconds = (double)(clock() - startTime) / CLOCKS_PER_SEC;
  122.   std::cout.setf(std::ios::fixed|std::ios::showpoint); // requires iostream
  123.   std::cout << std::setprecision(2); // requires iostream and iomanip
  124.   cout << "time in seconds: " << elapseSeconds << endl;
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top