Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string.h>
  5. #include "assocArray.h"
  6. using namespace std;
  7.  
  8. bool readInput(const string, assocArray<double>&);
  9. bool getArguments(int, char *[], string&, string&);
  10. bool printOutput(const string, assocArray<double>&);
  11.  
  12. double average = 0.0;
  13. double count = 0.0;
  14. double loScore1 = 10.0;
  15. string loTitle1 = "";
  16. double loScore2 = 10.0;
  17. string loTitle2 = "";
  18. double loScore3 = 10.0;
  19. string loTitle3 = "";
  20. double hiScore1 = 0.0;
  21. string hiTitle1 = "";
  22. double hiScore2 = 0.0;
  23. string hiTitle2 = "";
  24. double hiScore3 = 0.0;
  25. string hiTitle3 = "";
  26.  
  27. int main(int argc, char * argv[])
  28. {
  29.     assocArray<double> movieArr;
  30.     string inputFile;
  31.     string outputFile;
  32.  
  33.     if (!getArguments(argc, argv, inputFile, outputFile))
  34.         exit(1);
  35.  
  36.     if (!readInput(inputFile, movieArr))
  37.     {
  38.         cout << "readInput error" << endl;
  39.         exit(1);
  40.     }
  41.  
  42.     if (!printOutput(outputFile, movieArr))
  43.     {
  44.         cout << "printOutput error" << endl;
  45.         exit(1);
  46.     }
  47.  
  48.     return 0;
  49. }
  50.  
  51. bool getArguments(int argc, char * argv[], string& inputFile, string& outputFile)
  52. {
  53.     bool complete = 1;
  54.  
  55.     //cout << "argc: " << argc << endl;
  56.  
  57.     if (argc == 1)
  58.     {
  59.         complete = 0;
  60.         cout << "Usage: ./movieRatings -i <moviesFile> -o <outputFile>" << endl;
  61.         return complete;
  62.     }
  63.  
  64.     if (argc != 5)
  65.     {
  66.         complete = 0;
  67.         cout << "Error, invalid command line options." << endl;
  68.         return complete;
  69.     }
  70.  
  71.     if (strcmp(argv[1], "-i") != 0 && strcmp(argv[1], "-o") != 0)
  72.     {
  73.         complete = 0;
  74.         cout << "Error, invalid input file specifier." << endl;
  75.         return complete;
  76.     }
  77.  
  78.     if (strcmp(argv[3], "-i") != 0 && strcmp(argv[3], "-o") != 0)
  79.     {
  80.         complete = 0;
  81.         cout << "Error, invalid output file specifier." << endl;
  82.         return complete;
  83.     }
  84.  
  85.     if (strcmp(argv[1], "-o") == 0 && strcmp(argv[3], "-o") == 0)
  86.     {
  87.         complete = 0;
  88.         cout << "Error, invalid input file specifier." << endl;
  89.         return complete;
  90.     }
  91.  
  92.     if (strcmp(argv[1], "-i") == 0 && strcmp(argv[3], "-i") == 0)
  93.     {
  94.         complete = 0;
  95.         cout << "Error, invalid output file specifier." << endl;
  96.         return complete;
  97.     }
  98.  
  99.     if (strcmp(argv[1], "-i") == 0)
  100.     {
  101.         inputFile = argv[2];
  102.         outputFile = argv[4];
  103.     }
  104.     else
  105.     {
  106.         inputFile = argv[4];
  107.         outputFile = argv[2];
  108.     }
  109.     ifstream infile;
  110.     infile.open(inputFile);
  111.     ofstream outfile;
  112.     outfile.open(outputFile);
  113.  
  114.     if (!infile.is_open())
  115.     {
  116.         cout << "Error, unable to open input file." << endl;
  117.         complete = 0;
  118.     }
  119.  
  120.     if (!outfile.is_open())
  121.     {
  122.         complete = 0;
  123.         cout << "Error, unable to output file." << endl;
  124.     }
  125.  
  126.     infile.close();
  127.     outfile.close();
  128.  
  129.     return complete;
  130. }
  131.  
  132. bool readInput(const string inputFile, assocArray<double>& movieArr)
  133. {
  134.     ifstream infile;
  135.     infile.open(inputFile);
  136.     string movie = "";
  137.     string ratingStr = "";
  138.     double rating = 0.0;
  139.     double tempRating = 0.0;
  140.     string year = "";
  141.  
  142.     //cout << "readInput" << endl;
  143.  
  144.     while(getline(infile, movie, '\t'))
  145.     {
  146.         //if (movie == "")
  147.         //  break;
  148.         if (movie != "\n")
  149.         {
  150.         //infile >> rating;
  151.         getline(infile, ratingStr, '\t');
  152.         getline(infile, year);
  153.         rating = stod(ratingStr);
  154.         movie += "-" + year;
  155.  
  156.         if (!movieArr.exists(movie))
  157.         {
  158.             //cout << "exists" << endl;
  159.             movieArr[movie] = rating;
  160.         }
  161.         else
  162.         {
  163.            
  164.             //cout << "else" << endl;
  165.             tempRating = movieArr[movie];
  166.             rating = (rating + tempRating)/2;
  167.             movieArr[movie] = rating;
  168.         }
  169.  
  170.         average += rating;
  171.         count++;
  172.  
  173.         //cout << count << endl;
  174.        
  175.         //loScore ratings
  176.         if (rating < loScore3)
  177.         {
  178.             loScore3 = rating;
  179.             loTitle3 = movie;
  180.         }
  181.         if (loScore3 < loScore2)
  182.         {
  183.             swap(loScore2, loScore3);
  184.             swap(loTitle2, loTitle3);
  185.             //loScore2 = loScore3;
  186.         }
  187.         if (loScore2 < loScore1)
  188.         {
  189.             swap(loScore2, loScore1);
  190.             swap(loTitle2, loTitle1);
  191.             //loScore1 = loScore2;
  192.         }
  193.  
  194.         //hiScore Ratings
  195.         if (rating > hiScore3)
  196.         {
  197.             hiScore3 = rating;
  198.             hiTitle3 = movie;
  199.         }
  200.         if (hiScore3 > hiScore2)
  201.         {
  202.             swap(hiScore2, hiScore3);
  203.             swap(hiTitle2, hiTitle3);
  204.             //loScore2 = loScore3;
  205.         }
  206.         if (hiScore2 > hiScore1)
  207.         {
  208.             swap(hiScore2, hiScore1);
  209.             swap(hiTitle2, hiTitle1);
  210.             //loScore1 = loScore2;
  211.         }
  212.         //cout << "rating: " << movieArr[movie] << endl;
  213.  
  214.         //cout << movieArr.begin() << endl;
  215.  
  216.         //cout << movie << " " << rating << endl;
  217.         }
  218.     }
  219.  
  220.     average = average/count;
  221.     //cout << "loop out" << endl;
  222.  
  223.     return infile.is_open();
  224. }
  225.  
  226. bool printOutput(const string outputFile, assocArray<double>& movieArr)
  227. {
  228.     ofstream outfile;
  229.     outfile.open(outputFile);
  230.  
  231.     //cout << fixed << setprecision(2);
  232.  
  233.     //cout << movieArr.begin() << endl;
  234.  
  235.     //string curr = movieArr.begin();
  236.  
  237.     //cout << "print time" << endl;
  238.  
  239.     for (string curr=movieArr.begin(); curr != movieArr.end();
  240.         curr = movieArr.next())
  241.     {
  242.         //cout << "here" << endl;
  243.         outfile << "Movie: " << left << setw(50) << curr.substr (0,49) <<
  244.             "    Score: " << fixed << setprecision(1) << movieArr[curr] << endl;
  245.     }
  246.    
  247.     cout << "*****************************************************************" << endl;
  248.     cout << "CS 302 - Assignment #7" << endl;
  249.     cout << endl;
  250.     cout << "Overall Average: " << average << endl;
  251.     cout << endl;
  252.     cout << "Lowest Rated Movies:" << endl;
  253.     cout << "     Movie: " << setw(51) << left << loTitle1 << "  Score: " << loScore1 << endl;
  254.     cout << "     Movie: " << setw(51) << loTitle2 << "  Score: " << loScore2 << endl;
  255.     cout << "     Movie: " << setw(51) << loTitle3 << "  Score: " << loScore3 << endl;
  256.     cout << endl;
  257.     cout << "Highest Rated Movies:" << endl;
  258.     cout << "     Movie: " << setw(51) << hiTitle1 << "  Score: " << hiScore1 << endl;
  259.     cout << "     Movie: " << setw(51) << hiTitle2 << "  Score: " << hiScore2 << endl;
  260.     cout << "     Movie: " << setw(51) << hiTitle3 << "  Score: " << hiScore3 << endl;
  261.     cout << endl;
  262.     movieArr.showStats();
  263.     cout << endl;
  264.     cout << "*****************************************************************" << endl;
  265.     cout << "Game Over, thank you for playing." << endl;
  266.  
  267.     return outfile.is_open();
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement