Description
The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20.
The system should support the following three functions:
Please also make sure that
Implementation
Expected Output
Below is a sample run of the program. Assume that the file words.txt is empty initially.
1 for lookup; 2 for insertion; 3 for deletion; q for quit: 2 Please enter the word you want to insert: cat The word "cat" is inserted successfully! 1 for lookup; 2 for insertion; 3 for deletion; q for quit: 2 Please enter the word you want to insert: apple The word "apple" is inserted successfully! 1 for lookup; 2 for insertion; 3 for deletion; q for quit: 2 Please enter the word you want to insert: dog The word "dog" is inserted successfully! 1 for lookup; 2 for insertion; 3 for deletion; q for quit: 1 Please enter the word you want to search for: car The word "car" is not found! 1 for lookup; 2 for insertion; 3 for deletion; q for quit: 1 Please enter the word you want to search for: cat The word "cat" is found! 1 for lookup; 2 for insertion; 3 for deletion; q for quit: 2 Please enter the word you want to insert: cat The word "cat" already exists! 1 for lookup; 2 for insertion; 3 for deletion; q for quit: 3 Please enter the word you want to delete: cat The word "cat" is deleted successfully! 1 for lookup; 2 for insertion; 3 for deletion; q for quit: 3 Please enter the word you want to delete: car The word "car" is not in the file!
Then, below is the expected content of the text file words.txt.
apple dog
Hints
For word insertion and deletion tasks, creating a temporary file may simplify lots of work. This approach is particularly useful if the word bank is large such that you cannot read the whole word bank into memory. For the lab tasks, there are two useful functions for file operation that you may use:
In addition, we read and manipulate words through char array. The following two functions could be helpful:
======================Skeleton Code================================
#include <iostream> #include <fstream> #include <string.h> #include <stdio.h> using namespace std; #define N 20 // search for a word in the input file, return true if found bool lookup(const char filename[], const char word[]){ return false; } // delete a word in the input file // the result should not contain blank lines void delete_word(const char filename[], const char word[]){ } // insert a word in the input file such that the words are in ascending order // it should not insert duplicate word void insert_word(const char filename[], const char word[]){ } int main(){ const char filename[] = "words.txt"; char choice; char word[N]; while (true){ cout << "1 for lookup; 2 for insertion; 3 for deletion; q for quit: "; cin >> choice; if (choice == '1'){ cout << "Please enter the word you want to search for: "; cin >> word; if (lookup(filename, word)){ cout << "The word \"" << word << "\" is found!" << endl; }else{ cout << "The word \"" << word << "\" is not found!" << endl; } }else if (choice == '2'){ cout << "Please enter the word you want to insert: "; cin >> word; insert_word(filename, word); }else if (choice == '3'){ cout << "Please enter the word you want to delete: "; cin >> word; delete_word(filename, word); }else if (choice == 'q'){ break; }else{ cout << "Invalid input. Please input again." << endl; } cout << endl; } return 0; }
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define N 20
// search for a word in the input file, return true if
found
bool lookup(const char filename[], const char word[]){
char word2[20];
ifstream f;
int count=0;
f.open(filename);
while(f>>word2)
{
if(!(strcmp(word2,word)))
count++;
}
if(count>0)
return true;
else
return false;
}
// delete a word in the input file
// the result should not contain blank lines
void delete_word(const char filename[], const char word[]){
char word2[20];
ifstream f;
ofstream f2;
f.open(filename);
f2.open("copy.tmp");
while(f>>word2)
{
if(strcmp(word,word2)!=0)
{
f2<<word2<<endl;
}
}
remove("words.txt");
rename("copy.tmp","words.txt");
}
// insert a word in the input file such that the words are in
ascending order
// it should not insert duplicate word
void insert_word(const char filename[], const char word[]){
char word2[20];
int count=0,flag=0;
ifstream f;
ofstream f2;
f.open(filename);
f2.open("copy.tmp");
while(f>>word2)
{
if(strcmp(word2, word)<0 && flag==0)
{
f2<<word2<<endl;
count++;
}
else if(strcmp(word2, word)>0
&& flag==0 && count==0)
{
f2<<word<<endl<<word2<<endl;
flag=1;
count++;
}
else if(strcmp(word2, word)>0
&& flag==0 && count>0)
{
f2<<word<<endl<<word2<<endl;
flag=1;
count++;
}
else if(strcmp(word2, word)==0
&& flag==0)
{
f2<<word2<<endl<<word<<endl;
flag=1;
count++;
}
else if(flag==1)
{
f2<<word2<<endl;
count++;
}
}
if(flag==0)
{
f2<<word<<endl;
flag=1;
count++;
}
remove("words.txt");
rename("copy.tmp","words.txt");
}
int main(){
const char filename[] = "words.txt";
char choice;
char word[N];
while (true){
cout << "1 for lookup; 2 for insertion; 3 for deletion; q for
quit: ";
cin >> choice;
if (choice == '1'){
cout << "Please enter the word you want to search for:
";
cin >> word;
if (lookup(filename, word)){
cout << "The word \"" << word << "\" is found!"
<< endl;
}else{
cout << "The word \"" << word << "\" is not
found!" << endl;
}
}else if (choice == '2'){
cout << "Please enter the word you want to insert: ";
cin >> word;
insert_word(filename, word);
}else if (choice == '3'){
cout << "Please enter the word you want to delete: ";
cin >> word;
delete_word(filename, word);
}else if (choice == 'q'){
break;
}else{
cout << "Invalid input. Please input again." <<
endl;
}
cout << endl;
}
return 0;
}
OUTPUT
1 for lookup; 2 for insertion; 3 for deletion; q for quit:
1
Please enter the word you want to search for: happy
The word "happy" is found!
1 for lookup; 2 for insertion; 3 for deletion; q for quit:
2
Please enter the word you want to insert: working
1 for lookup; 2 for insertion; 3 for deletion; q for quit:
3
Please enter the word you want to delete: happy
1 for lookup; 2 for insertion; 3 for deletion; q for quit:
1
Please enter the word you want to search for: happy
The word "happy" is not found!
1 for lookup; 2 for insertion; 3 for deletion; q for quit:
1
Please enter the word you want to search for: working
The word "working" is found!
1 for lookup; 2 for insertion; 3 for deletion; q for quit: q
Get Answers For Free
Most questions answered within 1 hours.