| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 480 人关注过本帖, 2 人收藏
标题:一个C语言的问题。好像是链表那里错了,但是调试了好多次都没发现问题,求指 ...
取消只看楼主 加入收藏
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
结帖率:87.5%
收藏(2)
已结贴  问题点数:18 回复次数:2 
一个C语言的问题。好像是链表那里错了,但是调试了好多次都没发现问题,求指导
写了个求最大公约数的程序。结果运行的时候是这种情况
图片附件: 游客没有浏览图片的权限,请 登录注册

调试的时候是这样的
图片附件: 游客没有浏览图片的权限,请 登录注册

点了中断,再继续,就这样了
图片附件: 游客没有浏览图片的权限,请 登录注册

但是调试的时候,输出的结果是对的
图片附件: 游客没有浏览图片的权限,请 登录注册

我也不知道是怎么会,搞了半天没发现错误,求帮忙找下错误,谢谢了!
还有输出信息在这里
图片附件: 游客没有浏览图片的权限,请 登录注册

然后代码:
程序代码:
/****************************************

 *    Made by Liang Hanlei                *

 *    Copyright © Liang Hanlei Reverse    *

 ****************************************/

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct NUMBERS
{
    struct NUMBERS * front;
    int no;
    int num;
    struct NUMBERS * next;
} NUMBERS;

int GreatestCommonDivisor(int, int);    //To fig out the greatest common divisor
void AddList(int, int, NUMBERS *, NUMBERS *);    //To add items to list
void ChangeMax(int *, int *);    //To make the prvious number larger
void FreeList(NUMBERS *);
int main(void)
{
    NUMBERS * head = NULL;
    NUMBERS * num = NULL;
    NUMBERS * prev = NULL;
    NUMBERS * interate = NULL;    //To interate the list
    int prevNum;    //To store the previous number
    int remainder;    //To store the remainder
    int numTemp;    //To store the integer at moment
    int count = 0;    //To count how many integers are entered in
    int no;
    char dig;

    puts("Please enter some integers to cotinue (with \"Space\" button to dig, with \"Enter\" button to quit entering):");
    head = (NUMBERS *) malloc(sizeof(NUMBERS));
    prev = head;
    no = 0;
    while (scanf("%d%c", &numTemp, &dig))
    {
        if (!count && dig == '\n')
        {
            puts("Nothing have entered.");
            exit(1);
        }
        ++no;
        num = (NUMBERS *) malloc(sizeof(NUMBERS *));
        AddList(numTemp, no, prev, num);
        prev = num;
        if (dig == '\n')
        {
            break;
        }
        ++count;
    }
    num->next = head->next;
    head->next->front = num;

    interate = head->next;
    prevNum = interate->num;
    no = head->next->no;
    while (interate->next->no != no)
    {
        interate = interate->next;
        remainder = interate->num;
        ChangeMax(&prevNum, &remainder);
        prevNum = GreatestCommonDivisor(prevNum, remainder);
        if (prevNum == 1)
        {
            break;
        }
    }
    printf("The greatest common divisor is %d\n", prevNum);
    FreeList(head);

    return 0;
}

int GreatestCommonDivisor(int prevNum, int remainder)
{
    int prevRemainder;

    while (remainder)
    {
        prevRemainder = remainder;
        remainder = prevNum % remainder;
        prevNum = prevRemainder;
    }
    
    return prevNum;
}

void AddList(int numTemp, int no, NUMBERS * prev, NUMBERS * num)
{
    num->num = numTemp;
    num->no = no;
    prev->next = num;
    num->front = prev;
}

void ChangeMax(int * prevNum, int * remainder)
{
    int temp;

    if (*prevNum < *remainder)
    {
        temp = *prevNum;
        *prevNum = *remainder;
        *remainder = temp;
    }
}

void FreeList(NUMBERS * head)
{
    int no;
    NUMBERS * prev;

    no = head->next->front->no;
    prev = head->next;
    while (prev->no != no)
    {
        free(head);
        head = prev;
        prev = prev->next;
    }
    free(head);
    free(prev);
}
搜索更多相关主题的帖子: Copyright C语言 公约数 信息 
2013-10-02 15:13
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
收藏
得分:0 
回复 2楼 qunxingw
开辟空间的时候sizeof里面不是应该是加*的么
2013-10-02 19:39
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
收藏
得分:0 
回复 4楼 qunxingw
好吧果然是这问题
2013-10-02 22:21
快速回复:一个C语言的问题。好像是链表那里错了,但是调试了好多次都没发现问题 ...
数据加载中...
 
   



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

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