无法从保存的文件正确创建树 [英] Tree is not created Correctly from saved file
本文介绍了无法从保存的文件正确创建树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用对象I/O在文件上编写程序时,它保存在文件(硬盘)上,但是当我读取整个树时,如果有人对此有任何想法,它不会显示正确的数据,请在此帮助我. ..
//Asign3(Q1BST).cpp:定义控制台应用程序的入口点.
//
When I write Program on File Using Object I/O It saves on file (Hard Disk) but when I read this whole Tree It doesn''t show correct data if Any one Have idea about this Please Help me in this...
// Asign3(Q1BST).cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<conio.h>
using namespace std;
struct node
{
string pname,fname,age,adrs,diseas,dname;
int nic,pid;
node*lc,*rc;
node()
{
lc=0;
rc=0;
}
};
class BST
{
private:
/*string pname,fname,age,adrs,diseas,dname;
int nic,pid;*/
node*root;
char ch;//this is for remove function geting choice NIC/PID
int key;//for searching record
int not,found;//not stands for No Of Traversal
void set(node*t,BST obj)
{
/*t->adrs=obj.adrs;
t->age=obj.age;
t->diseas=obj.diseas;
t->dname=obj.dname;
t->fname=obj.fname;
t->nic=obj.nic;
t->pid=obj.pid;
t->pname=obj.pname;*/
}
void get(node*t)
{
//cout<<"Enter Patient Name ";cin>>t->pname;
//cout<<"\n Enter Father Name ";cin>>t->fname;
//cout<<"\nEnter Age ";cin>>t->age;
cout<<"\nEnter NIC ";
cin>>t->nic;
//cout<<"\nEnter Address ";cin>>t->adrs;
//cout<<"\nEnter Disease ";cin>>t->diseas;
//cout<<"\nEnter Doctor Name ";
//cin>>t->dname;
//cout<<"\nEnter Patient ID ";
//cin>>t->pid;
}
void show(node*t)
{
/*cout<<"\nPatient Name "<<t->pname;
cout<<"\nFather Name "<<t->fname;
cout<<"\nAge "<<t->age;*/
cout<<"\nNIC "<<t->nic;
/*cout<<"\nAddress "<<t->adrs;
cout<<"\nDisease "<<t->diseas;
cout<<"\nDoctor Name "<<t->dname;*/
//cout<<"\nPatient ID "<<t->pid<<endl;
}
void asending(node*t)
{
if(t->lc!=0)
asending(t->lc);
show(t);
if(t->rc!=0)
asending(t->rc);
}
void desending(node*t)
{
if(t->rc!=0)
desending(t->rc);
show(t);
if(t->lc!=0)
desending(t->lc);
}
void swap(node*p,node*c)//This works for delete by copy
{
p->adrs=c->adrs;
p->age=c->age;
p->diseas=c->diseas;
p->dname=c->dname;
p->fname=c->fname;
p->nic=c->nic;
p->pid=c->pid;
p->pname=c->pname;
}
void deleteall(node*t)
{
if(t->rc!=0)
deleteall(t->rc);
if(t->lc!=0)
deleteall(t->lc);
show(t);
cout<<"\nDeleting Above \n";
delete t;
}
public:
BST()
{
root=0;
not=0;
found=0;
}
~BST()
{
cout<<"Caling Destructor \n";
deleteall(root);
}
void showdata()//well work
{
node*t=root;
show(t);
}
void greatest()//well work//show greatest record
{
node*temp;
temp=root;
while(temp->rc!=0)
temp=temp->rc;
cout<<"Greatest Value Is \n ";
show(temp);
}
void least()//well work/show least record
{
node*temp;
temp=root;
while(temp->lc!=0)
temp=temp->lc;
cout<<"LeastS Value Is \n ";
show(temp);
}
void asendingorder()//wel work//show record in ascending order
{
node*temp=root;
asending(temp);
}
void desendingorder()//well work//show record in decending order
{
node*temp=root;
desending(temp);
}
void searchrecord(int val)//wel work//Search Records
{
found=0;
node*temp=root;
if(val==0)//search wrt nic
{
cout<<"Enter NIC Number ";
cin>>key;
while((temp!=0)&&(found==0))
{
if(temp->nic<key)>
{
temp=temp->rc;
not++;
}
else if(temp->nic>key)
{
temp=temp->lc;
not++;
}
else
{
cout<<"Record Is Found "
<<"Number Of Traversal "<<not<<endl;
not=0;
found=1;
}
}
if(found==0)
cout<<"Record Is Not Found!!!!!!!!!!";
}
else//Search wrt Patient ID
{
cout<<"Enter Patient ID ";
cin>>key;
while((temp!=0)&&(found==0))
{
if(temp->pid<key)>
{
temp=temp->rc;
not++;
}
else if(temp->pid>key)
{
temp=temp->lc;
not++;
}
else
{
cout<<"Record Is Found "
<<"Number Of Traversal "<<not<<endl;
not=0;
found=1;
}
}
if(found==0)
cout<<"Record Is Not Found!!!!!!!!!!";
}
}
void deleteallnode()//well work//Delete Complete Tree Including Root
{
node*temp=root;
deleteall(temp);
}
void remove(int val)//well work//Delete any Record 0 for merge 1 for copy
{
node*troot,*par;
troot=root;
par=root;
found=0;
cout<<"Enter 'N'for delete Record By NIC or 'P' delete BY P-ID ";
cin>>ch;
tolower(ch);
if(val ==0)//Delete By merge
{
if(ch=='n')//delet By NIC
{
cout<<"Enter NIC ";
cin>>key;
if(key==root->nic)//If Value is Root
{
if(troot->rc!=0)
{
troot=troot->rc;
par=troot;
while(troot->lc!=0)
{
par=troot;
troot=troot->lc;
}
par->lc=root->lc;
par=root;
troot=root->rc;
root=troot;
delete par;
}
else
root=troot->lc;
}
else
{
while((troot!=0)&&(found==0))
{
if(troot->nic!=key)
par=troot;
if(troot->nic>key)
troot=troot->lc;
else if(troot->nic<key)>
troot=troot->rc;
else
found=1;
}
if(found==1)
{
if(troot->rc==0)
{
if(par->nic>key)
par->lc=troot->lc;
else
par->rc=troot->lc;
}
else if(troot->lc==0)
{
if(par->nic>key)
par->lc=troot->rc;
else
par->rc=troot->rc;
}
else
{
node*temp;
temp=troot;
temp=temp->rc;
while(temp->lc!=0)
temp=temp->lc;
temp->lc=troot->lc;
if(par->nic>key)
par->lc=troot->rc;
else
par->rc=troot->rc;
}
delete troot;
}
else
{
cout<<"Value Not Exist In file";
found=0;
}
}
}
else//delete By PID with delete By merge
{
cout<<"Enter Patient ID ";
cin>>key;
if(key==root->pid)//If Value is Root
{
if(troot->rc!=0)
{
troot=troot->rc;
par=troot;
while(troot->lc!=0)
{
par=troot;
troot=troot->lc;
}
par->lc=root->lc;
par=root;
troot=root->rc;
root=troot;
delete par;
}
else
root=troot->lc;
}
else
{
while((troot!=0)&&(found==0))
{
if(troot->pid!=key)
par=troot;
if(troot->pid>key)
troot=troot->lc;
else if(troot->pid<key)>
troot=troot->rc;
else
found=1;
}
if(found==1)
{
if(troot->rc==0)
{
if(par->pid>key)
par->lc=troot->lc;
else
par->rc=troot->lc;
}
else if(troot->lc==0)
{
if(par->pid>key)
par->lc=troot->rc;
else
par->rc=troot->rc;
}
else
{
node*temp;
temp=troot;
temp=temp->rc;
while(temp->lc!=0)
temp=temp->lc;
temp->lc=troot->lc;
if(par->pid>key)
par->lc=troot->rc;
else
par->rc=troot->rc;
}
delete troot;
}
else
{
cout<<"Value Not Exist In file";
found=0;
}
}
}
}
else//delete by Copy
{
if(ch=='n')//delet By NIC
{
cout<<"Enter NIC ";
cin>>key;
if(key==root->pid)//If Value is Root
{
if(troot->rc!=0)
{
troot=troot->rc;
par=troot;
while(troot->lc!=0)
{
par=troot;
troot=troot->lc;
}
par->lc=root->lc;
par=root;
troot=root->rc;
root=troot;
delete par;
}
else
root=troot->lc;
}
else
{
while((troot!=0)&&(found==0))
{
if(troot->nic!=key)
par=troot;
if(troot->nic>key)
troot=troot->lc;
else if(troot->nic<key)>
troot=troot->rc;
else
found=1;
}
if(found==1)
{
node*tpar;
tpar=troot;
if(tpar->rc!=0)
{
par=tpar;
tpar=tpar->rc;
while(tpar->lc!=0)
{
par=tpar;
tpar=tpar->lc;
}
swap(troot,tpar);
//replace data troot with tpar
par->lc=0;
delete tpar;
}
else
{
if(par->nic>key)
par->lc=troot->lc;
else
par->rc=troot->lc;
delete troot;
}
}
else
{
cout<<"Value Not Exist In file";
found=0;
}
}
}
else//delete By PID
{
cout<<"Enter Patient-ID ";
cin>>key;
if(key==root->pid)//If Value is Root
{
if(troot->rc!=0)
{
troot=troot->rc;
par=troot;
while(troot->lc!=0)
{
par=troot;
troot=troot->lc;
}
par->lc=root->lc;
par=root;
troot=root->rc;
root=troot;
delete par;
}
else
root=troot->lc;
}
else
{
while((troot!=0)&&(found==0))
{
if(troot->pid!=key)
par=troot;
if(troot->pid>key)
troot=troot->lc;
else if(troot->pid<key)>
troot=troot->rc;
else
found=1;
}
if(found==1)
{
node*tpar;
tpar=troot;
if(tpar->rc!=0)
{
par=tpar;
tpar=tpar->rc;
while(tpar->lc!=0)
{
par=tpar;
tpar=tpar->lc;
}
swap(troot,tpar);
//replace data troot with tpar
par->lc=0;
delete tpar;
}
else
{
if(par->pid>key)
par->lc=troot->lc;
else
par->rc=troot->lc;
delete troot;
}
}
else
{
cout<<"Value Not Exist In file";
found=0;
}
}
}
}
}
void add()//well work //Add new record
{
if(root!=0)
{
node*temp,*troot,*par;
troot=root;
par=troot;
temp=new node;
get(temp);
found=0;
cout<<"Enter 'N'for Add Record By NIC or 'P' Add BY P-ID ";
cin>>ch;
tolower(ch);
if(ch=='n')
{
while((troot!=0)&&(found==0))
{
par=troot;
if(troot->nic>temp->nic)
troot=troot->lc;
else if(troot->nic<temp->nic)
troot=troot->rc;
else
{
cout<<"%%%%%%%%%%This NIC Already Exist%%%%%%%%%%%\n";
found=1;
}
}
if(found==0)
{
if(par->nic>temp->nic)
par->lc=temp;
else
par->rc=temp;
}
else
{
found=0;
}
}
else//adding wrt pid
{
while((troot!=0)&&(found==0))
{
par=troot;
if(troot->pid>temp->pid)
troot=troot->lc;
else if(troot->pid<temp->pid)
troot=troot->rc;
else
{
cout<<"This NIC Already Exist";
found=1;
}
}
if(found==0)
{
if(par->pid>temp->pid)
par->lc=temp;
else
par->rc=temp;
}
else
{
found=0;
}
}
}
else
{
node*temp=new node();
get(temp);
root=temp;
}
}
void getdat(int uch)//well work//uch 0 ==nic stands for Userchoice for NIC/PID 0 fro patient-ID 1 for nic
{
node*temp=new node;
node*troot=root;
node*parent;
get(temp);
if(root==0)
{
root=temp;
}
else
{
if(uch==0)//NIC
{
while((troot!=0)&&found==0)
{
parent=troot;
if(troot->nic>temp->nic)
troot=troot->lc;
else if(troot->nic<temp->nic)
troot=troot->rc;
else
{
cout<<"This NIC Already Exist!!!!! ";
found=1;
}
}
}
else//PID
{
while((troot!=0)&&found==0)
{
parent=troot;
if(troot->pid>temp->pid)
troot=troot->lc;
else if(troot->pid<temp->pid)
troot=troot->rc;
else
{
cout<<"This PID Already Exist!!!!! ";
found=1;
}
}
}//nic if-else
if(found==0)
{
if(uch==0)//nic
{
if(parent->nic>temp->nic)
parent->lc=temp;
else
parent->rc=temp;
}
else
{
if(parent->pid>temp->pid)
parent->lc=temp;
else
parent->rc=temp;
}
}
}//root if-else
found=0;//for next time checking
}
};
int _tmain(int argc, _TCHAR* argv[])
{
BST obj;
char ch;
fstream fs;
int chh;
cout<<"-------------------------------------------------------------------------------"
<<"\t\tHospital Information Using BST Including Filing \n"
<<"-------------------------------------------------------------------------------\n";
//fs.open("D:\\Fall-11\\Asignments\\DS\\3\\data.dat",ios::in|ios::binary);
// fs.seekg(0);
/*while(!fs.eof())
{
fs.read(reinterpret_cast<char*>(&obj),sizeof(BST));
obj.showdata();
}
fs.close();*/
int i=1;
up:
cout<<"Press 'N' for building BST wrt NIC ---'P' wrt P-ID";
cin>>ch;
tolower(ch);
if(ch=='n')
chh=0;
else if(ch=='p')
chh=1;
else
{
cout<<"InValid Choice";
goto up;
}
while(i)
{
obj.getdat(chh);
cout<<"If You Want To Stop Building BST Press 0";
cin>>i;
system("cls");
}
start:
cout<<"Enter Choice to show record\n "
<<"'g' to show greatest key \n"
<<"'l' for least key \n"
<<"'a'for acending order\n "
<<"'d'for decending order\n "
<<"'r' for delete Record \n"
<<"'n'for adding new \n"
<<"'s'for searching Record\n "
<<"'x' for delete all records\n"
<<"'z'To Close This menu\n"
<<"'c'To cleane Screen";
cin>>ch;
tolower(ch);
if(tolower(ch)=='g')
{
obj.greatest();//show greatest record
}
else if(tolower(ch)=='l')
{
obj.least();//show least record
}
else if (ch=='a')
{
obj.asendingorder();//show record in acending order
}
else if(ch=='d')
{
obj.desendingorder();//show record in decending order
}
else if(ch=='r')//delete node (single record)
{
upp:
cout<<"Press 'M'to delete By Merge or 'C'to delete By Copy";
cin>>ch;
if(tolower(ch)=='m')//By Merge
chh=0;
else if(tolower(ch)=='c')//By Copy
{
chh=1;
}
else
{
cout<<"Invalid Search Choice ";
goto upp;
}
obj.remove(chh);
}
else if(ch=='n')//add new record
obj.add();
else if(ch=='s')//searching record
{
pp:
cout<<"Press 'n' search wrt nic or 'p' wrt Patient ID";
cin>>ch;
if(tolower(ch)=='n')
chh=0;
else if(tolower(ch)=='p')
{
chh=1;
}
else
{
cout<<"Invalid Search Choice ";
goto pp;
}
obj.searchrecord(chh);
}
else if(ch=='x')//delete whole tree
{
obj.deleteallnode();
}
else if (ch=='z')
goto out;
else if(ch=='c')
system("cls");
else
{
cout<<"Invalid Choice ";
goto start;
}
goto start;
out:
return 0;
}
推荐答案
The Above Code is not running in VC++ of VS2008
Where we save data on file?
The Above Code is not running in VC++ of VS2008
Where we save data on file?
这篇关于无法从保存的文件正确创建树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文