精品偷拍一区二区三区,亚洲精品永久 码,亚洲综合日韩精品欧美国产,亚洲国产日韩a在线亚洲

  • <center id="usuqs"></center>
  • 
    
  • 一元多項(xiàng)式運(yùn)算

    一元多項(xiàng)式運(yùn)算
    一.問題描述
    設(shè)計(jì)一個(gè)簡單的一元稀疏多項(xiàng)式加法運(yùn)算器.
    二.基本要求
    一元稀疏多項(xiàng)式簡單計(jì)算器的基本功能包括:
    1.按照指數(shù)升序次序,輸入并建立多項(xiàng)式A與B.
    2.計(jì)算多項(xiàng)式A與B的和,即建立多項(xiàng)式A+B.
    3.按照指數(shù)升序次序,輸出多項(xiàng)式A、B、A+B.
    三.提示與分析
    1.一元n次多項(xiàng)式:P(x,n)=P0+P1X1+P2X2+…+PnXn,其每一個(gè)子項(xiàng)都是由“系數(shù)”和“指數(shù)”兩部分來組成的,因此可以將它抽象成一個(gè)由“系數(shù)、指數(shù)對(duì)”構(gòu)成的線性表,其中,多項(xiàng)式的每一項(xiàng)都對(duì)應(yīng)于線性表中的一個(gè)數(shù)據(jù)元素.由于對(duì)多項(xiàng)式中系數(shù)為0的子項(xiàng)可以不記錄它的指數(shù)值,對(duì)于這樣的情況就不再付出存儲(chǔ)空間來存放它了;基于此,可以采用一個(gè)帶有頭結(jié)點(diǎn)的單鏈表來表示一個(gè)一元多項(xiàng)式.
    例如,多項(xiàng)式A= 3+6X3-2X8+12X20 、B= 2X-2-6X3+8X10可分別表示為:
    2.?dāng)?shù)據(jù)類型定義可描述如下:
    typedef struct pnode
    {int coef; /*系數(shù)域*/
    int exp; /*指數(shù)域*/
    struct pnode *next; /*指針域,指向下一個(gè)系數(shù)不為0的子項(xiàng)*/
    }PolyNode,*PolyLink;
    PolyLink A,B,C; /*單鏈表存儲(chǔ)的多項(xiàng)式A、B、C*/
    3.基本功能分析
    (1)輸入多項(xiàng)式,建立多項(xiàng)式鏈表
    首先創(chuàng)建帶頭結(jié)點(diǎn)的單鏈表;然后按照指數(shù)遞增的順序和一定的輸入格式輸入各個(gè)系數(shù)不為0的子項(xiàng):“系數(shù)、指數(shù)對(duì)”,每輸入一個(gè)子項(xiàng)就建立一個(gè)結(jié)點(diǎn),并將其插入到多項(xiàng)式鏈表的表尾,如此重復(fù),直至遇到輸入結(jié)束標(biāo)志的時(shí)候停止,最后生成按指數(shù)遞增有序的鏈表.
    (2)多項(xiàng)式相加
    多項(xiàng)式加法規(guī)則:對(duì)于兩個(gè)多項(xiàng)式中指數(shù)相同的子項(xiàng),其系數(shù)相加,若系數(shù)的和非零,則構(gòu)成“和多項(xiàng)式”中的一項(xiàng);對(duì)于指數(shù)不同的項(xiàng),直接構(gòu)成“和多項(xiàng)式”中的一項(xiàng).
    將(1)中單鏈表表示的兩個(gè)多項(xiàng)式A和B相加,運(yùn)算的結(jié)果是利用原表空間生成一個(gè)新鏈表,表示和多項(xiàng)式C.運(yùn)算規(guī)則如下:
    設(shè)指針pa、pb分別指向多項(xiàng)式鏈表A、B的第一個(gè)結(jié)點(diǎn),比較pa、pb所指兩結(jié)點(diǎn)中的指數(shù)項(xiàng):
    ① 若pa->exp < pb->exp,則將pa所指結(jié)點(diǎn)插入到“和多項(xiàng)式”鏈表中去;
    ② 若pa->exp > pb->exp,則將pb所指結(jié)點(diǎn)插入到“和多項(xiàng)式”鏈表中去;
    ③ 若pa->exp== pb->exp,則計(jì)算系數(shù)和pa->coef+pb->coef,若和非零,插入到“和多項(xiàng)式”鏈表中去,刪除pb所指結(jié)點(diǎn);否則刪除pa、pb所指結(jié)點(diǎn).
    繼續(xù)比較下一項(xiàng),重復(fù)上述過程,直至A、B中某一鏈表結(jié)束,此時(shí)將非空鏈表中剩余的結(jié)點(diǎn)出入到“和多項(xiàng)式”鏈表即可.
    (3)多項(xiàng)式的輸出
    可以在文本界面下,采用類似于數(shù)學(xué)表達(dá)式的方式輸出多項(xiàng)式,如多項(xiàng)式A可顯示為:
    A=3+6XÙ3-2XÙ8+12XÙ20
    需要注意:
    系數(shù)值為1的非零次項(xiàng)的輸出形式中略去系數(shù)1,如子項(xiàng)1x8的輸出形式為x8,項(xiàng)-1x3的輸出形式為-x3.
    多項(xiàng)式的第一項(xiàng)的系數(shù)符號(hào)為正時(shí),不輸出“+”,其它項(xiàng)要輸出“+”、“-”符號(hào).
    其他人氣:754 ℃時(shí)間:2020-03-25 14:57:40
    優(yōu)質(zhì)解答
    #include
    #include
    #define A1(a,b,c,d,e) gotoxy(a,b);printf("c%d=",d);scanf("%f",e);
    #define A2(a,b,c,d,e) gotoxy(a,b);printf("e%d=",d);scanf("%f",e);
    #define B1 do { printf("\n想繼續(xù)嗎(求函數(shù)值/求導(dǎo)數(shù))(y/N)?")
    #define B2 ch2=bioskey(0); printf("[%c]",ch2)
    #define B3 if((ch2=='Y')||(ch2=='y')) tc(head1->next,head2->next,r); }
    #define B4 while((ch2=='y')||(ch2=='Y'))
    struct ma
    { float c;
    float e;
    struct ma *next; }
    main()
    { struct ma *head1,*head2,*p,*q,*r,*rm;
    float s1[100][3],s2[100][3],mid1,mid2,mid3;
    int i,j,t,num1,num2;
    char ch,ch1,ch2,ch11,ch21;
    struct ma *add();
    struct ma *mul();
    void tc();
    void output();
    loop1: t=0; ch='y';
    clrscr();
    printf("請(qǐng)輸入第一個(gè)多項(xiàng)式的項(xiàng)數(shù):");
    scanf("%d",&num1);
    q=head1=(struct ma*)malloc(sizeof(struct ma));
    (*q).next=NULL;
    if(num1>0)
    { printf("請(qǐng)輸入第一個(gè)多項(xiàng)式的系數(shù)(c) 和指數(shù)(e):");
    for(i=1;ic); (*r).e=q->e; q=(*q).next;}
    else if(p->ee) { (*r).c=p->c ; (*r).e=p->e; p=(*p).next; }
    else { if(ch1=='+') (*r).c=(p->c)+(q->c);
    else (*r).c=(p->c)-(q->c);
    (*r).e=(p->e); p=(*p).next;q=(*q).next;}
    }while((p!=NULL)&&(q!=NULL));
    while(q!=NULL)
    { r=(struct ma*)malloc(sizeof(struct ma));
    (*s).next=r; s=r;
    if(ch1=='+') (*r).c=q->c ;
    else (*r).c=-(q->c);(*r).e=q->e; q=(*q).next;}
    while(p!=NULL)
    { r=(struct ma*)malloc(sizeof(struct ma));
    (*s).next=r; s=r;
    (*r).c=p->c ; (*r).e=p->e;p=(*p).next; }
    (*r).next=NULL;
    s=r=(*head3).next;
    while(r->next!=NULL)
    if(r->e==(r->next)->e)
    { r->c+=(r->next)->c; s=r->next;
    r->next=(r->next)->next; free(s); s=r; }
    else { r=r->next; s=s->next; }
    s=r=(*head3).next;
    return(r);
    }
    struct ma *mul(struct ma *head1,struct ma *head2)
    { struct ma *head3,*head4,*p,*q,*r,*s,*r1,*s1;
    struct ma *w[100];
    int t=0;
    head3=r=s=(struct ma*)malloc(sizeof(struct ma));
    head4=r1=s1=(struct ma*)malloc(sizeof(struct ma));
    p=head1->next; q=head2->next;
    while(p!=NULL)
    { s=(struct ma*)malloc(sizeof(struct ma));
    s->c=(p->c)*(q->c);
    s->e=(p->e)+(q->e);
    r->next=s;
    r=s;
    p=p->next;}
    s->next=NULL; s=r=head3->next; w[0]=head3;
    p=head1->next; q=q->next;
    while(q!=NULL)
    { while(p!=NULL)
    { s1=(struct ma*)malloc(sizeof(struct ma));
    s1->c=(p->c)*(q->c);s1->e=(p->e)+(q->e);
    r1->next=s1;
    r1=s1;
    p=p->next;
    }
    s1->next=NULL;r1=s1=head4->next;
    t++;
    w[t]=(struct ma*)malloc(sizeof(struct ma));
    w[t]->next=add(w[t-1],head4,'+');
    while(head3->next!=NULL) {r=r->next;free(s);head3->next=s=r;}
    s=r=head3;
    while(head4->next!=NULL) {r1=r1->next;free(s1);head4->next=s1=r1;}
    s1=r1=head4;
    p=head1->next; q=q->next;
    }
    return(w[t]->next);}
    void tc(struct ma *p,struct ma *q, struct ma *r)
    { char ch1,ch2;
    float x;
    float qzhi(struct ma *head,float x);
    void qdao();
    printf("\n請(qǐng)選擇多項(xiàng)式的序號(hào):");
    printf("\n 1--第一個(gè);2--第二個(gè);3--第三個(gè)");
    ch1=bioskey(0);printf("[%c]",ch1);
    printf("\n請(qǐng)輸入功能代碼:");
    printf("\n z--求函數(shù)值,d--求導(dǎo)函數(shù)");
    ch2=bioskey(0);printf("[%c]",ch2);
    switch(ch1)
    { case '1': { if(ch2=='z')
    { printf("\n請(qǐng)輸入自變量 x:");
    scanf("%f",&x);
    printf("多項(xiàng)式的值為:%10.4f",qzhi(p,x));}
    if(ch2=='d') qdao(p); break;}
    case '2': { if(ch2=='z')
    { printf("\n請(qǐng)輸入自變量 x:");
    scanf("%f",&x);
    printf("多項(xiàng)式的值為:%10.4f",qzhi(q,x));}
    if(ch2=='d') qdao(q); break;}
    case '3': { if(ch2=='z')
    { printf("\n請(qǐng)輸入自變量 x:");
    scanf("%f",&x);
    printf("多項(xiàng)式的值為:%10.4f",qzhi(r,x));}
    if(ch2=='d') qdao(r); break;} } }
    float qzhi(struct ma *head,float x)
    { float value=0;
    while(head!=NULL)
    { value+=(head->c)*pow(x,head->e);head=head->next;}
    return(value);
    }
    void qdao(struct ma *head)
    { struct ma *p,*q,*r,*t;
    p=head;
    t=r=q=(struct ma *)malloc(sizeof(struct ma));
    while(p!=NULL)
    { q=(struct ma *)malloc(sizeof(struct ma));
    r->next=q;r=q;
    q->c=(p->c)*((p->e));q->e=(p->e)-1;
    p=p->next;}
    q->next=NULL;
    printf("\n多項(xiàng)式的導(dǎo)數(shù)為:");
    output(t->next);
    p=t;
    while(t!=NULL) { p=t->next;free(t);t=p;}
    }
    void output(struct ma *r)
    { int l=0;
    struct ma *t,*s,*head;
    t=r;
    while(r->next!=NULL)
    if(r->e==(r->next)->e)
    { r->c+=(r->next)->c; s=r->next;
    r->next=(r->next)->next; free(s); s=r; }
    else { r=r->next; s=s->next; }
    s=r=t;
    head=(struct ma*)malloc(sizeof(struct ma));
    head->next=r;
    s=head;
    while(r!=NULL)
    { if((r->c)==0) {s->next=r->next; free(r);r=s->next;}
    else { s=s->next;r=r->next;}}
    s=r=head->next;
    while(r!=NULL)
    { if(r->c==1) { l++;
    if(r->e==1) printf("x");
    else if(r->ee);
    else if(r->e==0) printf("1");
    else printf("x^%.0f",r->e);}
    else if(r->c==-1) { l++;
    if(r->e==1) printf("-x");
    else if(r->ee);
    else if(r->e==0) printf("-1");
    else printf("-x^%.0f",r->e);}
    else { l++;
    if(r->e==1) printf("%4.1fx",r->c);
    else if(r->ec,r->e);
    else if(r->e==0) printf("%4.1f",r->c);
    else printf("%4.1fx^%.0f",r->c,r->e); }
    if(((*r).next!=NULL)&&((r->next)->c>0)) printf(" +");
    r=r->next; }
    if(l==0) printf("0");
    }
    我來回答
    類似推薦
    請(qǐng)使用1024x768 IE6.0或更高版本瀏覽器瀏覽本站點(diǎn),以保證最佳閱讀效果。本頁提供作業(yè)小助手,一起搜作業(yè)以及作業(yè)好幫手最新版!
    版權(quán)所有 CopyRight © 2012-2024 作業(yè)小助手 All Rights Reserved. 手機(jī)版