Advertisement
rinab333

CSCI 340 assign 4

Jul 16th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. //Terina BUrr
  2. //Assignment four
  3. //due 10/02/15
  4. //section one
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <map>
  8. #include <algorithm>
  9. #include <fstream>
  10. using namespace std;
  11. int ITEM_W = 16;
  12. int NO_ITEMS = 3;
  13. void print_words( const map<string, int> &m );
  14.  
  15. void clean_entry( const string &x, string &y );
  16.  
  17. void get_words( map<string, int> &m, ifstream &y );
  18.  
  19. int main(int argc, char** argv) {
  20.     if ( argc < 2 ) {
  21.         cerr << "command line argument: input-file-name" << endl;
  22.         return 1;
  23.     }
  24.  
  25.     ifstream infile(argv[1], ios::in);
  26.  
  27.     map<string, int> m;
  28.  
  29.     get_words( m, infile );
  30.  
  31.     infile.close();
  32.  
  33.     print_words( m );
  34.  
  35.     return 0;
  36. }
  37. /***************************************************************
  38. Function: print_words
  39.  
  40. Use:      prints out the clean words to the screen and prints the number of nonempty words and the number of distinct words in 3 columns
  41.  
  42. Arguments:  1. m: map where clean words are
  43.        
  44. Returns:   nothing
  45. ***************************************************************/
  46.  
  47. void print_words( const map<string, int>&m ) {
  48. typedef map<string, int> mapType;
  49.     mapType::const_iterator i; //iterator to go through the map
  50.     int count = 0;
  51.     int words = 0;
  52.     for( i = m.begin(); i!=m.end(); i++)
  53.     {
  54. //using iterator goes through the map
  55.         cout << left << setw(ITEM_W) <<i ->first<<": " << setw(NO_ITEMS)<< i->second;
  56. // prints out the word and its frequency
  57.         count++;
  58.         words += i->second;
  59.         if(count == NO_ITEMS)
  60.         {
  61.             cout<<endl;
  62.             count = 0;
  63. //if count = the number of items allowed then it starts a new line and count returns to 0
  64.         }
  65. // if there is more than three words per line then start a new line
  66.     }
  67.     cout << endl << "no of words in input stream:  " << words << endl;
  68.     cout << "no of words in output stream: " << m.size() << endl;
  69.  
  70.  
  71.  
  72. }
  73. /***************************************************************
  74. Function: clean_entry
  75.  
  76. Use:      this cleans a word from punctuation marks
  77.  
  78. Arguments:  1. x: original dirty word with punctuation marks
  79.         2. y: the cleaned word without punctuation
  80. Returns:   nothing
  81. ***************************************************************/
  82.  
  83. void clean_entry( const string &x, string &y ) {
  84.  
  85.     int start = 0; // start pos of the clean in the dirty one
  86.     int current = 0; // current pos of where it is in the loop
  87.     int sl = 0; //length of the new string
  88.    
  89.    // Finds the start of x  word
  90.     while(start < (int) x.size())
  91.     {
  92.             if(isalnum(x[start]) )
  93.                 break;
  94.              start++;
  95.     }
  96.    
  97.    current = start;
  98.  
  99.    // Finds the length  of y( clean word)     /
  100.    while(current < (int) x.size())
  101.    {
  102.       if(isalnum(x[current]) )
  103.       {  
  104.          sl++;
  105.      current++;
  106.       }
  107.       else
  108.          break;
  109.    }
  110.    if(start< (int) x.size())
  111.       y = x.substr(start, sl);
  112.     //get clean word
  113.    for(int i = 0; i < (int) y.size(); i++)
  114.    {
  115.       y[i] = tolower(y[i]);
  116.    }
  117. //puts in lower case
  118.  
  119. }
  120. /***************************************************************
  121. Function: get_words
  122.  
  123. Use:      this function removes punctuation marks by using clean entry and saves the clean words to the map m
  124.  
  125. Arguments:  1. m: map where clean words go
  126.         2. y:  file that the words are read from
  127.  
  128. Returns:   nothing
  129. ***************************************************************/
  130.  
  131. void get_words( map<string, int> &m, ifstream &y ) {
  132.    
  133.     string dirty, clean;
  134.         while(!y.eof()) // while there is an input
  135.         {
  136.              y>> dirty;
  137.                 clean_entry(dirty, clean); // calls the clean entry functiom
  138.                 if(clean.length() > 0 )
  139.                 {
  140.                         m[clean]++; // adds it to set if equal to zero
  141.  
  142.                }
  143.     clean =""; 
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement