无法从保存的文件正确创建树 [英] Tree is not created Correctly from saved file

查看:63
本文介绍了无法从保存的文件正确创建树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用对象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-&gt;adrs=obj.adrs;
		t-&gt;age=obj.age;
		t-&gt;diseas=obj.diseas;
		t-&gt;dname=obj.dname;
		t-&gt;fname=obj.fname;
		t-&gt;nic=obj.nic;
		t-&gt;pid=obj.pid;
		t-&gt;pname=obj.pname;*/
	}
	void get(node*t)
	{
		//cout&lt;&lt;"Enter Patient Name ";cin&gt;&gt;t-&gt;pname;
		//cout&lt;&lt;"\n Enter Father Name ";cin&gt;&gt;t-&gt;fname;
		//cout&lt;&lt;"\nEnter Age ";cin&gt;&gt;t-&gt;age;
		cout&lt;&lt;"\nEnter NIC ";
		cin&gt;&gt;t-&gt;nic;
		//cout&lt;&lt;"\nEnter Address ";cin&gt;&gt;t-&gt;adrs;
		//cout&lt;&lt;"\nEnter Disease ";cin&gt;&gt;t-&gt;diseas;
		//cout&lt;&lt;"\nEnter Doctor Name ";
		//cin&gt;&gt;t-&gt;dname;
		//cout&lt;&lt;"\nEnter Patient ID ";
		//cin&gt;&gt;t-&gt;pid;
	}
	void show(node*t)
	{
		/*cout&lt;&lt;"\nPatient Name "&lt;&lt;t-&gt;pname;
		cout&lt;&lt;"\nFather Name "&lt;&lt;t-&gt;fname;
		cout&lt;&lt;"\nAge "&lt;&lt;t-&gt;age;*/
		cout&lt;&lt;"\nNIC "&lt;&lt;t-&gt;nic;
		/*cout&lt;&lt;"\nAddress "&lt;&lt;t-&gt;adrs;
		cout&lt;&lt;"\nDisease "&lt;&lt;t-&gt;diseas;
		cout&lt;&lt;"\nDoctor Name "&lt;&lt;t-&gt;dname;*/
		//cout&lt;&lt;"\nPatient ID "&lt;&lt;t-&gt;pid&lt;&lt;endl;
	}
	void asending(node*t)
	{
		if(t-&gt;lc!=0)
			asending(t-&gt;lc);
		show(t);
		if(t-&gt;rc!=0)
			asending(t-&gt;rc);
	}
	void desending(node*t)
	{
		if(t-&gt;rc!=0)
			desending(t-&gt;rc);
		show(t);
		if(t-&gt;lc!=0)
			desending(t-&gt;lc);
	}
	void swap(node*p,node*c)//This works for delete by copy
	{
		p-&gt;adrs=c-&gt;adrs;
		p-&gt;age=c-&gt;age;
		p-&gt;diseas=c-&gt;diseas;
		p-&gt;dname=c-&gt;dname;
		p-&gt;fname=c-&gt;fname;
		p-&gt;nic=c-&gt;nic;
		p-&gt;pid=c-&gt;pid;
		p-&gt;pname=c-&gt;pname;
	}
	void deleteall(node*t)
	{
		if(t-&gt;rc!=0)
			deleteall(t-&gt;rc);
		if(t-&gt;lc!=0)
			deleteall(t-&gt;lc);
		show(t);
		cout&lt;&lt;"\nDeleting Above  \n";
		delete t;

	}
public:
	BST()
	{
		root=0;
		not=0;
		found=0;
	}
	~BST()
	{
		cout&lt;&lt;"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-&gt;rc!=0)
			temp=temp-&gt;rc;
		cout&lt;&lt;"Greatest Value Is \n ";
		show(temp);
	}
	void least()//well work/show least record
	{
		node*temp;
		temp=root;
		while(temp-&gt;lc!=0)
			temp=temp-&gt;lc;
		cout&lt;&lt;"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&lt;&lt;"Enter NIC Number ";
			cin&gt;&gt;key;
			while((temp!=0)&&(found==0))
			{
				if(temp-&gt;nic&lt;key)&gt;
				{
					temp=temp-&gt;rc;
					not++;
				}
				else if(temp-&gt;nic&gt;key)
				{
					temp=temp-&gt;lc;
					not++;
				}
				else
				{
					cout&lt;&lt;"Record Is Found "
						&lt;&lt;"Number Of Traversal "&lt;&lt;not&lt;&lt;endl;
					not=0;
					found=1;
				}
			}
			if(found==0)
				cout&lt;&lt;"Record Is Not Found!!!!!!!!!!";
		}
		else//Search wrt Patient ID
		{
			cout&lt;&lt;"Enter Patient ID ";
			cin&gt;&gt;key;
			while((temp!=0)&&(found==0))
			{
				if(temp-&gt;pid&lt;key)&gt;
				{
					temp=temp-&gt;rc;
					not++;
				}
				else if(temp-&gt;pid&gt;key)
				{
					temp=temp-&gt;lc;
					not++;
				}
				else
				{
					cout&lt;&lt;"Record Is Found "
						&lt;&lt;"Number Of Traversal "&lt;&lt;not&lt;&lt;endl;
					not=0;
					found=1;
				}
			}
			if(found==0)
				cout&lt;&lt;"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&lt;&lt;"Enter 'N'for delete Record By NIC or 'P' delete BY P-ID ";
		cin&gt;&gt;ch;
		tolower(ch);
		if(val ==0)//Delete By merge
		{
			if(ch=='n')//delet By NIC
			{
				cout&lt;&lt;"Enter NIC ";
				cin&gt;&gt;key;
				if(key==root-&gt;nic)//If Value is Root
				{
					if(troot-&gt;rc!=0)
					{
						troot=troot-&gt;rc;
						par=troot;
						while(troot-&gt;lc!=0)
						{
							par=troot;
							troot=troot-&gt;lc;
						}
						par-&gt;lc=root-&gt;lc;
						par=root;
						troot=root-&gt;rc;
						root=troot;
						delete par;

					}
					else
						root=troot-&gt;lc;
				}
				else
				{
					while((troot!=0)&&(found==0))
					{
						if(troot-&gt;nic!=key)
							par=troot;
						if(troot-&gt;nic&gt;key)
							troot=troot-&gt;lc;
						else if(troot-&gt;nic&lt;key)&gt;
							troot=troot-&gt;rc;
						else 
							found=1;
					}
					if(found==1)
					{
						if(troot-&gt;rc==0)
						{
							if(par-&gt;nic&gt;key)
								par-&gt;lc=troot-&gt;lc;
							else
								par-&gt;rc=troot-&gt;lc;
						}
						else if(troot-&gt;lc==0)
						{
							if(par-&gt;nic&gt;key)
								par-&gt;lc=troot-&gt;rc;
							else
								par-&gt;rc=troot-&gt;rc;
						}
						else
						{
							node*temp;
							temp=troot;
							temp=temp-&gt;rc;
							while(temp-&gt;lc!=0)
								temp=temp-&gt;lc;
							temp-&gt;lc=troot-&gt;lc;
							if(par-&gt;nic&gt;key)
								par-&gt;lc=troot-&gt;rc;
							else
								par-&gt;rc=troot-&gt;rc;
						}
						delete troot;
					}
					else
					{
						cout&lt;&lt;"Value Not Exist In file";
						found=0;
					}
				}
			}
			else//delete By PID with delete By merge
			{
				cout&lt;&lt;"Enter Patient ID ";
				cin&gt;&gt;key;
				if(key==root-&gt;pid)//If Value is Root
				{
					if(troot-&gt;rc!=0)
					{
						troot=troot-&gt;rc;
						par=troot;
						while(troot-&gt;lc!=0)
						{
							par=troot;
							troot=troot-&gt;lc;
						}
						par-&gt;lc=root-&gt;lc;
						par=root;
						troot=root-&gt;rc;
						root=troot;
						delete par;

					}
					else
						root=troot-&gt;lc;
				}
				else
				{
					while((troot!=0)&&(found==0))
					{
						if(troot-&gt;pid!=key)
							par=troot;
						if(troot-&gt;pid&gt;key)
							troot=troot-&gt;lc;
						else if(troot-&gt;pid&lt;key)&gt;
							troot=troot-&gt;rc;
						else 
							found=1;
					}
					if(found==1)
					{
						if(troot-&gt;rc==0)
						{
							if(par-&gt;pid&gt;key)
								par-&gt;lc=troot-&gt;lc;
							else
								par-&gt;rc=troot-&gt;lc;
						}
						else if(troot-&gt;lc==0)
						{
							if(par-&gt;pid&gt;key)
								par-&gt;lc=troot-&gt;rc;
							else
								par-&gt;rc=troot-&gt;rc;
						}
						else
						{
							node*temp;
							temp=troot;
							temp=temp-&gt;rc;
							while(temp-&gt;lc!=0)
								temp=temp-&gt;lc;
							temp-&gt;lc=troot-&gt;lc;
							if(par-&gt;pid&gt;key)
								par-&gt;lc=troot-&gt;rc;
							else
								par-&gt;rc=troot-&gt;rc;
						}
						delete troot;
					}
					else
					{
						cout&lt;&lt;"Value Not Exist In file";
						found=0;
					}
				}
			}
		}
		else//delete by Copy 
		{
			if(ch=='n')//delet By NIC
			{
				cout&lt;&lt;"Enter NIC ";
				cin&gt;&gt;key;
				if(key==root-&gt;pid)//If Value is Root
				{
					if(troot-&gt;rc!=0)
					{
						troot=troot-&gt;rc;
						par=troot;
						while(troot-&gt;lc!=0)
						{
							par=troot;
							troot=troot-&gt;lc;
						}
						par-&gt;lc=root-&gt;lc;
						par=root;
						troot=root-&gt;rc;
						root=troot;
						delete par;

					}
					else
						root=troot-&gt;lc;
				}
				else
				{
					while((troot!=0)&&(found==0))
					{
						if(troot-&gt;nic!=key)
							par=troot;
						if(troot-&gt;nic&gt;key)
							troot=troot-&gt;lc;
						else if(troot-&gt;nic&lt;key)&gt;
							troot=troot-&gt;rc;
						else 
							found=1;
					}
					if(found==1)
					{
						node*tpar;
						tpar=troot;

						if(tpar-&gt;rc!=0)
						{
							par=tpar;
							tpar=tpar-&gt;rc;
							while(tpar-&gt;lc!=0)
							{
								par=tpar;
								tpar=tpar-&gt;lc;
							}
							swap(troot,tpar);
							//replace data troot with tpar
							par-&gt;lc=0;
							delete tpar;
						}
						else
						{
							if(par-&gt;nic&gt;key)
								par-&gt;lc=troot-&gt;lc;
							else
								par-&gt;rc=troot-&gt;lc;
							delete troot;
						}

					}
					else
					{
						cout&lt;&lt;"Value Not Exist In file";
						found=0;
					}
				}
			}
			else//delete By PID
			{
				cout&lt;&lt;"Enter Patient-ID ";
				cin&gt;&gt;key;
				if(key==root-&gt;pid)//If Value is Root
				{
					if(troot-&gt;rc!=0)
					{
						troot=troot-&gt;rc;
						par=troot;
						while(troot-&gt;lc!=0)
						{
							par=troot;
							troot=troot-&gt;lc;
						}
						par-&gt;lc=root-&gt;lc;
						par=root;
						troot=root-&gt;rc;
						root=troot;
						delete par;

					}
					else
						root=troot-&gt;lc;
				}
				else
				{
					while((troot!=0)&&(found==0))
					{
						if(troot-&gt;pid!=key)
							par=troot;
						if(troot-&gt;pid&gt;key)
							troot=troot-&gt;lc;
						else if(troot-&gt;pid&lt;key)&gt;
							troot=troot-&gt;rc;
						else 
							found=1;
					}
					if(found==1)
					{
						node*tpar;
						tpar=troot;

						if(tpar-&gt;rc!=0)
						{
							par=tpar;
							tpar=tpar-&gt;rc;
							while(tpar-&gt;lc!=0)
							{
								par=tpar;
								tpar=tpar-&gt;lc;
							}
							swap(troot,tpar);
							//replace data troot with tpar
							par-&gt;lc=0;
							delete tpar;
						}
						else
						{
							if(par-&gt;pid&gt;key)
								par-&gt;lc=troot-&gt;lc;
							else
								par-&gt;rc=troot-&gt;lc;
							delete troot;
						}

					}
					else
					{
						cout&lt;&lt;"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&lt;&lt;"Enter 'N'for Add Record By NIC or 'P' Add BY P-ID ";
			cin&gt;&gt;ch;
			tolower(ch);
			if(ch=='n')
			{
				while((troot!=0)&&(found==0))
				{

					par=troot;
					if(troot-&gt;nic&gt;temp-&gt;nic)
						troot=troot-&gt;lc;
					else if(troot-&gt;nic<temp->nic)
						troot=troot-&gt;rc;
					else
					{
						cout&lt;&lt;"%%%%%%%%%%This NIC Already Exist%%%%%%%%%%%\n";
						found=1;
					}
				}
				if(found==0)
				{
					if(par-&gt;nic&gt;temp-&gt;nic)
						par-&gt;lc=temp;
					else
						par-&gt;rc=temp;
				}
				else
				{
					found=0;
				}

			}
			else//adding wrt pid
			{
				while((troot!=0)&&(found==0))
				{
					par=troot;
					if(troot-&gt;pid&gt;temp-&gt;pid)
						troot=troot-&gt;lc;
					else if(troot-&gt;pid<temp->pid)
						troot=troot-&gt;rc;
					else
					{
						cout&lt;&lt;"This NIC Already Exist";
						found=1;
					}
				}
				if(found==0)
				{
					if(par-&gt;pid&gt;temp-&gt;pid)
						par-&gt;lc=temp;
					else
						par-&gt;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-&gt;nic&gt;temp-&gt;nic)
						troot=troot-&gt;lc;
					else if(troot-&gt;nic<temp->nic)
						troot=troot-&gt;rc;
					else
					{
						cout&lt;&lt;"This NIC Already Exist!!!!! ";
						found=1;
					}
				}
			}
			else//PID
			{
				while((troot!=0)&&found==0)
				{
					parent=troot;
					if(troot-&gt;pid&gt;temp-&gt;pid)
						troot=troot-&gt;lc;
					else if(troot-&gt;pid<temp->pid)
						troot=troot-&gt;rc;
					else
					{
						cout&lt;&lt;"This PID Already Exist!!!!! ";
						found=1;
					}
				}
			}//nic if-else
			if(found==0)
			{
				if(uch==0)//nic
				{
					if(parent-&gt;nic&gt;temp-&gt;nic)
						parent-&gt;lc=temp;
					else
						parent-&gt;rc=temp;
				}
				else
				{
					if(parent-&gt;pid&gt;temp-&gt;pid)
						parent-&gt;lc=temp;
					else
						parent-&gt;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&lt;&lt;"-------------------------------------------------------------------------------"
		&lt;&lt;"\t\tHospital Information Using BST Including Filing \n"
		&lt;&lt;"-------------------------------------------------------------------------------\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&lt;char*&gt;(&obj),sizeof(BST));
	obj.showdata();

	}
	fs.close();*/
	int i=1;
up:
	cout&lt;&lt;"Press 'N' for building BST wrt NIC ---'P' wrt P-ID";
	cin&gt;&gt;ch;
	tolower(ch);
	if(ch=='n')
		chh=0;
	else if(ch=='p')
		chh=1;
	else
	{
		cout&lt;&lt;"InValid Choice";
		goto up;
	}
	while(i)
	{
		obj.getdat(chh);
		cout&lt;&lt;"If You Want To Stop Building BST Press 0";
		cin&gt;&gt;i;
		system("cls");
	}

start:
	cout&lt;&lt;"Enter Choice to show record\n " 
		&lt;&lt;"'g' to show greatest key \n"
		&lt;&lt;"'l' for least key \n"
		&lt;&lt;"'a'for acending order\n "
		&lt;&lt;"'d'for decending order\n "
		&lt;&lt;"'r' for delete Record \n"
		&lt;&lt;"'n'for adding new \n"
		&lt;&lt;"'s'for searching Record\n "
		&lt;&lt;"'x' for delete all records\n"
		&lt;&lt;"'z'To Close This menu\n"
		&lt;&lt;"'c'To cleane Screen";
	cin&gt;&gt;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&lt;&lt;"Press 'M'to delete By Merge or 'C'to delete By Copy";
		cin&gt;&gt;ch;
		if(tolower(ch)=='m')//By Merge
			chh=0;		
		else if(tolower(ch)=='c')//By Copy
		{
			chh=1;
		}
		else
		{
			cout&lt;&lt;"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&lt;&lt;"Press 'n' search wrt nic or 'p' wrt Patient ID";
		cin&gt;&gt;ch;
		if(tolower(ch)=='n')
			chh=0;		
		else if(tolower(ch)=='p')
		{
			chh=1;
		}
		else
		{
			cout&lt;&lt;"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&lt;&lt;"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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆