| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 508 人关注过本帖
标题:给大家一个关于家谱运算的程序。。供大家参考!
只看楼主 加入收藏
猴哥取经
Rank: 2
等 级:论坛游民
帖 子:41
专家分:34
注 册:2012-10-28
结帖率:50%
收藏
 问题点数:0 回复次数:1 
给大家一个关于家谱运算的程序。。供大家参考!
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define MAX_LENGTH 10
struct personal
{
    char  name[MAX_LENGTH];
    personal *bigson ;
    personal *littleson;
    personal *parent;

};

personal *temp,*head=(personal *)malloc(sizeof(personal));
int thelevel=0;

/**
 *初始化
 */
void initial(personal *head)
{
    head->parent=NULL;
    head->bigson=NULL;
    head->littleson=NULL;
    (head->name)[0]=NULL;
}

/**
 *结char数组赋值
 */
void set(char *a,char b[])
{
    for(int i=0;b[i]!=NULL;i++){
        a[i]=b[i];
    }
    a[i]='\0';
}

/**
 *判断两个字符串是否相等
 */
bool checkeq(char a[],char b[])
{
    for(int i=0;b[i]!=NULL;i++){
        if(a[i]!=b[i]){
            return false;
        }
    }
    if(a[i]!=NULL){
         return false;
    }
    return true;
}

/**
 *先序遍历,找到名字为name的人
 */
void findperson(personal *root,char name[],int num)
{
    num++;
    if(root!=NULL){
        if(checkeq(root->name,name)){
            thelevel=num;
            temp=root;
        }else{
            findperson(root->bigson,name,num);
            findperson(root->littleson,name,num);
        }
    }
}


/**
 *打印家谱信息
 */
void printinfo(personal *root)
{
    if(root!=NULL){
          printf("%s ",root->name);
          printinfo(root->bigson);
          printinfo(root->littleson);
    }
}


/**
 *将家庭信息保存进结构体中
 */
bool push(char fa[],char son[])
{
    temp=NULL;
    personal *sonone;
     sonone=(personal *)malloc(sizeof(personal));
    initial(sonone);
    set(sonone->name,son);
    if(checkeq(head->name,"")){
    set(head->name,fa);
        sonone->parent=head;
        head->bigson=sonone;
        printf("      %s   已经加入家谱\n",fa);
        printf("      %s   已经加入家谱\n",son);
        return true;
    }else{
        findperson(head,fa,0);
        if(temp->name==NULL){
            free (sonone);
            printf("输入的父亲不存在\n");
            return false;
        }else if(temp->bigson!=NULL && temp->littleson!=NULL){
            free (sonone);
            printf("儿子字数已满\n");
            return false;
        }else if(temp->bigson==NULL){
            sonone->parent=temp;
            temp->bigson=sonone;

            printf("      %s   已经加入家谱\n",son);
            return true;
        }else{
            sonone->parent=temp;
            temp->littleson=sonone;
            printf("      %s   已经加入家谱\n",son);
            return true;
        }
        return true;
    }
   
    return true;
}

/**
 *输入家庭信息
 */
void inputinfo()
{
    char father[MAX_LENGTH],sonone[MAX_LENGTH],sontwo[MAX_LENGTH];
    printf("           ****************************************************\n");
    printf("           *             格式为:侯超  儿子1  儿子2            *\n");
    printf("           *        侯超为父亲名字,后两个名字为儿子名字       *\n");
    printf("           *        不存在的儿子用0代替,输入两个-1结束        *\n");
    printf("           ****************************************************\n");
   while(scanf("%s%s",father,sonone)){
       if(checkeq(father,sonone) && checkeq(sonone,"-1")){
           break;
       }else{
           scanf("%s",sontwo);
           if(sonone!="0"){
               if(push(father,sonone)){
                   if(sontwo!="0"){
                       push(father,sontwo);
                   }
               }
           }
       }
   }
}

/**
 *输出家庭信息
 */
void outputinfo()
{
    if(head->name==""){
        printf("信息为空\n");
    }else{
       printf("家族信息:\n");
       printinfo(head);
       printf("\n");
       printf("打印完毕\n");
    }
}

/**
 *输入某人的所有儿子
 */
void checkson()
{
    char name[MAX_LENGTH];
     printf("请输入名字\n");
    scanf("%s",name);
    temp=NULL;
    findperson(head,name,0);
    if(temp==NULL){
        printf("此人不在家谱中\n");
        return;
    }else if(temp->littleson==NULL && temp->bigson==NULL){
        printf("此人没有儿子\n");
        return;
    }else if(temp->littleson!=NULL){
        printf("儿子为:\n");
        printf("       %s",temp->littleson->name);
    }
    if(temp->bigson!=NULL){
         printf("  %s",temp->bigson->name);
    }
     printf("\n");

}

/**
 *输入某人的双亲
 */
void checkparent()
{
    char name[MAX_LENGTH];
     printf("请输入名字\n");
    scanf("%s",name);
    temp=NULL;
    findperson(head,name,0);
    if(temp==NULL){
         printf("此人不在家谱里\n");
    }else if(temp->parent==NULL){
         printf("此人没有双亲\n");
    }else{
        printf("双亲为:\n");
        printf("      %s\n",temp->parent->name);
    }
}

/**
 *遍历,找到兄弟
 */
void findba(personal *root,int num,char name[])
{
    num++;
    if(root!=NULL){
        if(num==thelevel){
            if(!checkeq(root->name,name)){
                printf("%s ",root->name);
            }
        }else if(num<thelevel){
            findba(root->bigson,num,name);
            findba(root->littleson,num,name);
        }
    }
}

/**
 *找到某人的兄弟
 */
void showbor()
{
    char name[MAX_LENGTH];
    printf("请输入名字:\n");
    scanf("%s",name);
    temp=NULL;
    thelevel=0;
    findperson(head,name,0);
    if(temp==NULL){
         printf("此人不在家谱里\n");
    }else if(temp->parent==NULL){
        printf("此人没有兄弟\n");
    }
    printf("兄弟为:\n");
    printf("        ");
    findba(head,0,name);
    printf("\n");

}


/**
 *遍历找到祖先
 */
void findan(personal *root,int num)
{
    num++;
    if(root!=NULL){
        if(num<thelevel){
            printf("%s ",root->name);
            findan(root->bigson,num);
            findan(root->littleson,num);
        }
    }
}

/**
 *找到某人的祖先
 */
void showan()
{
    char name[MAX_LENGTH];
    printf("请输入名字:\n");
    scanf("%s",name);
    temp=NULL;
    thelevel=0;
    findperson(head,name,0);
    if(temp==NULL){
        printf("此人不在家谱中\n");
    }else if(temp->parent==NULL){
        printf("此人没有祖先\n");
    }
    printf("祖先为:\n");
    printf("          \n");
    findan(head,0);
    printf("\n");

}
int main()
{
    int inputcase;
    initial(head);
    while(1){
            printf("           ****************************************************\n");
            printf("           *                       菜单                       *\n");
            printf("           *                1  输入家庭信息                   *\n");
            printf("           *                2  输出家庭信息                   *\n");
            printf("           *                3  查看某人的儿子                 *\n");
            printf("           *                4  查看某人的双亲                 *\n");
            printf("           *                5  查看某人的所有兄弟             *\n");
            printf("           *                6  查看某人的所有祖先             *\n");
            printf("           *                0  退出                           *\n");
            printf("           ****************************************************\n");
            scanf("%d",&inputcase);
           if(inputcase==0){
                break;
            }
            switch(inputcase){
            case 1:
                inputinfo();
                break;
            case 2:
                outputinfo();
                break;
            case 3:
                checkson();
                break;
            case 4:
                checkparent();
                break;
            case 5:
                showbor();
                break;
            case 6:
                showan();
                break;
            default:
                printf("输入错误\n");
            }
    }

    return 0;
}
搜索更多相关主题的帖子: head void personal include initial 
2012-12-23 20:00
liuwanbing
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-12-23
收藏
得分:0 
错误好多,怎么办啊??
2014-12-23 14:41
快速回复:给大家一个关于家谱运算的程序。。供大家参考!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022011 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved