这是一个链表的应用题目,写这个的时候加减运算很好写,乘法还是废了一定时间的,因为忘记修改,导致了一系列错误,最终终于成型了。就是期间q刚开始一直忘记给他赋值,因而只有LA第一个与LB的所有选项的乘积。
- #include<stdio.h>
- #include<stdlib.h>
- #define TRUE 1
- #define FALSE 0
- typedef struct node
- {
- int exp;
- int coef;
- struct node *next;
- }Node,*LinkNode;
- void Init(LinkNode *L)
- {
- *L=(LinkNode)malloc(sizeof(Node));
- (*L)->next=NULL;
- }
- void meun()
- {
- printf("输入您想要的算数运算\n");
- printf("1:相加\n");
- printf("2:相减\n");
- printf("3:相乘\n");
- }
- void Polycreate(LinkNode L)
- {
- Node *r,*s;
- int c,e;
- r=L;
- printf("please input the coef and exp\n");
- scanf("%d,%d",&c,&e);
- while(c!=0)
- {
- s=(Node *)malloc(sizeof(Node));
- s->coef=c;
- s->exp=e;
- r->next=s;
- r=s;
- scanf("%d,%d",&c,&e);
- }
- r->next=NULL;
- }
- void Polyadd(LinkNode la,LinkNode lb)
- {
- Node *p,*q,*tail,*temp;
- int sum;
- p=la->next;
- q=lb->next;
- tail=la;
- while(p!=NULL&&q!=NULL)
- {
- if(p->exp==q->exp)
- {
- sum=p->coef+q->coef;
- if(sum!=0)
- {
- p->coef=sum;
- tail->next=p;
- tail=p;
- p=p->next;
- temp=q;
- q=q->next;
- free(temp);
- }
- else
- {
- temp=p;
- p=p->next;
- free(temp);
- temp=q;
- q=q->next;
- free(temp);
- }
- }
- else if(p->exp<q->exp)
- {
- tail->next=p;
- tail=p;
- p=p->next;
- }
- else
- {
- tail->next=q;
- tail=q;
- q=q->next;
- }
- }
- if(p!=NULL)
- tail->next=p;
- else
- tail->next=q;
- }
- void Polysub(LinkNode la,LinkNode lb)
- {
- Node *p,*q,*tail,*temp;
- int sub;
- p=la->next;
- q=lb->next;
- tail=la;
- while(p!=NULL&&q!=NULL)
- {
- if(p->exp<q->exp)
- {
- tail->next=p;
- tail=tail->next;
- p=p->next;
- }
- else if(p->exp==q->exp)
- {
- if(p->coef<q->coef)
- sub=q->coef-p->coef;
- else
- sub=p->coef-q->coef;
- if(sub)
- {
- if(p->coef<q->coef)
- p->coef=-sub;
- else
- p->coef=sub;
- tail->next=p;
- tail=tail->next;
- p=p->next;
- temp=q;
- q=q->next;
- free(temp);
- }
- else
- {
- temp=p;
- p=p->next;
- free(p);
- temp=q;
- q=q->next;
- free(q);
- }
- }
- else
- {
- tail->next=q;
- tail=q;
- q=q->next;
- }
- }
- if(p)
- tail->next=p;
- else
- tail->next=q;
- }
- void Polytimes(LinkNode la,LinkNode lb)
- {
- Node *p,*q,*tail,*temp,*s;
- int times,niem;
- p=la->next;
- tail=la;
- while(p!=NULL)
- {
- q=lb->next;
- for(q;q!=NULL; )
- {
- s=(Node *)malloc(sizeof(Node));
- times=(p->coef)*(q->coef);
- niem=(p->exp)+(q->exp);
- s->coef=times;
- s->exp=niem;
- times=0;
- niem=0;
- tail->next=s;
- tail=s;
- // p=p->next;
- // temp=q;
- q=q->next;
- // free(temp);
- }
- p=p->next;
- }
- }
- void Print(LinkNode L)
- {
- Node *p;
- p=L->next;
- while(p)
- {
- printf("%d %d ",p->coef,p->exp);
- p=p->next;
- }
- printf("\n");
- }
- int main()
- {
- LinkNode la,lb;
- char c;
- int a;
- Init(&la);
- Init(&lb);
- printf("请输入第一个一元多项式:");
- Polycreate(la);
- printf("la的结果是:");
- Print(la);
- printf("请输入第二个一元多项式:");
- Polycreate(lb);
- printf("lb的结果是:");
- Print(lb);
- meun();
- scanf("%d",&a);
- switch(a)
- {
- case 1 :printf("请让他们相加!\n");
- Polyadd(la,lb);
- printf("相加后的结果是:");
- Print(la);
- printf("请按任意键返回!");
- break;
- case 2: printf("请让他们相减!\n");
- Polysub(la,lb);
- printf("相减的结果是:");
- Print(la);
- break;
- case 3: printf("请让他们相乘:");
- Polytimes(la,lb);
- printf("相乘后的结果是:");
- Print(la);
- break;
- default:
- printf("输入错误");
- break;
- }
- }
阅读(4789) | 评论(0) | 转发(0) |