Advertisement
darkhelmet125

main.cpp

Dec 8th, 2011
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. /*Matt Short
  2. CPSC 131
  3. Assignment 8
  4. Purpose: create a program that will use the helper class to read information
  5. from a file, sort, and print the information into another file.*/
  6. #include<iostream>
  7. #include<string>
  8. #include<fstream>
  9. #include"QueueTemplate.h"
  10. #include"movieType.h"
  11. typedef MovieType ItemType;
  12. #include"PQType.h"
  13. using namespace std;
  14.  
  15. void getMovieInfo(PQType<MovieType>& rankSort);
  16. /*Purpose: read movie information from movie1.txt and sort by rank
  17. Pre: movie1.txt exists and contains movie info
  18. Post: movie info is read from movie1.txt and sorted by rank
  19. */
  20. void sortByGenre(PQType<MovieType>& rankSort, QueueType<MovieType>& genreSort, QueueType<MovieType>* movieGenres);
  21. /*Purpose: sort movie info by genre
  22. Pre: movie info is already read from file and sorted by rank
  23. Post: movie info is sorted by rank and genre
  24. */
  25. void printToFile(QueueType<MovieType>* movieGenres);
  26. /*Purpose: print movie info to file
  27. Pre: movie info is sorted by rank and genre
  28. Post: movie info is printed to file
  29. */
  30.  
  31. int main()
  32. {
  33.  
  34.     MovieType movie;//creates an instance of MovieType
  35.     PQType<MovieType> rankSort = PQType<MovieType> (50);//creates an array to hold movie info stored in a queue of size 50
  36.     QueueType<MovieType> movieGenres[8];//creates an array to hold movie genres
  37.     QueueType<MovieType> genreSort;//creates an instance of QueueType which is of type MovieType
  38.    
  39.     getMovieInfo(rankSort);
  40.     sortByGenre(rankSort, genreSort, movieGenres);
  41.     printToFile(movieGenres);
  42.     return 0;
  43. }
  44.  
  45. void getMovieInfo(PQType<MovieType>& rankSort)
  46. {
  47.     MovieType movie;//creates an instance of type MovieType
  48.     ifstream readFile;//creates an ifstream variable to read from a file
  49.     bool flag=true;//creates a flag to determine whether or not to continue an operation
  50.  
  51.     readFile.open("movie1.txt");//opens movie1.txt
  52.     while(flag&&readFile.is_open())//checks while flag=true and if file is open
  53.     {
  54.         flag=movie.readMovieInfo(readFile);//sets flag to rank
  55.         if(flag)//checks if true
  56.             rankSort.Enqueue(movie);//enqueues movie
  57.     }
  58.     readFile.close();//closes movie1.txt
  59. }
  60.  
  61. void sortByGenre(PQType<MovieType>& rankSort, QueueType<MovieType>& genreSort, QueueType<MovieType>* movieGenres)
  62. {
  63.     MovieType movie;//creates an instance of type MovieType
  64.     int i;//counter variable
  65.  
  66.     for(i=0;i<8;i++)//loops through all 8 genres
  67.     {
  68.         movieGenres[i]=genreSort;//initiates genre array
  69.     }
  70.     while(!rankSort.IsEmpty())//makes sure rank queue isn't empty
  71.     {
  72.         rankSort.Dequeue(movie);//dequeues movie
  73.         switch(movie.getGenre())//switch statement to put the movies in order according to genre
  74.         {
  75.         case 'A': movieGenres[0].Enqueue(movie);//action
  76.             break;
  77.         case 'C': movieGenres[1].Enqueue(movie);//comedy
  78.             break;
  79.         case 'D': movieGenres[2].Enqueue(movie);//drama
  80.             break;
  81.         case 'H': movieGenres[3].Enqueue(movie);//horror
  82.             break;
  83.         case 'M': movieGenres[4].Enqueue(movie);//mystery
  84.             break;
  85.         case 'R': movieGenres[5].Enqueue(movie);//romance
  86.             break;
  87.         case 'V': movieGenres[6].Enqueue(movie);//adventure
  88.             break;
  89.         case 'W': movieGenres[7].Enqueue(movie);//western
  90.             break;
  91.         }
  92.     }
  93. }
  94.  
  95. void printToFile(QueueType<MovieType>* movieGenres)
  96. {
  97.     MovieType movie;//creates an instance of type MovieType
  98.     ofstream printFile;//creates an ofstream variable to print to a file
  99.     string Movies[8]={"Action Movies","Comedy Movies","Drama Movies","Horror Movies","Mystery Movies","Romance Movies","Adventure Movies","Western Movies"};//creates string array of genres
  100.     int i;//counting variable
  101.     printFile.open("results.txt");//opens results.txt
  102.     for(i=0;i<8;i++)//loops through all 8 genres
  103.     {
  104.         printFile<<Movies[i]<<endl;//prints out title of genre
  105.         if(movieGenres[i].IsEmpty())//checks if movieGenres is empty
  106.             printFile<<"There are no movies for this genre."<<endl;
  107.         while(!movieGenres[i].IsEmpty())//makes sure that the genre array isn't empty
  108.         {
  109.             movieGenres[i].Dequeue(movie);//dequeues movie
  110.             movie.printMovieInfo(printFile);//prints movie info
  111.         }
  112.         printFile<<endl;
  113.     }
  114.     printFile.close();//closes results.txt
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement