一元多项式运算一.问题描述设计一个简单的一元稀疏多项式加法运算器.二.基本要求一元稀疏多项式简单计算器的基本功能包括:1

1个回答

  • #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想继续吗(求函数值/求导数)(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("请输入第一个多项式的项数:");

    scanf("%d",&num1);

    q=head1=(struct ma*)malloc(sizeof(struct ma));

    (*q).next=NULL;

    if(num1>0)

    { printf("请输入第一个多项式的系数(c) 和指数(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请选择多项式的序号:");

    printf("n 1--第一个;2--第二个;3--第三个");

    ch1=bioskey(0);printf("[%c]",ch1);

    printf("n请输入功能代码:");

    printf("n z--求函数值,d--求导函数");

    ch2=bioskey(0);printf("[%c]",ch2);

    switch(ch1)

    { case '1': { if(ch2=='z')

    { printf("n请输入自变量 x:");

    scanf("%f",&x);

    printf("多项式的值为:%10.4f",qzhi(p,x));}

    if(ch2=='d') qdao(p); break;}

    case '2': { if(ch2=='z')

    { printf("n请输入自变量 x:");

    scanf("%f",&x);

    printf("多项式的值为:%10.4f",qzhi(q,x));}

    if(ch2=='d') qdao(q); break;}

    case '3': { if(ch2=='z')

    { printf("n请输入自变量 x:");

    scanf("%f",&x);

    printf("多项式的值为:%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多项式的导数为:");

    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");

    }