Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5. #include "WordHash.h"
  6.  
  7. void read(WordHash & hash, char* fileName);
  8.  
  9. int main(int argc, char *args[])
  10. {
  11. // check for input
  12. if(argc != 2) {
  13. cerr << "Please specify filename on command-line\n";
  14. return 1;
  15. } // end if
  16.  
  17. WordHash hash(103); // create hash table
  18. read( hash, args[1] );
  19. hash.printCommon();
  20. return 0;
  21. } // end main
  22.  
  23.  
  24. // Method: read
  25. // Arguments: String fileName - name of file to read
  26. // Purpose: reads a file, adding the words to the hash table
  27. void read(WordHash & hash, char* fileName)
  28. {
  29. // delimiters used in StringTokenizer
  30. string delimiters = " .,!?:;@#$%^&*-_=+1234567890";
  31.  
  32. string line;
  33.  
  34. ifstream in ( fileName );
  35. if( !in ) {
  36. cerr << "Cannot open " << fileName;
  37. return;
  38. }
  39. while( getline(in, line) ) {
  40. int lineLen = line.length();
  41. for(int i=0; i<lineLen; i++)
  42. if( line[i]>='A' && line[i]<='Z' )
  43. line[i] += 'a'-'A' ;
  44. int start = line.find_first_not_of(delimiters,0);
  45. while( start>=0 && start<lineLen ) {
  46. int stop = line.find_first_of(delimiters,start);
  47. if( stop<0 || stop>lineLen)
  48. stop = lineLen;
  49. hash.addWord( line.substr( start, stop-start ) );
  50. start = line.find_first_not_of( delimiters, stop+1);
  51. } // end while
  52. } // end while
  53.  
  54. } // end read
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement