使用链接列表添加和减去Bigints

And*_*ndy 5 c linked-list

我差不多完成了这项任务,这让我很伤心.这是我关于三个不同部分的第三篇文章,老实说我很尴尬,因为我正在努力解决这个问题.

赋值本身是一个程序,使用链表执行大整数的加法和减法(我慢慢开始讨厌链接列表,在Lisp之外).除了实际的加法和减法之外,现在一切似乎都在起作用.我不确定它是否是算术函数,因为它们之前有点工作(但从不100%),但是与S/O社区一起检查并没有什么坏处(通常我不会要求这么多帮助完成作业,因为我更喜欢自己解决问题,但这是一个非常糟糕和忙碌的一周,截止日期即将来临.

我写的算术函数如下,任何人都可以帮我挑出错误吗?

/*
 * Function add
 *
 * @Paramater STRUCT* Integer
 * @Parameter STRUCT* Integer
 *
 * Takes two linked lists representing
 * big integers stored in reversed order,
 * and returns a linked list containing
 * the sum of the two integers.
 *
 * @Return STRUCT* Integer
 * 
 * TODO Comment me
 */
struct integer* add( struct integer *p, struct integer *q )
{
    int carry = 0;

    struct integer *sHead, *sCurr;
    struct integer *pHead, *qHead;

    pHead = p;
    qHead = q;

    sHead = NULL;

    while( p )
    {
        sCurr = ( struct integer* ) malloc (sizeof(struct integer));
        sCurr->digit = p->digit + q->digit + carry;
        sCurr->next = sHead;
        sHead = sCurr;

        carry = 0;

        /*
         * If the current digits sum to greater than 9,
         * create a carry value and replace the current
         * value with value mod 10.
         */
        if( sCurr->digit > 9 )
        {
            carry = 1;
            sCurr->digit = sCurr->digit % 10;
        }

        /*
         * If the most significant digits of the numbers
         * sum to 10 or greater, create an extra node
         * at the end of the sum list and assign it the
         * value of 1.
         */
        if( carry == 1 && sCurr->next == NULL )
        {
            struct integer *sCarry = ( struct integer* ) malloc (sizeof(struct integer));
            sCarry->digit = 1;
            sCarry->next = NULL;
            reverse( &sCurr );
            sCurr->next = sCarry;
            reverse( &sCurr );
        }

        p = p->next;
        if( q->next ) q = q->next; 
        else q->digit = 0; 
    }

    return sHead;
}
Run Code Online (Sandbox Code Playgroud)
/*
 * Function subtract
 *
 * @Parameter STRUCT* Integer
 * @Parameter STRUCT* Integer
 *
 * Takes two linked lists representing struct integers.
 * Traverses through the lists, subtracting each
 * digits from the subsequent nodes to form a new
 * struct integer, and then returns the newly formed
 * linked list.
 *
 * @Return STRUCT* Integer
 * 
 * TODO Comment me
 */
struct integer* subtract( struct integer *p, struct integer *q )
{
    int borrow = 0;

    struct integer *dHead, *dCurr;
    struct integer *pHead, *qHead;

    pHead = p;
    qHead = q;

    dHead = NULL;

    while( p )
    {
        dCurr = (struct integer*) malloc (sizeof(struct integer));
        if( q )
        {
            dCurr->digit = p->digit - q->digit - borrow;
        }
        else
        {
            dCurr->digit = p->digit - borrow;
        }
        dCurr->next = dHead;

        if( dCurr->digit < 0 )
        {
            dCurr->digit += 10;
            borrow = 1;
        }

        dHead = dCurr;

        p = p->next;
        if( q->next) q = q->next;
    }

    return dHead;
}
Run Code Online (Sandbox Code Playgroud)



示例输出应如下所示:

8888888888 + 2222222222 = 11111111110
10000000000 – 9999999999 = 1
10000000000 – 9999999999 = 1
Run Code Online (Sandbox Code Playgroud)

但相反,它看起来像这样:

8888888888 + 2222222222 = 1111111110
10000000000 - 9999999999 = 10000000001
10000000000 - 9999999999 = 10000000001
Run Code Online (Sandbox Code Playgroud)

EDIT整个节目,以目前的形式为3:30 PM EST的,可以在这里以供参考,或情况下,这些功能都没有问题.

pmg*_*pmg 1

else q->digit = 0;
您正在更改函数内部的参数。

尝试更改您的函数以接受const参数并重新编译。

struct integer* add( const struct integer *p, const struct integer *q )
struct integer* subtract( const struct integer *p, const struct integer *q )
Run Code Online (Sandbox Code Playgroud)