注册 登录
编程论坛 C++ Builder

帮我改个程序

QQ812723228 发布于 2017-06-27 14:49, 3338 次点击
#include "iostream"
#include "string"
using namespace std;
/********************* 树的节点类 *****************************/
 class Node
    {
     friend class Person; public: Node()
     {   
     name = '?'; lchild = NULL; rchild = NULL;
     }
    private: string name; Node *lchild; Node *rchild;
    };
 /****************** 家谱成员类 ***************************/
 class Person
 {
 public: void Update(Person &L);
//家谱成员改名
         void Delete(Person &L);   
//删除部分家庭成员
         void Insert(Person &L);   
//添加单个家庭成员
         void Print(Node *p);   
//显示部分家庭成员
         Node * Lookup(Node *p, string name);   
//查找某人
         void Add(Person &L);   
//添加部分家庭成员
         void Creat(Person &L);   
//创建家谱
         Person() { root = NULL; }
         private: Node *root; };
/************** 创建家谱 *****************/
         void Person::Creat(Person &L)
         {
         cout << "请输入祖先的姓名:"; string rootname;
         cin >> rootname; Node *p = new Node;
         p->name = rootname; L.root = p;
         cout << "此家谱的祖先是:" << p->name << "\n";
         }
/************** 添加部分家庭成员 *****************/
         void Person::Add(Person &L)
         {
         cout << "请输入要建立家庭的人的姓名:";
         string rootname;
         cin >> rootname;
         Node *s = Person::Lookup(L.root, rootname);
         if (s)
         {
         Node *r = s;
         cout << "请输入" << s->name << "的儿女人数:";
         int n;
         cin >> n;
         int m = n;
         cout << "请依次输入" << s->name << "的儿女的姓名:";
         while (m)
            {
            Node *q = new Node;
            string name;
            cin >> name;
            q->name = name;
            if (m == n)
                {
                s->lchild = q;
                s = s->lchild;
                }
            else
                {
                s->rchild = q;
                s = s->rchild;
                }
            m --;
            }
        Person::Print(r);
        }
        else
            {
            cout << "查无此人,请重新输入!" << '\n'; Person::Add(L);
            }
        }
/***************** 查找某人 *********************/
Node * Person::Lookup(Node *p, string name)
{
Node *t = NULL;
Node *s[100];
int top = 0;
while (p || top > 0)
    {
    while (p)
        {
        if (p->name == name)
            {
            t = p;
            }
        s[++ top] = p;
        p = p->lchild;
        }
    p = s[top --];
    p = p->rchild;
    }
return t;
}
/***************** 显示部分家庭成员 ********************/
void Person::Print(Node *p)
{
cout << p->name << "的第一代子孙是:" << p->lchild->name << '\t';
p = p->lchild;
while (p->rchild)
    {
    cout << p->rchild->name << '\t';
    p = p->rchild;
    }
cout << '\n';
}
/****************** 添加单个家庭成员 **************/
void Person::Insert(Person &L)
{
    cout << "请输入要添加儿子(或女儿)的人的姓名:";
    string rootname;
    cin >> rootname;
    Node *s = Person::Lookup(L.root, rootname);
    if (s)
    {
        Node *r = s;
        cout << "请输入" << s->name << "新添加的儿子(或女儿)的姓名:" ;
        Node *p = new Node;
        string name;   
        cin >> name;
        p->name = name;
        if (!s->lchild)        
        {
            s->lchild = p;
        }
        else
        {
            s = s->lchild;
            while (s->rchild)
            {
                s = s->rchild;
            }
            s->rchild = p;
        }
        Person::Print(r);
    }
    else
    {
        cout << "查无此人,请重新输入!" << '/n';
        Person::Insert(L);
    }
}
/************* 删除部分家庭成员 ***********/
void Person::Delete(Person &L)
{
    cout << "请输入要解散家庭的人的姓名:";
    string rootname;
    cin >> rootname;
    Node *s = Person::Lookup(L.root, rootname);
    if (s)
    {
        if (s->lchild)
        {
            cout << "要解散家庭的人是:" << s->name << '/n';
            Person::Print(s);
            s->lchild = NULL;
        }
        else
        {
            cout << s->name << "尚未有家庭!";
        }
    }
    else
    {
        cout << "查无此人,请重新输入!" << '/n';
        Person::Delete(L);
    }
}
/*********** 家谱成员改名 ***********/
void Person::Update(Person &L)
{
    cout << "请输入要更改姓名的人的目前姓名:";
    string rootname;
    cin >> rootname;
    Node *s = Person::Lookup(L.root, rootname);
    if (s)
    {
        cout << "请输入更改后的姓名:";
        string name;
        cin >> name;
        s->name = name;
        cout << rootname << "已更名为" << s->name << '/n';
    }
    else
    {
        cout << "查无此人,请重新输入!" << '/n';
        Person::Update(L);
    }
}
/******** 主函数 ****************/
void main()
{
    int ch;
    Person L;
    do{
            cout<<"** 家谱管理系统 **";
            cout<<"\n\n  1.创建家谱";
            cout<<"\n  2.添加部分家庭成员";
            cout<<"\n  3.添加单个家庭成员";
            cout<<"\n  4.删除部分家庭成员";
            cout<<"\n  5.家谱成员改名";
            cout<<"\n  6.查找某人";
            cout<<"\n  7.显示部分家庭成员";
            cout<<"\n  8.退出系统";
            cout<<"\n 请输入你的选择(1-8):";
            cin>>ch;
    switch(ch)
    {   
        case 1:{
                L.Creat(L);
           }break;
        case 2:
            {
                L.Add(L);
                 
            } break;
        case 3:
            {
                L.Insert(L);
                 
            } break;
        case 4:
            {
                L.Delete(L);
                 
            } break;
        case 5:
            {
                L.Update(L);
               
            } break;
        case 6:
            {
                L.Lookup(L);
            }break;
        case 7:
            {
                L.Print(L);
            }
        case 8:
            {
                exit(0);
            }
    }
    }while(ch>=1&&ch<=8);
}
0 回复
1