For this computer assignment, you are to write and implement an interactive C++
ID: 3872102 • Letter: F
Question
For this computer assignment, you are to write and implement an interactive C++ program to scan and process a stream of words of a plain text.
Use a map container to store the words, where each word is represented by a pair < string, int >. The first element of the pair, named first, contains the name of the input word, and the second element of the pair, named second, contains its frequency in the input stream. To use a map container in your program, you need to insert the statement: #include in your header file, and to use the functions in the STL, you also need to insert the statement: #include in your header file.
In addition to the main ( ) routine, implement the following subroutines in your program:
• void get_words ( map < string, int >& ) : It gets a word from the input stream and removes its punctuation marks.
• void print_words ( const map < string, int >& ) : It prints the final list of words and their frequencies. It also prints the number of nonempty words and the number of distinct words in the input stream.
• void clean_entry ( const string&, string& ) : It cleans a word from its punctuation marks. The first argument is the original word in the input stream and the second argument contains the same word after cleaning.
The main ( ) routine calls the subroutine get_words ( ) to get words from the input stream, and to clean an individual word from its punctuation marks, get_words ( ) calls the subroutine clean_entry ( ).
You can check the size of a string by the member function length ( ). For string s, the statement: s.length ( ) returns the total number of characters in s. If the returned length is 0 (i.e., the original word contains nothing but punctuation marks), simply ignore the word and read in the next one.
Use a map container to store the cleaned words (but not the empty words). The statement: m [ s ]++ copies the string s into the map m and updates its frequency. Remember a map keeps only one copy of each individual item (in sorted order) specified in its first argument, key, and stores its frequency in its second argument, value. When key is used as an index to a map, it returns the corresponding value in the map.
Print the number of nonempty words in the input stream and the number of distinct words. You can obtain the number of elements in a map by the member function size ( ). Also print the contents of your map: name of each word and its frequency (NO_ITEMS words per line and the words are left aligned). To allocate certain number of spaces for an output value, use the function setw ( ITEM_W ), and for its left alignment, use left in your cout statement. In your program, use NO_ITEMS = 3, and ITEM_W = 16. For printing, the main ( ) routine calls the subroutine print_words ( )
Explanation / Answer
#include <iostream>
#include <cctype>
#include <algorithm>
#include <string>
#include <map>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef map < string, int >::const_iterator it_type; //Declaring the map type
const int NO_ITEMS = 3; //Number of items delcaration, const value
const int ITEM_W = 16; //Item width declaration, const value.
// isA function, determines if character is alphanumeric or not, returns the result of the std function isalnum()
bool isA ( const char& c){
return isalnum(c);
}
// isNotA function, determines if character is alphanumeric or not, returns the opposite of the result of the std function isalnum()
bool isNotA ( const char& c){
return !isalnum(c);
}
// convert_to_low function, takes a character and calls the tolower() std function to convert it to lower case. Returns nothing.
void convert_to_low ( char& c){
c = tolower(c);
return;
}
// print_words function, takes a map as an argument and prints the contents. Returns nothing.
void print_words(const map < string, int >& map) {
int counter = 0; //Initialize counter for rows
int number = 0; //initialize number of words for columns
for(it_type it = map.begin(); it != map.end(); ++it) { //for loop with iterator to iterate through map
if(counter == NO_ITEMS){ //If the counter gets to the number of items max allowed in a row,
counter = 0; //reset counter and
cout << endl; //start a new line.
}
number += (*it).second; //increment number with the map value
cout << setw(ITEM_W) << left << (*it).first << " : " << setw(3) << (*it).second << " "; //cout statement with formatting
counter++; //increment counter value.
}
cout << endl << endl; //formatting
cout << "number of words in input stream : " << number << endl; //text to print at end of output
cout << "number of words in output stream : " << (int)map.size(); // ^^^
}
// clean_entry function, takes 2 strings as arguments, removes all punctuation characters, and sets the words to lowercase.
void clean_entry( const string& word1, string& word2) {
int index = 0; //initialize index value to determine first alnum character
int indexEnd = 0; //initialize indexEnd value to determine space after last alnum character
//setting bool values to determine formatting requirements
bool hasPunc = false;
bool start = false;
bool end = false;
for(int count=0; count<(int)word1.length(); count++){
if(isNotA(word1[count])){ //If there is no alnum character, hasPunc is set to true
hasPunc = true;
}
if(isA(word1[count]) && start == false){
start = true; //If there is an alnum character and start is still false, will set start equal to true and index to count.
index = count;
}
if(start == true){ //If start is true and isNotA returns true, end will set to true and indexEnd will equal count - This defines the location of the word without punctuation characters
if(isNotA(word1[count])){
end = true;
indexEnd = count;
}
}
if(end == true){ //If the end value has been found, break out of loop - this is what ends the word with non alnum characters in the middle of the word. (i.e. fish-sticks to just fish)
break;
}
}
word2 = word1.substr(index, indexEnd - index); //Sets word2 equal to word1's substring with the boundaries being the indexes from above.
for_each(word2.begin(), word2.end(), convert_to_low); //calls the convert_to_low function to change characters to lowercase.
if(hasPunc==false){ //If the hasPunc is false, there is no need to use a substr boundary, and word2 can just equal the word1 in its entirety.
word2 = word1;
for_each(word2.begin(), word2.end(), convert_to_low); //calls the convert_to_low function to change characters to lowercase.
}
}
// get_words function, gets the words from the input and calls the clean_entry function to clean the words, then deposits them into the map. Takes a map and the input file as arguments, returns nothing.
void get_words(map<string, int>& map, ifstream& infile) {
string toBeCleaned, word;
while (infile >> toBeCleaned){ //While new words are retrieved
clean_entry(toBeCleaned, word); // call clean_entry with toBeCleaned value and word value
if (word.length() > 0){ // if the returned value is greater than 0,
map[word]++; // deposit into map.
}
}
if(infile.eof()){ //If the eof is reached,
return; // return back to main().
}
}
int main(int argc, char** argv) {
// if ( argc < 2 ) { // if the argument line isn't used correctly, prints error message.
// cerr << "command line argument: input.txt" << endl;
// return 1;
// }
ifstream infile("C:\Users\Swapnil\CLionProjects\Words\input.txt"); //Reads in the input file from the command line with ios::in
map<string, int> m; //Declares map m
get_words( m, infile ); //calls get_words function to get the words into the map
infile.close(); // closes input file
print_words( m ); //calls print_words to print contents of the map
return 0;
}
--------------------------------------------------------------------------------------------------
input.txt
---------------------------------------------
This is a book about the computer language called C. If you are
seeking a book to increase your typing speed, expand on your
knowledge of word processing, or learn the secrets of chip
fabrication and design, this is not the one for you. However , if
you want to become thoroughly familiar with the C programming
language, then you have made a wise choice. For this book is devoted
to just that--to help you become proficient in C
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.