| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 297 人关注过本帖
标题:学习中碰到的一个程序,写完执行的时候出错了,麻烦高手求解,感谢!!
只看楼主 加入收藏
a544107900
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-2-8
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
学习中碰到的一个程序,写完执行的时候出错了,麻烦高手求解,感谢!!
代码如下:
程序代码:
/* Program 11.6 Basics of a family tree */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

struct Family *get_person(void); /* Prototype for input function */
bool set_ancestry(struct Family *pmember1, struct Family *pmember2);
bool related(struct Family *pmember1, struct Family *pmember2);

struct Date
{
    int day;
    int month;
    int year;
};

struct Family /* Family strcture declaration */
{
    struct Date dob;
    char name[20];
    char father[20];
    char mother[20];
    struct Family *next;
    struct Family *previous;
    struct Family *p_to_pa;
    struct Family *p_to_ma;
};

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;
    }

    /* Output Family data in correct order */
    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\t%s's birthdate 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;
    }

    /* Now free the memory */
    current = first;

    while(current -> next != NULL )
    {
        last = current;
        current = current ->next;
        free(last);
    }



    return 0;
}


/* Function to input data on Family menbers */
struct Family *get_person(void)
{
    struct Family *temp; /* Define temporary structure pointer */

    /* Allocate memory for a structure */
    temp = (struct Family*) malloc (sizeof(struct Family));

    printf("\nEnter the name of the person: ");
    scanf("%s", temp -> name);  /* Read the Family's 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 = 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;
}


/* Fill in pointers for mother or father relationships */
bool related (struct Family *pmember1, struct Family *pmember2)
{
    return set_ancestry(pmember1,pmember2) ||
            set_ancestry(pmember2, pmember1);
}




附件里是输入和执行错误的截图:
图片附件: 游客没有浏览图片的权限,请 登录注册
2014-01-18 23:18
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:20 
一般来说都是指针非法访问迼成的

DO IT YOURSELF !
2014-01-19 07:59
a544107900
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-2-8
收藏
得分:0 
回复 2楼 wp231957
大哥,能再详细点吗,是我哪里错了呢
2014-01-19 20:46
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
做为这么长的代码   很少有人会亲自调式的
所以自已学会调式代码很重要
基本都是指针越界的问题

DO IT YOURSELF !
2014-01-19 20:56
a544107900
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-2-8
收藏
得分:0 
不是很明白唉,能不能再指导下呢,谢谢!!
2014-01-22 23:14
快速回复:学习中碰到的一个程序,写完执行的时候出错了,麻烦高手求解,感谢!! ...
数据加载中...
 
   



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

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