永发信息网

编程:数据结构,链表实现多项式乘法

答案:2  悬赏:60  手机版
解决时间 2021-05-04 17:59
  • 提问者网友:心牵心
  • 2021-05-04 10:24
编程:数据结构,链表实现多项式乘法
最佳答案
  • 五星知识达人网友:渊鱼
  • 2021-05-04 10:46
这东西要自己写,我这写好了,加qq 1085124834给你
全部回答
  • 1楼网友:渊鱼
  • 2021-05-04 11:06
这是我亲自写的,没有按照你定义的结构体和函数名来写,已经实现创建,相加,相乘,输出了,插入的话你自己完成吧 输入方法: 1 2 1 1 0 0 1 2 1 1 0 0 #include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct polynomial { int coef,ex; struct polynomial *next; }POLY; POLY *create_poly(void); POLY *add_poly(POLY *,POLY *); POLY *mul_poly(POLY *,POLY *); void print_poly(POLY *); int main(void) { POLY *heada=NULL,*headb=NULL,*headsum=NULL,*headmul=NULL; printf("create the first multinomial:\n"); heada=create_poly(); printf("create the second multinomial:\n"); headb=create_poly(); printf("The first multinomial:\n"); print_poly(heada); printf("The second multinomial:\n"); print_poly(headb); headsum=add_poly(heada,headb); printf("The sum multinomial:"); print_poly(headsum); headmul=mul_poly(heada, headb); printf("The mul multinomial:"); print_poly(headmul); return 0; } //创建 POLY *create_poly() { int iA=0,iE=0; POLY *head=NULL,*s=NULL,*r=NULL; head=(POLY *)malloc(sizeof(POLY)); r=head; printf("please input coef and exponent:\n"); scanf("%d%d",&iA,&iE); while(iA != 0) { s=(POLY *)malloc(sizeof(POLY)); s->coef=iA,s->ex=iE; r->next=s; r=s; scanf("%d%d",&iA,&iE); } r->next=NULL; return head; } //相加 POLY *add_poly(POLY *p1, POLY *p2) //多项式相加 { POLY *head, *tmp, *s; int value; p1=p1->next; p2=p2->next; head=tmp=(POLY*)malloc(sizeof(POLY)); head->next=NULL; while(p1 && p2) { if(p1->ex == p2->ex) { value=p1->coef+p2->coef; if(value != 0) { s=(POLY *)malloc(sizeof(POLY)); s->coef=value; s->ex=p1->ex; s->next=NULL;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯