In: Computer Science
*****MUST ONLY USE******
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
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; }
# Only word sorting not implemented
#include<iostream>
#include<fstream>
using namespace std;
int len = 0;
int lookup(string key , string words[]){
for (int i = 0 ; i < len ; i++){
if(key == words[i])
return i;
}
return -1;
}
int del(string key , string words[]){
int a = lookup(key , words);
if (a == -1)
return 0;
for(int i = a ; i < len - 1 ; i++)
words[i] = words[i+1];
len--;
return 1;
}
int insert(string key , string words[]){
int a = lookup(key , words);
if( a == -1 ){
if(len == 0)
words[len] = key;
else
words[len+1] = key;
len++;
return 1;
}
return 0;
}
int main(){
char choice = 'a';
string key;
string words[100];
fstream f;
f.open("words.txt",ios::out);
int i = 0;
while(!f.eof() )
getline(f , words[i++]);
f.close();
len = i;
do{
cout << "1.Lookup" << endl;
cout << "2.Insert" << endl;
cout << "3.Delete" << endl;
cout << "q.Quit" << endl;
cout << "> " ;
cin >> choice;
if(choice == '1'){
cout << "> ";
cin >> key;
if(lookup(key , words) != -1)
cout << "> "<< key << " Found" << endl;
else
cout << "> " << key << " Not Found" << endl;
}
else if(choice == '2'){
cout << "> ";
cin >> key;
if(insert(key , words))
cout << "> " << key << " Inserted" << endl;
else
cout << "> " << key << " Already present" << endl;
}
else if(choice == '3'){
cout << "> ";
cin >> key;
if(del(key, words))
cout << "> "<< key << " Deleted" << endl;
else
cout << "> "<< key << " Not found" << endl;
}
else if(choice == 'q'){
cout << "> "<< "Bye" << endl;
ofstream f;
f.open("words.txt");
for(int i = 0 ; i < len ; i++){
cout << words[i] << endl;
f << words[i] << endl;
}
f.close();
}
else
cout << "Wrong Choice" << endl;
}while(choice != 'q');
return 0;
}