PLEASE WRITE C++ PROGRAM you will rework the below C++ Program Called \" countWo
ID: 3797337 • Letter: P
Question
PLEASE WRITE C++ PROGRAM
you will rework the below C++ Program Called "countWord_Program.cpp" (Located in the last part of this page) to employ an object in an application. It constructs the same word list as was done in Project below ; one object for each unique word, with multiplicity.
Each word will now be represented in a full-blown object, replacing the data only structs from Project below:
Using the suggested name WordRec, here are the specs:
WordRec: Data attributes: word or token (string) and number of appearances (int)
Implement these member functions in the Word.Rec class to perform the actions given (operators noted):
- Constructor initializes word to " " and counter to 0.
- Set and get for each data member.
- Increment the counter (++; both pre and post).
- Return whether a WordRec object's word attribute is alphanumerically less than that of another Word.Rec object passed in (operator <)
- Return whether a WordRec object's word attribute is equal to that of another Word.Rec object passed in (operator ==)
Add the following non-member associated operators to the WordRec class' files (these are NOT friends):
- Print a WordRec object's data attributes in a well-formatted manner (operator <<)
- Read a string from an ifstream into a WordRec object (and set its counter to l) (operator >>)
This C++ class will be stored in files Word.Rec.h and Word.Rec.cpp.
Your application, to be named p2.cpp, will repeat the Project below task of reading the data into an array of 100 WordRec objects, and then sorting it while processing duplicates (still a combined operation). Additionally. it will now permit the user to request output of the data in several modes, chosen via a menu matching the one in the box.
Choice:
A)ll Words with Number of Appearances
P)rint Words That Appeared a Specified t Times S)how first N Characters of All Words
F)ind a Word
Q)uit
Functions :
- Open the file
-- Same as Project l's openFile ( )
- Read the file into the array of words
-- In a loop, call the >> operator to input the data (you can update the inputWords function from Project below).
- Sort/Remove duplicates.
-- Process User RequestsIn a loop prompt and input the user's choice. You must use the character choices (case insensitive) in the menu in the box. If the user enters:
-- 'A', print all words and their multiplicities, well formatted in a loop using the << operator you wrote for the WordRec class; you should be able to condense and convert the print function from project 1 for this purpose.
-- 'P', input an int and call a function (also in the application file) that prints qualifying words.
-- 'S' is the same as 'A', excpt you are printing all words after applying the substr() function of the string class. This won't be a << overload.
-- 'F',perform the search in yet another function in the application, reporting the result.
-- 'Q', terminate the program.
Notes:
»- Your main () will be mostly function calls. Each task must be in its own module (function). This includes handling the user's choice in the application. This will be a point of emphasis in grading.
»- Proper object-oriented design is a must. Implement your WordRec class properly, using sets and gets to access the data, even .in other member functions.
»- The sort will no longer directly compare word members. It must use the overloaded < operator to compare entire WordRec objects by their words.
»- Similarly, find will make use of == (const WordRec &,const string &) from above.
You should be able to reuse functions from Project 1, likely with_ modification to access data.
»- Your prompts must be descriptive.
»- Input of menu choices must be case insensitive. Letter grade penalty for non-compliance,
»- Your output must be well labeled and easy to read. It must have category header(s) when user input results in output of tabular data. When a user specifies a multiplicity, you should not print how many times each word appeared (it will be the same for all of them; put that info in the header). If you print all the words, the output must line up. No category headers should be printed if no data is output; print a 'not found' message.
»- To show the first N characters of a string requires using substr ( ).
»- . All << operators must take a constant reference to the object. Note that >> operators can't have a constant reference; instead, they have a reference.
»- A makefile is provided for you below under the name Makefile.txt Don 't forget to call it with • /p2 .
»- Word.Rec.h is provided for you below under the name Word.Rec.h . Do NOT add any functions to this file.
»- To make sure the program is working , I will type make to compile the file, and p2 to run it Make sure that it will run on acad under these conditions.
»- The program must be well written, properly indented, and commented.
----------------------------------------------------------------------------------------------------------
Makefile.txt :
------------------------------------------------------------------------------------------
Word.Rec.h :
------------------------------------------------------------------------------------------------------------
countWord_Program.cpp :
#include <iostream>
#include <fstream>
#include <string.h>
#include <iomanip>
#include <ctype.h>
#include <sstream>
using namespace std;
#define CAPACITY 100
/**
* Struct for holding the word and the count
*/
struct words
{
string word;
int count;
};
/**
* function to get the file name from the user and then open the file
*/
bool openFile(ifstream &infile)
{
string inputFile;
cout<<"Enter the input file name: ";
cin>>inputFile;
infile.open(inputFile.c_str());
if(!infile.good())
{
cout<<"Either the file is not available or is locked by another program ";
return false;
}
else return true;
}
/**
* increments the counter for the word by 1
*/
void increment(struct words &word)
{
word.count++;
}
/**
* swaps two structs of words
*/
void swap(struct words &word1, struct words &word2)
{
struct words temp = word1;
word1 = word2;
word2 = temp;
}
/**
* sorts the words in ASCII order
*/
void sort(struct words wordArray[], int &size)
{
for(int m=0; m<size; m++)
{
for(int n=0; n<size; n++)
{
if(wordArray[m].word.compare(wordArray[n].word) < 0)
{
swap(wordArray[m], wordArray[n]);
}
}
}
}
/**
* checks the array for duplicate words
*/
bool isDuplicate(struct words wordsArray[], int size, string word)
{
for(int i=0; i<size; i++)
{
if(wordsArray[i].word == word)
{
increment(wordsArray[i]);
return true;
}
}
return false;
}
/*
*processes the file and adds words into the array, if already available then increases the count of the word
*/
void process(struct words wordsArray[], ifstream &infile)
{
string line;
int counter = 0;
bool canBeAdded = true;
while(getline(infile,line ))
{
istringstream iss(line);
string word;
while(true)
{
if(!(iss>>word))
{
break;
}
if(!isDuplicate(wordsArray, counter, word) && canBeAdded)
{
wordsArray[counter].word = word;
wordsArray[counter].count = 1;
counter++;
}
if(counter==CAPACITY)
{
sort(wordsArray, counter);
canBeAdded = false;
}
}
if(canBeAdded)
sort(wordsArray, counter);
}
}
/**
* prints the array to console
*/
void printResult(struct words wordsArray[])
{
cout<<" Word "<<"Count "<<" ";
for(int i=0; i<CAPACITY; i++)
{
if(wordsArray[i].count != 0)
{
cout<<fixed<<setprecision(2);
string word = wordsArray[i].word;
while(word.length()<25)
word+= " ";
cout<<word<<(wordsArray[i].count)<<endl;
}
}
}
/**
* main function
*/
int main()
{
ifstream in;
struct words wordsArray[CAPACITY];
openFile(in);
process(wordsArray, in);
printResult(wordsArray);
in.close();
}
--------
Thank you
Explanation / Answer
#include <stdio.h>
#include <string.h>
void main()
{
char s[100];
int count = 0, i;
printf("enter the string ");
scanf("%[^ ]s", s);
for (i = 0;s[i] != '';i++)
{
if (s[i] == ' ')
count++;
}
printf("number of words in given string are: %d ", count + 1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.