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

📄 main.cpp

📁 1.程序完整实现了异质树的遍历
💻 CPP
字号:
#include <iostream>
#include <string>
#include "Person.h" 
#include "Tree.h"
using namespace std;

Person *CreatNode();
 
int main()
{
     int opt_1;
     string  name;
	 Tree  YxTree;
     Person* Point = NULL; 
	 Person *re_point = NULL;
     
     opt_1 = 0;     
      
     cout<<"欢迎使用异质树!"<<endl;
     cout<<"请选择相应的操作数字"<<endl;
     cout<<"1.插入一个新节点"<<endl;
     cout<<"2.查询一个节点"<<endl;
     cout<<"3.删除一个节点"<<endl;
     cout<<"4.遍历并打印全树"<<endl;
 
while(1)
{
	 cout<<"请输入操作类型"<<endl;
     cin>>opt_1;
     switch (opt_1)
     {
         case 1:
			 cout<<"请输入增加节点类型及信息:"<<endl;
			 if(Point == NULL)
			 {
				 Point = CreatNode();
				 if(Point == NULL)
					 cout<<"创建根节点不成功"<<endl;
			 }
			 else
			 {
				 Person *C_Point = NULL;
				 C_Point = CreatNode();
				 if(C_Point != NULL)
				 YxTree.insert_t(Point,C_Point);
			 }			
             break;         
         case 2:
             cout<<"请输入查找节点的姓名:"<<endl;
			 cin>>name;
			 re_point = YxTree.findnode(Point,name);
			 if(re_point == NULL)
				 cout<<"没有找到该节点"<<endl;
			 else
			 {
				 re_point->print();
			 }
             break; 
         case 3 :
			 if(Point == NULL)
			 {
				cout<<"请创建根节点"<<endl;
				break;
			 }
             cout<<"请输入删除节点的姓名:"<<endl;
			 cin>>name;
			 re_point = YxTree.findnode(Point,name);
			 if((Point == re_point)&&(Point->r_Child == NULL)&&(Point->l_Child == NULL))
			 {
				cout<<"只有根节点,删除根节点"<<endl;
				Point = NULL;				
			 }	
			 else if(Point == re_point)
			 {
				 if(Point->r_Child !=NULL)
				 { 
					 if(Point->l_Child!=NULL)
						 YxTree.insert_t(Point->r_Child,Point->l_Child);
					Point = Point->r_Child;
				 }
				 else
					 Point = Point->l_Child;
				 cout<<"删除成功"<<endl;
			 }
			 else
 			 YxTree.del(Point,name);
             break; 
         case 4 : 
			  YxTree.travel(Point);
			  break;
         default:
             {
              cout<<"操作数不正确,系统退出!"<<endl;
			  return 0;
             }    
     } 
}
              
}     
     
Person *CreatNode()
{
    string name;
    int age;
    string birthday;
    string tel;
    Person *Point;
    cout<<"请输入以下信息"<<endl;
    
    cout<<"姓名:";
	cin>>name;
     
    cout<<"年龄:";
	cin>>age;
     
    cout<<"出生日期:";
	cin>>birthday;
    
    cout<<"电话号码:";
    cin>>tel;
    
    int opt_2 = 0;
   
    cout<<"请选择节点类型:1.学生,2.教职工"<<endl;
    cin>>opt_2;
    
    switch (opt_2)
    {
        case 1:           
            int studentnum;  
            int score;
            
            cout<<"您要添加的节点类型为学生类,请输入以下信息:"<<endl;
            
            cout<<"学号:";
            cin>>studentnum;

            cout<<"获得学分:"; 
            cin>>score;     
            Point = new Student(name,age,birthday,tel,studentnum,score);
			return Point;
             break; 
            
        case 2:
			int opt_3;
            int facultynum;
            float salary;
            
            cout<<"您要添加的节点类型为教职工类,请输入以下信息:"; 
            
            cout<<"职工号:";
            cin>>facultynum;
            cout<<"月工资:";
			cin>>salary;           
            cout<<"请选择职工类别:1.教师,2.职工"<<endl;
			opt_3 = 0;
            cin>>opt_3;
            
            switch(opt_3)
            {
                case 1:
                    int deptnum; 
                    
                    cout<<"院系号:";
					cin>>deptnum;
                    Point = new Teacher(name,age,birthday,tel,facultynum,salary,deptnum); 
                    return Point;
					break;
                    
                case 2:
                    int comment;                   
                    cout<<"部门";
					cin>>comment;
                    Point = new Staff(name,age,birthday,tel,facultynum,salary,comment);
                    return Point;
					break;                   
                default:  
					cout<<"操作错误,请返回";
					return NULL;
					 break;
            }
		default: 
			return NULL;
            break;
    } 
 
}













                

⌨️ 快捷键说明

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