纪念一次肉眼debugger的过程

    题目要求写多项式的加法和乘法。

    原理已经想明白了,写完加法,代码有两百多行,吓得我赶紧用函数代替了一些重复度高的代码,缩减到了150行左右。初步代码是这样的:

#include<stdio.h>
#include<stdlib.h>
typedef struct Node *PtrToNode;
struct Node {
    int Coefficient;
    int Exponent;
    PtrToNode Next;
};
typedef PtrToNode Polynomial;



//例程
Polynomial ReadPoly ( );
void PrintPoly ( Polynomial P );
Polynomial AddPolynomial ( Polynomial P1, Polynomial P2 );
Polynomial MultPolynomial ( Polynomial P1, Polynomial P2 );
Polynomial Attach_to_Polynomial ( Polynomial P, Polynomial TmpCell );
Polynomial Find_the_last ( Polynomial P );
Polynomial Creat_Polynomial_Node ( );
void Copy_Data( Polynomial TmpCell, Polynomial P );


int main ( void )
{
    Polynomial P1 = NULL;
    Polynomial P2 = NULL;
    P1 = ReadPoly ( );
    P2 = ReadPoly ( );
    PrintPoly ( P1 );
    PrintPoly ( P2 );


    puts( "\n" );

    Polynomial P3 = NULL;
    P3 = AddPolynomial ( P1, P2 );

    PrintPoly ( P3 );
    puts( "\n" );



    return 0;
}


Polynomial ReadPoly ( )
{
    Polynomial P = NULL;
    int N = 0;
    scanf("%d", &N );
    while ( N-- ){

        int tmp_coefficent = 0;
        int tmp_exponent = 0;
        scanf("%d", &tmp_coefficent );
        scanf("%d", &tmp_exponent );

        Polynomial TmpCell = Creat_Polynomial_Node( );

        TmpCell->Coefficient = tmp_coefficent;
        TmpCell->Exponent = tmp_exponent;
        TmpCell->Next = NULL;

        P = Attach_to_Polynomial( P, TmpCell );
    }

    return P;
}


void PrintPoly ( Polynomial P )
{
    Polynomial TmpCell = P;
    for ( TmpCell = P; TmpCell; TmpCell = TmpCell->Next ){
        printf("%d %d\n", TmpCell->Coefficient, TmpCell->Exponent );
    }
}


Polynomial AddPolynomial ( Polynomial P1, Polynomial P2 )
{
    Polynomial P1_Tmp = P1;
    Polynomial P2_Tmp = P2;
    Polynomial P = NULL;

    while ( P1_Tmp  &&  P2_Tmp ){

        Polynomial TmpCell = Creat_Polynomial_Node( );
        printf("check2......................\n");
        if ( P1_Tmp->Exponent > P2_Tmp->Exponent ){
            Copy_Data ( TmpCell, P1_Tmp );
            P = Attach_to_Polynomial( P, TmpCell );
            P1_Tmp = P1_Tmp->Next;
        }else if ( P2_Tmp->Exponent < P1_Tmp->Exponent ){
            Copy_Data ( TmpCell, P2_Tmp );
            P = Attach_to_Polynomial( P, TmpCell );
            P2_Tmp = P2_Tmp->Next;
        }else if ( P1_Tmp->Exponent == P2_Tmp->Exponent ){
            TmpCell->Coefficient = P1_Tmp->Coefficient + P2_Tmp->Coefficient;
            TmpCell->Exponent = P1_Tmp->Exponent;//or TmpCell->Exponent = P2_Tmp->Exponent
            TmpCell->Next = NULL;
            P = Attach_to_Polynomial( P, TmpCell );
            P1_Tmp = P1_Tmp->Next;
            P2_Tmp = P2_Tmp->Next;
        }
    }

    while ( P1_Tmp ){ //将P1剩下所有数据拷贝到P中
        Polynomial TmpCell = Creat_Polynomial_Node( );
        Copy_Data ( TmpCell, P1_Tmp );
        P1_Tmp = P1_Tmp->Next;
        P = Attach_to_Polynomial( P, TmpCell );
    }

    while ( P2_Tmp ){//将P2剩下所有数据拷贝到P中
        Polynomial TmpCell = Creat_Polynomial_Node( );
        Copy_Data ( TmpCell, P1_Tmp );
        P2_Tmp = P2_Tmp->Next;
        P = Attach_to_Polynomial( P, TmpCell );
    }

    return P;
}


Polynomial Find_the_last ( Polynomial P )
{
    Polynomial last = P;
    while ( last->Next ){
        last = last->Next;
    }
    return last;
}

Polynomial Attach_to_Polynomial ( Polynomial P, Polynomial TmpCell )
{
    Polynomial last = P;
    if ( last != NULL ){
        last = Find_the_last ( P );
        last->Next = TmpCell;
    } else if ( last == NULL ){
        P = TmpCell;
    }

    return P;
}


Polynomial Creat_Polynomial_Node ( )
{
    Polynomial P = ( Polynomial ) malloc ( sizeof( struct Node ) );
    return P;
}

void Copy_Data( Polynomial TmpCell, Polynomial P )
{
    TmpCell->Coefficient = P->Coefficient;
    TmpCell->Exponent = P->Exponent;
    TmpCell->Next = NULL;
}




    一运行,咦,怎么老是在等待,是什么鬼。加了几行打印,来check,发现一只在AddPolynomial函数的第一个while循环中,不往下走。我反复检查while条件,又是写成

while ( P1_Tmp != NULL    &&    P2_Tmp != NULL ), 又是用恒成立,然后在while中加if来判断...............结果还是他喵的一直在while中不出去。

    妈的,灵机一动,输入两个一样的多项式,结果正确!!!!

    再肉眼一看,知道错那里了!!!

if ( P1_Tmp->Exponent > P2_Tmp->Exponent ){
            Copy_Data ( TmpCell, P1_Tmp );
            P = Attach_to_Polynomial( P, TmpCell );
            P1_Tmp = P1_Tmp->Next;
        }else if ( P2_Tmp->Exponent < P1_Tmp->Exponent ){
            Copy_Data ( TmpCell, P2_Tmp );
            P = Attach_to_Polynomial( P, TmpCell );
            P2_Tmp = P2_Tmp->Next;

        }

   他喵的,else if ( P2_Tmp->Exponent < P1_Tmp->Exponent ) 应该写成 else if ( P2_Tmp->Exponent > P1_Tmp->Exponent )

    啊,就这么一个符号的错误,浪费我不止一个半小时。

 


    纪念这次肉眼debugging。要尽快学会用工具来检错了............不然太浪费时间了。


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值