1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 习题3.6 一元多项式的乘法与加法运算 (20 分)(有测试点具体数据)c语言链表版本

习题3.6 一元多项式的乘法与加法运算 (20 分)(有测试点具体数据)c语言链表版本

时间:2020-11-11 15:54:22

相关推荐

习题3.6 一元多项式的乘法与加法运算 (20 分)(有测试点具体数据)c语言链表版本

习题3.6 一元多项式的乘法与加法运算 (20 分)

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2 6 1 -2 03 5 20 -7 4 3 1

结尾无空行

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 15 20 -4 4 -5 2 9 1 -2 0

结尾无空行

流程(注意点):①先建立两个链表存储两条方程

②进行乘法运算:1当相乘为0时候要删除 2当相乘后如果未知数x的次幂相等时候相加但是当相加后出现和的系数为0时候要清除 3要按照大小从大到小进行插入

③运算加法:将一式加入二式 1.注意当开头系数相等时候 2.开头系数相等且互为相反数时候 3.注意从大到小插入

④输出: 注意最后的一个数字不带空格,如果都为空时候输出 0 0

测试数据

如果有能力最好用函数写分成一块一块更为简洁

#include<stdio.h>#include<stdlib.h>struct list_unti{int xpow;int xishu;struct list_unti *next;};int main(){int n;struct list_unti *p,*head1=NULL,*head2=NULL,*tail1,*tail2;struct list_unti *head3=NULL,*tail3,*temp,*temp1;struct list_unti *p3,*p1,*p2;scanf("%d",&n); while(n>0){//先读入第一条方程 p=(struct list_unti*)malloc(sizeof(struct list_unti));scanf("%d %d",&p->xishu,&p->xpow);p->next=NULL;if(head1==NULL)head1=tail1=p;else {tail1->next=p;tail1=p;}n--;}scanf("%d",&n);//读入第二条方程 while(n>0){p=(struct list_unti*)malloc(sizeof(struct list_unti));scanf("%d %d",&p->xishu,&p->xpow);p->next=NULL;if(head2==NULL)head2=tail2=p;else {tail2->next=p;tail2=p;}n--;}//计算乘法 p1=head1;p2=head2;while(p1!=NULL){while(p2!=NULL){//运用goto是因为如果都是0相乘系数为0不需要读入 if(p2->xishu==0||p1->xishu==0) goto out;p=(struct list_unti*)malloc(sizeof(struct list_unti));p->xishu=p1->xishu*p2->xishu;p->xpow=p1->xpow+p2->xpow;//创建新节点 p->next=NULL;if(head3==NULL){ //新建开头 head3=tail3=p;}else { //链表插叙按照从大到小 p3=head3;if(p3->xpow<p->xpow){//如果大于头节点,则代替他成为新的头节点 p->next=p3;head3=p;}else {//如果小于头节点 while(p3->next&&p3->next->xpow>p->xpow){p3=p3->next;//遍历寻找出不小于的位置 } if(p3->next&&p3->next->xpow==p->xpow) {//如果有相同系数并入 p3->next->xishu+=p->xishu;free(p);if(p3->next->xishu==0){ //如果相加系数为0 要清除 temp1=p3->next->next;free(p3->next);p3->next=temp1;} }else { //如果是中间则插入 temp=p3->next;p3->next=p;p->next=temp;}}}out:p2=p2->next;//p2再次变成头节点 }p2=head2;p1=p1->next;}//计算加法 把第条方程加到第二条方程上 p1=head1;p2=head2; if(head2==NULL&&head1!=NULL) head2=head1;else if(head2!=NULL&&head1==NULL) ;else {while(p1!=NULL){temp=p1->next;if(p1->xpow>p2->xpow) { //如果开头就比另外条方程要大着插在头 p1->next=p2;head2=p1;}else if(p1->xpow==p2->xpow){//如果开头就相等时候 p2->xishu+=p1->xishu;if(p2->xishu==0) head2=p2->next;//开头相加为0时候 }else {//如果小于头节点插入其中,如果相等就并入然后free while(p2->next&&p2->next->xpow>p1->xpow) {p2=p2->next;}if(p2->next&&p2->next->xpow==p1->xpow){p2->next->xishu+=p1->xishu;if(p2->next->xishu==0){ //如果相加系数为0 就清除 temp1=p2->next->next;free(p2->next);p2->next=temp1;} free(p1);}else {temp1=p2->next;p2->next=p1;p1->next=temp1;}}p1=temp;p2=head2;}}//输出 乘法 if(head3==NULL) printf("0 0\n");//如果为空时候 else {//如果存在时候 while(head3->next!=NULL){//遍历输出,但不输出最后一项 printf("%d %d ",head3->xishu,head3->xpow);head3=head3->next;} printf("%d %d",head3->xishu,head3->xpow);//因为末尾没有空格 printf("\n");}//输出加法 if(head2==NULL) printf("0 0");//如果为空时候 else{//如果存在时候while(head2->next!=NULL){ //遍历输出,但不输出最后一项 printf("%d %d ",head2->xishu,head2->xpow);head2=head2->next;} printf("%d %d",head2->xishu,head2->xpow);}//因为末尾没有空格 return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。