⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10372049keshe.cpp

📁 本设计使用C++来实现一个简单的数据结构问题
💻 CPP
字号:
#include<iomanip.h>
#include"AvlTreeADT.h"
#include<stdlib.h>
#include<fstream.h>
#include<string.h>
#include<ctype.h>

typedef char  String[25];
struct DATA
{
int  key;
String name;
int  phonenumber;
};// DATA

void buildList  (AvlTree<DATA,int>& wList);
bool getData  (DATA& aWord,ifstream& fsWords);
void process    (AvlTree<DATA,int>& wList);  
char getchoice  ();
void searchData     (AvlTree<DATA,int>& wList);
void insertData (AvlTree<DATA,int>& wList);
void deleteData (AvlTree<DATA,int>& wList);
void printList  (AvlTree<DATA,int>& wList);
void printData  (DATA aWord);

void main()
{

    AvlTree<DATA,int> wList;
	buildList(wList);
	process(wList);
}//	main

//***************************** buildList  ************************************
void buildList(AvlTree<DATA,int>& wList)
{
    String fileName;
    DATA newData;
	ifstream fsWords;
   
	cout<<"Please enter the name of file to be processed:"<<endl;
   cin>>fileName;
   fsWords.open(fileName);
   if (!fsWords)
   {
   cout<<fileName<<"\a\a could not be opened\n";
   cout<<"Please verify  your file name and try again.\n";
   exit(100);
   }
  
   while(getData (newData,fsWords))
   { 
	 if (!wList.AVL_Insert(newData))
	 {
      cout<<"\a\aMemory full. Can't continue\n";
      exit(121);
	 }
     
   }
   fsWords.close();
   return ;
}// buildList
//******************************* getWord *************************************
bool getData(DATA& aWord,ifstream& fsWords)
{
	String strIn;
	int  number;
	int  phonenumber;
    fsWords>>number;
	fsWords>>strIn;
    fsWords>>phonenumber;
	if (!(fsWords.good()))
		return false;
    aWord.key=number;
    strcpy(aWord.name,strIn);
    aWord.phonenumber=phonenumber;
	 return true;
}


//***************************** process ***************************************
void process (AvlTree<DATA,int>& wList)  
{

	char choice;
	bool valid=true;
	
	do
	   {
	    choice = getchoice ();	

	    switch (choice)
	       {
		    case 'S': searchData (wList);  
				     break;
	        case 'I': insertData (wList); 
				     break;
			case 'D': deleteData(wList);    
				     break;
            case 'P': printList(wList);
                     break;
			case 'Q': cout<<"  The programe has been closed  "<<endl;
				      break;
		 } 
		} while (choice !='Q');
	return;
}//	process


//************************* getchoice *************************************************
char getchoice()
{
	char choice;
	bool valid=true;
    cout<<"Here your choices:\n"
	    <<"  S : searchData of a person's infortion\n"
	    <<"  I : Insert a  person's information\n"
	    <<"  D : Delete a person's information\n"
        <<"  P : Print all person's informations\n"
        <<"  Q : Quit\n"
        <<"  Enter your choice:  ";
	do
	{
	cin>>choice;
	choice=toupper(choice);
	switch (choice)
	{
	   case 'S' :
	   case 'I' :
	   case 'D' :	   
	   case 'P' :	  	
	   case 'Q' :  valid =true ;
		           break;
       default  :  valid = false;
	               cout << "Please try again: ";
	                break;
	       }  
	   } while (!valid);
	
	return choice;
} // getchoice
	
//****************************** searchData *********************************
void searchData(AvlTree<DATA,int>& wList)
{
//Local Definetions
  bool found;
  int number1;
  DATA inform;
  cout<<" Enter you want to searchData number1\n";
  cin>>number1;
  found=wList.AVL_Retrieve(number1,inform);
  if (found)
  {
	  cout<<"    number           name       phonenumber "<<endl;   
	  cout<<setw(10)<<inform.key<<setw(15)<<inform.name
	      <<setw(10)<<inform.phonenumber<<endl;
  }
 else 
	  cout<<"sorry ,there is no this person"<<endl;
}// searchData

//****************************** insertData ********************************
void insertData(AvlTree<DATA,int>& wList)
{     
    int number1;
	String name;
	int telephonenun;
	DATA inform2;
    cout<<" Enter your number"<<endl;
	cin>>number1;
    cout<<" Enter your name"<<endl;
	cin>>name;
    cout<<" Enter your phonenumber"<<endl;
	cin>>telephonenun;
    inform2.key=number1;
    strcpy(inform2.name,name);
    inform2.phonenumber=telephonenun;
	if (!wList.AVL_Insert(inform2))
	{
      cout<<"\a\aMemory full. Can't continue\n";
      exit(121);
	}
	ofstream fsWords;
     fsWords.open("jilu.txt",ios::app);
	 fsWords<<inform2.key<<"  ";
	 fsWords<<inform2.name<<"  ";
	 fsWords<<inform2.phonenumber<<"  ";

}// insertData


//***************************** deleteData ******************************
void deleteData(AvlTree<DATA,int>& wList)
{
	bool found;
	int  delnumber;
    cout<<" Enter the number you will delete "<<endl;
	cin>>delnumber;
    found=wList.AVL_Delete(delnumber);
	if (found)
	   cout<<" the information of "<<delnumber<<" has been deleted\n";
	else 
       cout<<" there is no this person\n" ;
}//deleteData

//***************************** printList *******************************
void printList(AvlTree<DATA,int>& wList)
{
   wList.AVL_Traverse(printData);
       return ;
}//  printList

//***************************** printData ******************************
void printData   (DATA aWord)
{

    cout<<"    number          name       phonenum "<<endl;
	cout<<setw(10)<<aWord.key
	    <<setw(15)<<aWord.name
		<<setw(15)<<aWord.phonenumber<<endl;
	return;
}//  printData

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -