Ok, i\'ve done the correction.If the sign is not there, I\'m pretty sure that I\
ID: 3636478 • Letter: O
Question
Ok, i've done the correction.If the sign is not there, I'm pretty sure that I've already correct it.But when paste it here, it disappeared.Basically this program reads a text file that contains phone list, user will have 3 options, to insert new name and phones, to search existing name, to remove existing name and phone number and to print the phone list into an AVL tree. below are the codes and contents of text file:#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
ret = fclose( f );
struct AVLNode
{
string Name;
int Balance;
AVLNode *LeftChild;
AVLNode *RightChild;
};
AVLNode *InputData(fstream &);
AVLNode *InsertData(AVLNode *, AVLNode *);
int FindBalance(AVLNode *);
void Traversal(AVLNode *);
string find(string &);
AVLNode *LeftLeft(AVLNode *);
AVLNode *RightRight(AVLNode *);
AVLNode *LeftRight(AVLNode *);
AVLNode *RightLeft(AVLNode *);
int main()
{
int choice;
string yes_no;
//ifstream CheckFile; //ifstream checks for file existence
fstream CheckFile;
char inputFileName[]="phone.txt";
CheckFile.open(inputFileName); //attempts to open read file, and tests for existence
/*if(!CheckFile)
cout << "Input file does not exist." << endl << endl;
else
cout <<"file open"<<endl;*/
AVLNode *head = InputData(CheckFile);
cout << "travesal: ";
Traversal(head);
cout<<" Do you want to continue?[Yes or No]: ";
cin>>yes_no;
while(yes_no=="Yes"||yes_no=="yes")
{
cout<<"==========Menu==========";
cout<<" 1. Insert new name 2. Search existing name 3. Remove existing name 4. Print Tree ";
cout<<" Please enter your choice [1,2,3,4]: ";
cin>>choice;
switch(choice)
{
case 1:
{
string name, phone;
cout<<" enter Name : ";
cin>>name;
cout<<"enter Phone : ";
cin>>phone;
ofstream myfile;
myfile.open ("phone.txt", ios::app);
if(!myfile)
cout << "Input file does not exist." << endl << endl;
else
cout <<""<<endl;
myfile <<" "<<name<<" "<<phone ;
myfile.close();
break;
}
case 2:
{
string searchLine;
string dataToSearch, name, phone;
int offset;
ifstream myfileSearch1 ("phone.txt");
if (myfileSearch1.is_open())
{
while (myfileSearch1.good()) //while(!myfile.eof())
{
getline (myfileSearch1,searchLine);
//cout << searchLine << endl;
}
myfileSearch1.close();
}
cout<<"Enter name that you want to search: ";
//cin>>dataToSearch;
cin>>name;
cout<<"Enter phone number that you want to search: ";
cin>>phone;
dataToSearch = name " " phone;
ifstream myfileSearch2 ("phone.txt");
if(myfileSearch2.is_open())
{
while(!myfileSearch2.eof())
{
getline(myfileSearch2,searchLine);
if ((offset = searchLine.find(dataToSearch, 0)) != string::npos) {
cout << "found '" << dataToSearch << endl;
}
}
myfileSearch2.close();
}
else
cout<<"Unable to open this file."<<endl;
break;
}
case 3:
{
string line;
// open input file
ifstream in("phone.txt");
if( !in.is_open())
{
cout << "Input file failed to open ";
return 1;
}
// now open temp output file
ofstream out("newphone.txt");
// loop to read/write the file. Note that you need to add code here to check
// if you want to write the line
string dataToDelete,name,phone;
cout<<"text to delete : ";
cin>>name;
cin>>phone;
dataToDelete = name " " phone;
//getline(cin,dataToDelete);
cout<<" dataToDelete : "<<dataToDelete;
int a = 0;
bool del=false;;
while( getline(in,line) ) //loop copy
{
//if(line != "I want to delete this line")
//if(line != "bbb")
if (a==0){
//out <<line;
if(line != dataToDelete){
//out << line << " ";
out << line;
del = false;
}
else{
del = true;
}
}
else{
if(del==true && ((a-1)==0) ){
if(line != dataToDelete)
out << line;
}
else{ //del==false
if(line != dataToDelete){
out << " " << line;
}
}
}
a ;
}
in.close();
out.close();
// delete the original file
remove("phone.txt");
if( remove( "phone.txt" ) != 0 )
cout<<"Error deleting file";
else
cout<<"File successfully deleted";
// rename old to new
rename("newphone.txt","phone.txt");
//fstream NCheckFile;
//NCheckFile.open("newphone.txt");
//AVLNode *head = InputData(NCheckFile);
//cout<<"travesal: ";
//cout<<"Printing.. ";
//Traversal(head);
// all done!
break;
}
case 4:
{
//string wish;
CheckFile.open(inputFileName); //attempts to open read file, and tests for existence
//cout<<"Open Latest Phone List? [Yes or No]: ";
//cin>>wish;
//if((wish == "Yes")||(wish == "yes"))
//{
fstream NewCheckFile;
NewCheckFile.open ("phone.txt");
/*if(!NewCheckFile)
cout << "Input file does not exist." << endl << endl;
else
cout <<"file open"<<endl;*/
AVLNode *head = InputData(NewCheckFile);
cout<<"travesal: ";
cout<<"Printing.. ";
Traversal(head);
CheckFile.close();
/*}
else
{
fstream OldCheckFile;
OldCheckFile.open ("phone.txt");
AVLNode *head = InputData(OldCheckFile);
cout<<"travesal: ";
cout<<"Printing.. ";
Traversal(head);
}*/
break;
}
}
cout<<" Do you want to continue?[Yes or No]: ";
cin>>yes_no;
}
system("pause");
}
AVLNode *InputData(fstream &InFile)
{
AVLNode *Root = NULL;
AVLNode *Leaf;
while(!InFile.eof())
{
Leaf = new AVLNode;
getline(InFile, Leaf->Name);
Leaf->Balance = 0;
Leaf->RightChild = Leaf->LeftChild = NULL;
if(Root == NULL)
Root = Leaf;
Root = InsertData(Leaf, Root);
}
return Root;
}
AVLNode *InsertData(AVLNode *Leaf, AVLNode *Root)
{
if(Root == NULL)
return Leaf;
else if(Leaf->Name < Root->Name)
{
Root->LeftChild = InsertData(Leaf, Root->LeftChild);
if(FindBalance(Root->LeftChild) - FindBalance(Root->RightChild) == 2)
{
if(Leaf->Name < Root->LeftChild->Name)
Root = LeftLeft(Root);
else
Root = LeftRight(Root);
}
}
else if(Leaf->Name > Root->Name)
{
Root->RightChild = InsertData(Leaf, Root->RightChild);
if(FindBalance(Root->RightChild) - FindBalance(Root->LeftChild) == 2)
{
if(Leaf->Name > Root->RightChild->Name)
Root = RightRight(Root);
else
Root = RightLeft(Root);
}
}
Root->Balance = max(FindBalance(Root->LeftChild), FindBalance(Root->RightChild)) 1;
return Root;
}
int FindBalance(AVLNode *Root)
{
if(Root == NULL)
return -1;
else
return Root->Balance;
}
AVLNode *LeftLeft(AVLNode *Rotate)
{
AVLNode *Pivot = Rotate->LeftChild;
Rotate->LeftChild = Pivot->RightChild;
Pivot->RightChild = Rotate;
Rotate->Balance = max(FindBalance(Rotate->LeftChild), FindBalance(Rotate->RightChild)) 1;
Pivot->Balance = max(FindBalance(Pivot->LeftChild), FindBalance(Rotate->RightChild)) 1;
return Pivot;
}
AVLNode *RightRight(AVLNode *Rotate)
{
AVLNode *Pivot = Rotate->RightChild;
Rotate->RightChild = Pivot->LeftChild;
Pivot->LeftChild = Rotate;
Rotate->Balance = max(FindBalance(Rotate->LeftChild), FindBalance(Rotate->RightChild)) 1;
Pivot->Balance = max(FindBalance(Pivot->RightChild), FindBalance(Rotate->LeftChild)) 1;
return Pivot;
}
AVLNode *LeftRight(AVLNode *RotateTop)
{
RotateTop->LeftChild = RightRight(RotateTop->LeftChild);
return LeftLeft(RotateTop);
}
AVLNode *RightLeft(AVLNode *RotateTop)
{
RotateTop->RightChild = LeftLeft(RotateTop->RightChild);
return RightRight(RotateTop);
}
void Traversal(AVLNode *Root)
{
AVLNode *temp;
if(Root != NULL)
{
Traversal(Root->LeftChild); // print left subtree
cout << "(" << Root->Name << ", "; // print this node
if(Root->LeftChild == NULL)
cout << "NULL, ";
else
{
temp = Root->LeftChild;
cout << temp->Name << ", ";
}
if(Root->RightChild == NULL)
cout << "NULL, ";
else
{
temp = Root->RightChild;
cout << temp->Name << ", ";
}
int temp1 = (FindBalance(Root->RightChild) - FindBalance(Root->LeftChild));
cout << temp1 << ")" << endl;
Traversal(Root->RightChild); // print right subtree
}
return;
}
Text File
========
DAB 0129384890
YAN 0193849283
OBU 0132748573
GAB 0148264829
AFI 0128729304
SAK 0103248827
DAF 0139349283
BAA 0193729183
MAR 0173628473
DAH 0191872776
SHA 0127362772
JAC 0127482882
IAN 0194920019
YAH 0179277744
KAG 0138274472
EDI 0107284720
SAN 0128472940
SAM 0193628294
ZAB 0127492749
RAC 0196264481
SUE 0129578807
MUN 0129399949
RIZ 0199292999
DAD 0199510085
MOM 0129833707
SIS 0199602791
CIK 0129533860
BDE 0139293999
IBU 0192999437
IBU 0192837747
ZAY 0192838495
ZON 0129482940
ZOM 0129399948
ZOE 0129833707
JON 0129299384
BOB 0192838486
DEY 0129939884
BOM 0129384755
There's a few questions and problem:
1) how can i read a sentence from the text file. for example, if the user wanted to search for BOM phone number, user will type BOM and during display, the program will print out the name together with the phone number.
2)in the switch case, case 3 is to remove name. after create a temp text file, after removal,the content of the original file will be copied to the temp file and the original file will be deleted then, the temp file will be rename as the original file name. But in my case, the original is not deleted and remained in my folder.
what can i do to solve the above problem?
Explanation / Answer
int main() { int choice; string yes_no; //ifstream CheckFile; //ifstream checks for file existence fstream CheckFile; char inputFileName[]="phone.txt"; CheckFile.open(inputFileName); //attempts to open read file, and tests for existence /*if(!CheckFile) coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.