| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 446 人关注过本帖
标题:这个程序显示不出某个人父母的信息~~但是要求能显示~~错在哪~~
只看楼主 加入收藏
pokerLee
Rank: 2
等 级:论坛游民
帖 子:41
专家分:29
注 册:2012-11-4
结帖率:93.33%
收藏
已结贴  问题点数:20 回复次数:7 
这个程序显示不出某个人父母的信息~~但是要求能显示~~错在哪~~
程序代码:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
struct Family *get_person(void);
bool related(struct Family *pmember1,struct Family *pmember2);
bool set_ancestry(struct Family *pmember1,struct Family *pmember2);
struct Date
{
    int day;
    int month;
    int year;
};
struct Family
{
    struct Date dob;
    char name[20];
    char father[20];
    char mother[20];
    struct Family *next;
    struct Family *previous;
    struct Family *p_to_ma;
    struct Family *p_to_pa;
};
int main(void)
{
    struct Family *first=NULL;
    struct Family *current=NULL;
    struct Family *last=NULL;
    char more='\0';
    for( ; ; )
    {
        printf("\nDo you want to enter details of a%s person(Y or N)?",
               first!=NULL?"nother":"");
        scanf(" %c",&more);
        if(tolower(more)=='n')
         break;
        current=get_person();
        if(first==NULL)
        {
            first=current;
            last=current;
        }
        else
        {
            (*last).next=current;
            (*current).previous=last;
            last=current;
        }
    }
    current=first;
    while((*current).next!=NULL)
    {
        int parents=0;
        last=(*current).next;
        while(last!=NULL)
        {
            if(related(current,last))
             if(++parents==2)
              break;
            last=(*last).next;
        }
        current=(*current).next;
    }
    current=first;
    while(current!=NULL)
    {
        printf("\n%s was born %d%d%d,and has %s and %s as parents.",
               (*current).name,(*current).dob.day,(*current).dob.month,
               (*current).dob.year,(*current).father,(*current).mother);
        if((*current).p_to_pa!=NULL)
         printf("\n%s's birth date is %d/%d/%d",
                current->father,(*((*current).p_to_pa)).dob.day,
                (*((*current).p_to_pa)).dob.month,
                (*((*current).p_to_pa)).dob.year);
        if((*current).p_to_ma!=NULL)
         printf("and %s's birth date is %d/%d/%d.\n",
                current->mother,(*((*current).p_to_ma)).dob.day,
                (*((*current).p_to_ma)).dob.month,
                (*((*current).p_to_ma)).dob.year);
        current=(*current).next;
    }
    current=first;
    while(current!=NULL)
    {
        last=current;
        current=(*current).next;
        free(last);
    }
    return 0;
}
struct Family *get_person(void)
{
    struct Family *temp;
    temp=(struct Family*)malloc(sizeof(struct Family));
    printf("\nEnter the name of the person: ");
    scanf("%s",(*temp).name);
    printf("\nEnter %s's date of birth (day month year);",(*temp).name);
    scanf("%d %d %d",&(*temp).dob.day,&(*temp).dob.month,&(*temp).dob.year);
    printf("\nWho is %s's father",(*temp).name);
    scanf("%s",(*temp).father);
    printf("\nWho is %s's mother",(*temp).name);
    scanf("%s",(*temp).mother);
    (*temp).next=(*temp).previous=(*temp).p_to_ma=(*temp).p_to_pa=NULL;
    return temp;
}
bool set_ancestry(struct Family *pmember1,struct Family *pmember2)
{
    if(strcmp((*pmember1).father,(*pmember2).name)==0)
    {
        (*pmember1).p_to_pa=pmember2;
        return true;
    }
    if(strcmp((*pmember1).mother,(*pmember2).name)==0)
    {
        (*pmember1).p_to_ma=pmember2;
        return true;
    }
    else
     return false;
}
bool related(struct Family *pmember1,struct Family *pmember2)
{
    return set_ancestry(pmember1,pmember2)||
     set_ancestry(pmember1,pmember2);
}
set_ancestry()函数没有错误~~求指点~~
搜索更多相关主题的帖子: 信息 
2013-11-15 21:36
loveClangage
Rank: 8Rank: 8
来 自:广东云浮
等 级:蝙蝠侠
帖 子:326
专家分:891
注 册:2013-8-23
收藏
得分:5 
好长的代码,没心情看了,

编写的程序,不能改变世界,却可以改变自己...
2013-11-16 00:57
pokerLee
Rank: 2
等 级:论坛游民
帖 子:41
专家分:29
注 册:2012-11-4
收藏
得分:0 
回复 2楼 loveClangage
current=first;
     while((*current).next!=NULL)
     {
         int parents=0;
         last=(*current).next;
         while(last!=NULL)
         {
             if(related(current,last))
              if(++parents==2)
               break;
             last=(*last).next;
         }
         current=(*current).next;
     }
这段代码能把两个人的关系复制到~~struct Family *p_to_pa或者struct Family *p_to_ma中么~~就是求显示~~
2013-11-16 08:02
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:1 
细心检查下
2013-11-16 08:12
pokerLee
Rank: 2
等 级:论坛游民
帖 子:41
专家分:29
注 册:2012-11-4
收藏
得分:0 
回复 4楼 tlliqi
求大神指个方向~~是不是函数作用域的问题?
2013-11-16 08:32
loading_f
Rank: 2
等 级:论坛游民
帖 子:4
专家分:18
注 册:2013-11-15
收藏
得分:13 
单步跟踪下,要学会找出问题
2013-11-16 09:13
pokerLee
Rank: 2
等 级:论坛游民
帖 子:41
专家分:29
注 册:2012-11-4
收藏
得分:0 
回复 6楼 loading_f
谢谢~~
2013-11-16 10:54
亢奋青年
Rank: 2
来 自:遵义市
等 级:论坛游民
帖 子:64
专家分:28
注 册:2013-11-2
收藏
得分:1 
好长啊,渣渣不会

my time,my chips.
2013-11-16 10:56
快速回复:这个程序显示不出某个人父母的信息~~但是要求能显示~~错在哪~~
数据加载中...
 
   



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

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