Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Matt Short
- CPSC 131
- Assignment 8
- Purpose: create a program that will use the helper class to read information
- from a file, sort, and print the information into another file.*/
- #include<iostream>
- #include<string>
- #include<fstream>
- #include"QueueTemplate.h"
- #include"movieType.h"
- typedef MovieType ItemType;
- #include"PQType.h"
- using namespace std;
- void getMovieInfo(PQType<MovieType>& rankSort);
- /*Purpose: read movie information from movie1.txt and sort by rank
- Pre: movie1.txt exists and contains movie info
- Post: movie info is read from movie1.txt and sorted by rank
- */
- void sortByGenre(PQType<MovieType>& rankSort, QueueType<MovieType>& genreSort, QueueType<MovieType>* movieGenres);
- /*Purpose: sort movie info by genre
- Pre: movie info is already read from file and sorted by rank
- Post: movie info is sorted by rank and genre
- */
- void printToFile(QueueType<MovieType>* movieGenres);
- /*Purpose: print movie info to file
- Pre: movie info is sorted by rank and genre
- Post: movie info is printed to file
- */
- int main()
- {
- MovieType movie;//creates an instance of MovieType
- PQType<MovieType> rankSort = PQType<MovieType> (50);//creates an array to hold movie info stored in a queue of size 50
- QueueType<MovieType> movieGenres[8];//creates an array to hold movie genres
- QueueType<MovieType> genreSort;//creates an instance of QueueType which is of type MovieType
- getMovieInfo(rankSort);
- sortByGenre(rankSort, genreSort, movieGenres);
- printToFile(movieGenres);
- return 0;
- }
- void getMovieInfo(PQType<MovieType>& rankSort)
- {
- MovieType movie;//creates an instance of type MovieType
- ifstream readFile;//creates an ifstream variable to read from a file
- bool flag=true;//creates a flag to determine whether or not to continue an operation
- readFile.open("movie1.txt");//opens movie1.txt
- while(flag&&readFile.is_open())//checks while flag=true and if file is open
- {
- flag=movie.readMovieInfo(readFile);//sets flag to rank
- if(flag)//checks if true
- rankSort.Enqueue(movie);//enqueues movie
- }
- readFile.close();//closes movie1.txt
- }
- void sortByGenre(PQType<MovieType>& rankSort, QueueType<MovieType>& genreSort, QueueType<MovieType>* movieGenres)
- {
- MovieType movie;//creates an instance of type MovieType
- int i;//counter variable
- for(i=0;i<8;i++)//loops through all 8 genres
- {
- movieGenres[i]=genreSort;//initiates genre array
- }
- while(!rankSort.IsEmpty())//makes sure rank queue isn't empty
- {
- rankSort.Dequeue(movie);//dequeues movie
- switch(movie.getGenre())//switch statement to put the movies in order according to genre
- {
- case 'A': movieGenres[0].Enqueue(movie);//action
- break;
- case 'C': movieGenres[1].Enqueue(movie);//comedy
- break;
- case 'D': movieGenres[2].Enqueue(movie);//drama
- break;
- case 'H': movieGenres[3].Enqueue(movie);//horror
- break;
- case 'M': movieGenres[4].Enqueue(movie);//mystery
- break;
- case 'R': movieGenres[5].Enqueue(movie);//romance
- break;
- case 'V': movieGenres[6].Enqueue(movie);//adventure
- break;
- case 'W': movieGenres[7].Enqueue(movie);//western
- break;
- }
- }
- }
- void printToFile(QueueType<MovieType>* movieGenres)
- {
- MovieType movie;//creates an instance of type MovieType
- ofstream printFile;//creates an ofstream variable to print to a file
- string Movies[8]={"Action Movies","Comedy Movies","Drama Movies","Horror Movies","Mystery Movies","Romance Movies","Adventure Movies","Western Movies"};//creates string array of genres
- int i;//counting variable
- printFile.open("results.txt");//opens results.txt
- for(i=0;i<8;i++)//loops through all 8 genres
- {
- printFile<<Movies[i]<<endl;//prints out title of genre
- if(movieGenres[i].IsEmpty())//checks if movieGenres is empty
- printFile<<"There are no movies for this genre."<<endl;
- while(!movieGenres[i].IsEmpty())//makes sure that the genre array isn't empty
- {
- movieGenres[i].Dequeue(movie);//dequeues movie
- movie.printMovieInfo(printFile);//prints movie info
- }
- printFile<<endl;
- }
- printFile.close();//closes results.txt
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement