Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Movie.h" // include Movie class definition
- #include "Movies.h" // include Movies class definition
- #include <fstream>
- using namespace std;
- Movies::Movies(string fn){loadMovies(fn);}
- int Movies::getMovieCount() const {return movieCnt;}
- const Movie * Movies::getMovie(string mc) const {
- if(mc.length()==0)
- return NULL; // not found
- else {
- int hash = myHash(mc);
- if(hashArray[hash].getTitle() == mc)
- return &hashArray[hash];
- else
- return NULL;
- }
- }
- const Movie * Movies::operator[](int ndx) const {
- // adjust for zero-based indexing
- return (ndx > 0 && ndx <= movieCnt)?&hashArray[ndx-1]:NULL;
- }
- Movies::~Movies() {
- delete[] hashArray;
- hashArray = NULL;
- }
- void Movies::loadMovies(string fn) {
- movieCnt++;
- ifstream iS(fn);
- string s;
- getline(iS, s); // skip heading
- getline(iS, s);
- movieCnt=0;
- hashArray = new Movie[hashSize];
- while(!iS.eof()) {
- hashArray[myHash(Movie(s).getTitle())] = Movie(s);
- movieCnt++;
- getline(iS, s);
- }
- iS.close();
- }
- /*void Movies::reSize() {
- Movie * m = movies;
- movies = new Movie[movieCnt];
- for(int i=0;i<movieCnt;i++)
- movies[i] = m[i];
- }
- const int Movies::myHash(string s) const {
- int hash = 0;
- const int FACTOR = 31, len = s.length();
- for (int i = 0; i < len; ++i)
- hash = FACTOR * hash + s[i];
- hash = abs(hash) % hashSize;
- return hash;
- }
- void main()
- {
- Movies movies("Box Office Mojo.txt");
- if(movies.getMovieCount() > 0)
- {
- string movieCode;
- cout << "Please enter the movie title: ";
- getline(cin, movieCode);
- if (movieCode.length() > 0)
- {
- int mn = 0;
- mn=movies.myHash(movieCode);
- cout<<*movies.getMovie(mn)<<"n";
- }
- cout << "Please enter the movie title: ";
- getline(cin, movieCode);
- while (movieCode.length() > 0);
- {
- if (movieCode.length() > 0)
- {
- int mn = 0;
- mn=movies.myHash(movieCode);
- cout<<*movies.getMovie(mn)<<"n";
- }
- }
- }
- }
- cout<<movies.getMovie(movieCode)
- const Movie* m = movies.getMovie(movieCode);
- if (m != NULL) cout << m->getTitle();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement