C++ You are not allowed to use #include<map> and #include<algorithm> All variabl
ID: 3555524 • Letter: C
Question
C++
You are not allowed to use #include<map> and #include<algorithm>
All variable must be documented . Please!!!!
You can create many function as you want
You should create a function, accept vector as argument . ask user to type in the address of the text file. read a txt file into vector. Then count and display how many words in the txt. Please note. Do not use array. Because I have a very very big text file.
You will write a program that produces a lexicographical (alphabetical) listing of all distinct words in a text file of unspecified size. The name of the text file will be passed to your program as a command line argument. If no command line argument is provided or the file cannot be opened, your program will print a succinct message to the user and terminate. Otherwise, your program will read and count each distinct word in the text file, ignoring case. For example, given the demonstration input file: "It was the best of times, it was the worst of times." your program will generate the following output in lexicographic order: best 1 it 2 of 2 the 2 was 2 worst 1 Words are defined as groups of characters separated by whitespace. Trim only trailing punctuation characters (i.e. contractions like "you're" are valid words).Explanation / Answer
#include<iostream>
#include<cstring>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
struct word_t{
string word;
unsigned int count;
};
int main()
{
char filename[100];
cout<<"Give the path of the test file"<<endl;
cin>>filename;
string line;
vector<string> words;
vector<word_t*> dist_words;
char* word;
int n;
char a;
ifstream myfile (filename); //Opening the file
if (myfile.is_open())
{
while (myfile>>line) //Reading the file word by word as separated by spaces
{
word = (char *)line.c_str();
n = line.size();
a = word[n-1];
//Removing Punctuation Marks
if (a==',' or a=='?' or a=='.' or a=='!' or a=='-' or a=='\' or a=='/' or a==';')
word[n-1] = '';
//Uppercase to lowercase
for (int i=0; word[i]!=''; i++){
if (word[i] >= 65 and word[i] <= 90)
word[i] = word[i] + 32;
}
words.push_back(line); //Pushing the words into the vector after the removal of punctuation and uppercases.
}
myfile.close();
cout<<"Number of words in the text file: "<<words.size()<<endl;
/* Removing duplicates and forming a new vector 'dist_words' with only distinct words */
word_t* nword;
int j;
for (int i=0; i<words.size(); i++)
{
for(j=0; j<dist_words.size(); j++)
{
if(strcmp((char*) dist_words[j]->word.c_str(),(char*) words[i].c_str())==0){
dist_words[j]->count++;
break;
}
}
if (j==dist_words.size())
{
nword = new word_t;
nword->word = words[i];
nword->count = 1;
dist_words.push_back(nword);
}
}
cout<<"Number of distinct words in the text file: "<<dist_words.size()<<endl;
cout<<endl;
cout<<"Printing all the distinct words:"<<endl;
for(int i=0; i<dist_words.size(); i++)
{
cout<<dist_words[i]->word<<" "<<dist_words[i]->count<<endl;
}
int l;
word_t* temp;
/* Sorting the 'dist_words' words in alphabetical order of the words */
for (int i=0; i<dist_words.size()-1; i++)
{
l=i;
for (int j=i+1;j<dist_words.size(); j++)
{
if (strcmp((char*) dist_words[l]->word.c_str(),(char*) dist_words[j]->word.c_str()) > 0)
{
l=j;
}
}
temp = dist_words[i];
dist_words[i] = dist_words[l];
dist_words[l] = temp;
}
/* Printing the words in alphabetical order */
cout<<endl;
cout<<"Printing in alphabetical order:"<<endl;
for(int i=0; i<dist_words.size(); i++)
{
cout<<dist_words[i]->word<<" "<<dist_words[i]->count<<endl;
}
/* Printing the words in the order of frequencies */
cout<<endl;
cout<<"Printing in order of frequencies:"<<endl;
bool* isDone = new bool[dist_words.size()];
for (int i=0; i<dist_words.size(); i++)
{
isDone[i] = false;
}
int i=0;
while(i<dist_words.size())
{
l=i;
if (isDone[i]==true)
{
i++;
continue;
}
else{
for (int j=i+1;j<dist_words.size(); j++)
{
if (isDone[j]==false and dist_words[l]->count < dist_words[j]->count)
{
l=j;
}
}
cout<<dist_words[l]->word<<" "<<dist_words[l]->count<<endl;
isDone[l]=true;
}
}
}
else cout << "Unable to open file"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.