• 數據結構課程設計報告一元多項式計算 聯系客服

    printf(\請輸入多項式hb的系數與指數:\\n\hb=create();

    gotoTextPos(2,17); print(hb);

    gotoTextPos(2,18); printf(\

    gotoTextPos(2,18);

    printf(\多項式的和是:\\n\polyadd(ha,hb); gotoTextPos(2,19); print(ha);

    gotoTextPos(2,19); printf(\

    gotoTextPos(2,20);

    printf(\請輸入多項式hc的系數與指數:\\n\hc=create();

    gotoTextPos(2,27); print(hc);

    gotoTextPos(2,27); printf(\

    gotoTextPos(2,28);

    printf(\請輸入多項式hd的系數與指數:\\n\hd=create();

    gotoTextPos(2,35); print(hd);

    gotoTextPos(2,35); printf(\

    gotoTextPos(2,36);

    printf(\多項式的差是:\\n\polycha(hc,hd); gotoTextPos(2,37); print(hc);

    gotoTextPos(2,38); printf(\getch(); menu(); }

    4.4 算法

    void polyadd(node *ha, node *hb)//一元多項式相加函數,用于將兩個多項式相加,然后將和多項式存放在多項式ha中,并將多項式hb刪除

    8

    {

    node *p,*q,*pre,*temp; int sum; p=ha->next; q=hb->next; pre=ha;

    while(p!=NULL&&q!=NULL) {

    if(p->expexp) {

    pre->next=p; pre=pre->next; p=p->next; }

    else if(p->exp==q->exp) {

    sum=p->coef+q->coef; if(sum!=0) {

    p->coef=sum;

    pre->next=p;pre=pre->next;p=p->next; temp=q;q=q->next;free(temp); }

    else //如果系數和為零,則刪除結點p與q,并將指針指向下一個結點 {

    temp=p->next;free(p);p=temp; temp=q->next;free(q);q=temp; } } else {

    pre->next=q; pre=pre->next; q=q->next; } }

    if(p!=NULL) //將多項式A中剩余的結點加入到和多項式中 pre->next=p; else

    pre->next=q; }

    4.5主函數

    int main()

    9

    {

    system(\

    setConsoleTitle(\一元多項式計算 by余海剛\ system(\ menu();

    kuanjia1();

    gotoTextPos(10,7); printf(\拜\ delayMS(600);

    gotoTextPos(19,7); printf(\拜\ delayMS(600);

    gotoTextPos(20,9); printf(\ delayMS(600);

    gotoTextPos(21,9); printf(\ delayMS(600);

    gotoTextPos(24,9); printf(\余\ delayMS(600);

    gotoTextPos(26,9); printf(\海\ delayMS(600);

    gotoTextPos(28,9); printf(\剛\ delayMS(600);

    return 0;

    10

    5、 測試結果

    5.1輸入界面圖

    圖5.1

    11