1 Replies - 263 Views - Last Post: 29 September 2013 - 12:56 PM Rate Topic: -----

#1 ovaltine99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-September 13

Help with creating a counter with STL template <deque>

Posted 29 September 2013 - 12:44 PM

I've got a programming assignment where we need to read in a .txt file containing a list of classes. However, in this list there are duplicate classes that we need to count as well. My program is able to read in the number of total classes, sort them, and count the number but I've yet to be able to figure out a way to count the number of duplicate classes.

If anyone could help I'd greatly appreciate it. People have suggested to do a "linear search"? However, I've got no idea how to do implement this with <deque>.

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <deque>
using namespace std;

struct SubjectCode
{
  string name;
  int count;
};

bool operator == (const SubjectCode&, const SubjectCode&);

int main()
{
  // Objects and variables
  deque<SubjectCode> subjectCodes;
  string read;
  int res;
  int totalDup=0; //total duplicates
  int size=0; //size of array
  SubjectCode temp;


  // For parsing the input file
  char* token;
  char buf[1000];
  const char* const tab = "\t";
  
  // Open the input file
  ifstream fin;
  fin.open("dvc-schedule.txt");
  if (!fin.good()) throw "I/O error";
  
  // Start the time!
  clock_t startTime = clock();
  
  // 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 : "");
    const string instructor((token = strtok(0, tab)) ? token : "");
    const string whenWhere((token = strtok(0, tab)) ? token : "");
    if (course.find('-') == string::npos) continue; // invalid line
    const string subjectCode(course.begin(), course.begin() + course.find('-'));   
    
    bool found = false;
    
    // Traverse the deque, check for duplicates of a specific course type
    // If a duplicate is found, increment it's count
    for (int i = 0; i < subjectCodes.size(); i++)
    {
      if (subjectCodes[i].name == subjectCode)
      {
        found = true;
        subjectCodes[i].count++;
        break;
      }
    }
    
    // If no duplicates are found, create a new object and initialize the course data
    if (!found)
    {
      SubjectCode sc;
      sc.count = 0;
      
      sc.name = subjectCode;
      sc.count++;
      subjectCodes.push_back(sc);
    }
  }
  
  // Close the file
  double elapsedSeconds = (double)(clock() - startTime) / CLOCKS_PER_SEC;

  fin.close();
  
  // Sort the vector in alphabetical order
  for (int i = 0; i < subjectCodes.size(); i++)
  {
    for (int k = i + 1; k < subjectCodes.size(); k++)
    {
      if (subjectCodes[i].name > subjectCodes[k].name)
      {
        SubjectCode temp = subjectCodes[i];
        subjectCodes[i] = subjectCodes[k];
        subjectCodes[k] = temp;
      }
    }
  }
  
  for (int i = 0; i < subjectCodes.size();i++)
  {
    cout << "Course           : " << subjectCodes[i].name << endl;
    cout << "Number of classes: " << subjectCodes[i].count << endl;
    cout << endl;
    cout << "Time elapsed: " << elapsedSeconds << endl;

  }
}

// operator ==
bool operator == (const SubjectCode& a, const SubjectCode & B)/>
{
  if (a.name == b.name)
    return true;
  else
    return false;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with creating a counter with STL template <deque>

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,497
  • Joined: 05-May 12

Re: Help with creating a counter with STL template <deque>

Posted 29 September 2013 - 12:56 PM

Do not open duplicate topics. Use the other thread you had going.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1