Guest User

Untitled

a guest
Mar 25th, 2013
357
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Movie.h" // include Movie class definition
  2. #include "Movies.h" // include Movies class definition
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. Movies::Movies(string fn){loadMovies(fn);}
  7.  
  8. int Movies::getMovieCount() const {return movieCnt;}
  9.  
  10. const Movie * Movies::getMovie(string mc) const {
  11. if(mc.length()==0)
  12. return NULL; // not found
  13. else {
  14. int hash = myHash(mc);
  15. if(hashArray[hash].getTitle() == mc)
  16. return &hashArray[hash];
  17. else
  18. return NULL;
  19. }
  20. }
  21.  
  22. const Movie * Movies::operator[](int ndx) const {
  23. // adjust for zero-based indexing
  24. return (ndx > 0 && ndx <= movieCnt)?&hashArray[ndx-1]:NULL;
  25. }
  26.  
  27. Movies::~Movies() {
  28. delete[] hashArray;
  29. hashArray = NULL;
  30. }
  31.  
  32. void Movies::loadMovies(string fn) {
  33. movieCnt++;
  34. ifstream iS(fn);
  35. string s;
  36. getline(iS, s); // skip heading
  37. getline(iS, s);
  38. movieCnt=0;
  39. hashArray = new Movie[hashSize];
  40. while(!iS.eof()) {
  41. hashArray[myHash(Movie(s).getTitle())] = Movie(s);
  42. movieCnt++;
  43. getline(iS, s);
  44. }
  45. iS.close();
  46. }
  47.  
  48. /*void Movies::reSize() {
  49. Movie * m = movies;
  50. movies = new Movie[movieCnt];
  51. for(int i=0;i<movieCnt;i++)
  52. movies[i] = m[i];
  53. }
  54.  
  55. const int Movies::myHash(string s) const {
  56. int hash = 0;
  57. const int FACTOR = 31, len = s.length();
  58. for (int i = 0; i < len; ++i)
  59. hash = FACTOR * hash + s[i];
  60. hash = abs(hash) % hashSize;
  61. return hash;
  62. }
  63.  
  64. void main()
  65. {
  66. Movies movies("Box Office Mojo.txt");
  67. if(movies.getMovieCount() > 0)
  68. {
  69. string movieCode;
  70. cout << "Please enter the movie title: ";
  71. getline(cin, movieCode);
  72. if (movieCode.length() > 0)
  73. {
  74. int mn = 0;
  75. mn=movies.myHash(movieCode);
  76. cout<<*movies.getMovie(mn)<<"n";
  77. }
  78. cout << "Please enter the movie title: ";
  79. getline(cin, movieCode);
  80. while (movieCode.length() > 0);
  81. {
  82. if (movieCode.length() > 0)
  83. {
  84. int mn = 0;
  85. mn=movies.myHash(movieCode);
  86. cout<<*movies.getMovie(mn)<<"n";
  87. }
  88. }
  89. }
  90. }
  91.  
  92. cout<<movies.getMovie(movieCode)
  93.  
  94. const Movie* m = movies.getMovie(movieCode);
  95. if (m != NULL) cout << m->getTitle();
RAW Paste Data