create an inheritance class to output the synonym and antonym along with meaning when the client inputs the word in the dictionary. Write the C++ code and algorithm of the problem (Marks 30)
Use the following .txt files
Synonym.txt
Word: foo; Synonym: A two o s’ file
Word: fooo; Synonym: A three o s’ file
Word: foooo; Synonym: A three o s’ file
Word: fooooo; Synonym: A four o s’ file
Word: foooooo; Synonym: A five o s’ file
Word: fooooooo; Synonym: A six o s’ file
Antonym.txt
Word: foo; Antonym: A short file
Word: fooo; Antonym: A short short file
Word: foooo; Antonym: A short short short file
Word: fooooo; Antonym: A short short short short file
Word: foooooo; Antonym: A short short short short short file
Word: fooooooo; Antonym: A short short short short short short file
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
string trimData(string s){
string res = "";
bool start = false;
for(int i=0;i<s.length();i++){
if(s[i]!=' '){
start =
true;
}
if(start){
res +=
s[i];
}
}
return res;
}
int main( ){
ifstream infile;
string line;//for read line
infile.open ("Synonym.txt"); //name of file here. plz
mention Complete path if file is not at root
string Synonym[100];
string word[100];
int synonymCounter =0;
if (infile.is_open()) //if file opened
{
while( getline(infile, line,'\n') )
{ //get row from text file
stringstream
ss(line); //stream to other variable
int
wordInd=0;
while(getline(ss, line, ';')){ //row delimeter by space
stringstream sss(line);
int ind =0;
while(getline(sss, line, ':')){ //row delimeter
by space
if(ind==1){
if(wordInd==0){
word[synonymCounter] = trimData(line);
}else{
Synonym[synonymCounter] = trimData(line);
synonymCounter++;
}
wordInd++;
}
ind++;
}
}
}
infile.close(); //close file
cout<<"Loaded
"<<synonymCounter<<" synonym"<<endl;
}
else //if file not found show the below message
{
cout << "Sorry, we could not
find the file." << endl;
return 0;
}
infile.open ("Antonym.txt"); //name of file here.
plz mention Complete path if file is not at root
string Antonym[100];
string word2[100];
int AntonymCounter =0;
if (infile.is_open()) //if file opened
{
while( getline(infile, line,'\n') )
{ //get row from text file
stringstream
ss(line); //stream to other variable
int
wordInd=0;
while(getline(ss, line, ';')){ //row delimeter by space
stringstream sss(line);
int ind =0;
while(getline(sss, line, ':')){ //row delimeter
by space
if(ind==1){
if(wordInd==0){
word2[AntonymCounter] = trimData(line);
}else{
Antonym[AntonymCounter] = trimData(line);
AntonymCounter++;
}
wordInd++;
}
ind++;
}
}
}
infile.close(); //close file
cout<<"Loaded
"<<AntonymCounter<<" Antonym"<<endl;
}
else //if file not found show the below message
{
cout << "Sorry, we could not
find the file." << endl;
return 0;
}
cout<<"Enter a word to find it's synonym and
antonym : ";
string findMe;
cin>>findMe;
for(int i=0;i<synonymCounter;i++){
if(word[i]==findMe){
cout<<"Synonym of "<<findMe<<" is :
"<<Synonym[i]<<endl;
}
}
for(int i=0;i<AntonymCounter;i++){
if(word2[i]==findMe){
cout<<"Antonym of "<<findMe<<" is :
"<<Antonym[i]<<endl;
}
}
}
Synonym.txt
Word: foo; Synonym: A two o s’ file
Word: fooo; Synonym: A three o s’ file
Word: foooo; Synonym: A three o s’ file
Word: fooooo; Synonym: A four o s’ file
Word: foooooo; Synonym: A five o s’ file
Word: fooooooo; Synonym: A six o s’ file
Antonym.txt
Word: foo; Antonym: A short file
Word: fooo; Antonym: A short short file
Word: foooo; Antonym: A short short short file
Word: fooooo; Antonym: A short short short short file
Word: foooooo; Antonym: A short short short short short file
Word: fooooooo; Antonym: A short short short short short short
file
Get Answers For Free
Most questions answered within 1 hours.