标题:一个C语言的问题。好像是链表那里错了,但是调试了好多次都没发现问题,求指 ...
只看楼主
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
结帖率:87.5%
已结贴  问题点数:18 回复次数:4 
一个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
qunxingw
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:24
帖 子:1676
专家分:7295
注 册:2011-6-30
得分: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;

www.qunxingw.wang
2013-10-02 17:17
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
得分:0 
回复 2楼 qunxingw
开辟空间的时候sizeof里面不是应该是加*的么
2013-10-02 19:39
qunxingw
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:24
帖 子:1676
专家分:7295
注 册:2011-6-30
得分:18 
那种情况是“乘”的意思吧

www.qunxingw.wang
2013-10-02 20:48
DarylL
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2012-12-6
得分:0 
回复 4楼 qunxingw
好吧果然是这问题
2013-10-02 22:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-421367-1-1.html




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

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