Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string.h>
- #include "assocArray.h"
- using namespace std;
- bool readInput(const string, assocArray<double>&);
- bool getArguments(int, char *[], string&, string&);
- bool printOutput(const string, assocArray<double>&);
- double average = 0.0;
- double count = 0.0;
- double loScore1 = 10.0;
- string loTitle1 = "";
- double loScore2 = 10.0;
- string loTitle2 = "";
- double loScore3 = 10.0;
- string loTitle3 = "";
- double hiScore1 = 0.0;
- string hiTitle1 = "";
- double hiScore2 = 0.0;
- string hiTitle2 = "";
- double hiScore3 = 0.0;
- string hiTitle3 = "";
- int main(int argc, char * argv[])
- {
- assocArray<double> movieArr;
- string inputFile;
- string outputFile;
- if (!getArguments(argc, argv, inputFile, outputFile))
- exit(1);
- if (!readInput(inputFile, movieArr))
- {
- cout << "readInput error" << endl;
- exit(1);
- }
- if (!printOutput(outputFile, movieArr))
- {
- cout << "printOutput error" << endl;
- exit(1);
- }
- return 0;
- }
- bool getArguments(int argc, char * argv[], string& inputFile, string& outputFile)
- {
- bool complete = 1;
- //cout << "argc: " << argc << endl;
- if (argc == 1)
- {
- complete = 0;
- cout << "Usage: ./movieRatings -i <moviesFile> -o <outputFile>" << endl;
- return complete;
- }
- if (argc != 5)
- {
- complete = 0;
- cout << "Error, invalid command line options." << endl;
- return complete;
- }
- if (strcmp(argv[1], "-i") != 0 && strcmp(argv[1], "-o") != 0)
- {
- complete = 0;
- cout << "Error, invalid input file specifier." << endl;
- return complete;
- }
- if (strcmp(argv[3], "-i") != 0 && strcmp(argv[3], "-o") != 0)
- {
- complete = 0;
- cout << "Error, invalid output file specifier." << endl;
- return complete;
- }
- if (strcmp(argv[1], "-o") == 0 && strcmp(argv[3], "-o") == 0)
- {
- complete = 0;
- cout << "Error, invalid input file specifier." << endl;
- return complete;
- }
- if (strcmp(argv[1], "-i") == 0 && strcmp(argv[3], "-i") == 0)
- {
- complete = 0;
- cout << "Error, invalid output file specifier." << endl;
- return complete;
- }
- if (strcmp(argv[1], "-i") == 0)
- {
- inputFile = argv[2];
- outputFile = argv[4];
- }
- else
- {
- inputFile = argv[4];
- outputFile = argv[2];
- }
- ifstream infile;
- infile.open(inputFile);
- ofstream outfile;
- outfile.open(outputFile);
- if (!infile.is_open())
- {
- cout << "Error, unable to open input file." << endl;
- complete = 0;
- }
- if (!outfile.is_open())
- {
- complete = 0;
- cout << "Error, unable to output file." << endl;
- }
- infile.close();
- outfile.close();
- return complete;
- }
- bool readInput(const string inputFile, assocArray<double>& movieArr)
- {
- ifstream infile;
- infile.open(inputFile);
- string movie = "";
- string ratingStr = "";
- double rating = 0.0;
- double tempRating = 0.0;
- string year = "";
- //cout << "readInput" << endl;
- while(getline(infile, movie, '\t'))
- {
- //if (movie == "")
- // break;
- if (movie != "\n")
- {
- //infile >> rating;
- getline(infile, ratingStr, '\t');
- getline(infile, year);
- rating = stod(ratingStr);
- movie += "-" + year;
- if (!movieArr.exists(movie))
- {
- //cout << "exists" << endl;
- movieArr[movie] = rating;
- }
- else
- {
- //cout << "else" << endl;
- tempRating = movieArr[movie];
- rating = (rating + tempRating)/2;
- movieArr[movie] = rating;
- }
- average += rating;
- count++;
- //cout << count << endl;
- //loScore ratings
- if (rating < loScore3)
- {
- loScore3 = rating;
- loTitle3 = movie;
- }
- if (loScore3 < loScore2)
- {
- swap(loScore2, loScore3);
- swap(loTitle2, loTitle3);
- //loScore2 = loScore3;
- }
- if (loScore2 < loScore1)
- {
- swap(loScore2, loScore1);
- swap(loTitle2, loTitle1);
- //loScore1 = loScore2;
- }
- //hiScore Ratings
- if (rating > hiScore3)
- {
- hiScore3 = rating;
- hiTitle3 = movie;
- }
- if (hiScore3 > hiScore2)
- {
- swap(hiScore2, hiScore3);
- swap(hiTitle2, hiTitle3);
- //loScore2 = loScore3;
- }
- if (hiScore2 > hiScore1)
- {
- swap(hiScore2, hiScore1);
- swap(hiTitle2, hiTitle1);
- //loScore1 = loScore2;
- }
- //cout << "rating: " << movieArr[movie] << endl;
- //cout << movieArr.begin() << endl;
- //cout << movie << " " << rating << endl;
- }
- }
- average = average/count;
- //cout << "loop out" << endl;
- return infile.is_open();
- }
- bool printOutput(const string outputFile, assocArray<double>& movieArr)
- {
- ofstream outfile;
- outfile.open(outputFile);
- //cout << fixed << setprecision(2);
- //cout << movieArr.begin() << endl;
- //string curr = movieArr.begin();
- //cout << "print time" << endl;
- for (string curr=movieArr.begin(); curr != movieArr.end();
- curr = movieArr.next())
- {
- //cout << "here" << endl;
- outfile << "Movie: " << left << setw(50) << curr.substr (0,49) <<
- " Score: " << fixed << setprecision(1) << movieArr[curr] << endl;
- }
- cout << "*****************************************************************" << endl;
- cout << "CS 302 - Assignment #7" << endl;
- cout << endl;
- cout << "Overall Average: " << average << endl;
- cout << endl;
- cout << "Lowest Rated Movies:" << endl;
- cout << " Movie: " << setw(51) << left << loTitle1 << " Score: " << loScore1 << endl;
- cout << " Movie: " << setw(51) << loTitle2 << " Score: " << loScore2 << endl;
- cout << " Movie: " << setw(51) << loTitle3 << " Score: " << loScore3 << endl;
- cout << endl;
- cout << "Highest Rated Movies:" << endl;
- cout << " Movie: " << setw(51) << hiTitle1 << " Score: " << hiScore1 << endl;
- cout << " Movie: " << setw(51) << hiTitle2 << " Score: " << hiScore2 << endl;
- cout << " Movie: " << setw(51) << hiTitle3 << " Score: " << hiScore3 << endl;
- cout << endl;
- movieArr.showStats();
- cout << endl;
- cout << "*****************************************************************" << endl;
- cout << "Game Over, thank you for playing." << endl;
- return outfile.is_open();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement