一元多项式的加减 c语言链表实现

1.题目
实现一元多项式的加减法运算,要求多项式采用链表存储结构。
2.测试用例
(1)a(x)=3x^1000 +7x^3-2x+1
b(x)=x^99 -x^3+2x+8
加法运算结果:
c(x)=9.00 +6.00x^3 +1.00x^99 +3.00x^1000
减法运算结果:
d(x)=-7.00 -4.00x+8.00x^3 -1.00x99+3.00x1000
(2)a(x)= 3x^5 +7x^3+1
b(x)=9x^6 -7x3+4x2+5x-1
加法运算结果:
c(x)= 5.00x+4.00x^2 +3.00x^5 +9.00x^6
减法运算结果:
d(x)=2.00 -5.00x-4.00x^2 +14.00x^3 +3.00x^5 -9.00x^6
(3)a(x)= 3x^5 +7x^3+1
b(x)=-3x^5 -7x^3-1
加法运算结果:
c(x)=0
减法运算结果:
d(x)=2.00 +14.00x^3 +6.00x^5
(4)a(x)=0
b(x)=1
加法运算结果:
c(x)=1.00
减法运算结果:
d(x)=-1.00
(5)a(x)=x^4 +x^2+1
b(x)=x^5 +x^3 -x^2+1
加法运算结果:
c(x)=2.00 +1.00x^3 +1.00x^4 +1.00x^5
减法运算结果:
d(x)=2.00x^2 -1.00x^3 +1.00x^4 -1.00x^5
3算法描述
(1)一元多项式的存储
采用链表来储存多项式。考虑到多项式习惯用降幂排列,因此建立多项式时,进行一个排序。
(2)一元多项式的建立
建立一元多项式,分别输入每一项的系数和指数。
(3)一元多项式的加减法
两个多项式进行加减法运算时,建立一个新的链表,把结果存在里面要注意当系数之和或之差为0时,将结点删除。
4验证算法对测试用例的操作步骤
以a(x)=x^4 +x^2 +1、b(x)=x^5 +x^3- x^2+1的加法为例,说明算法操作的步骤。
1)建立链表,将多项式储存,链表中储存多项式系数和指数。
2)对多项式进行排序,按照升幂的次序将多项式中每一项进行排序。
3)分别输出两个多项式。
4)对两个多项式分别进行加法和减法运算。进行加减法运算时,若指数想相同,则将系数相加减,若指数不同,则新建立节点,储存数据,注意:当系数为0时,将该节点删除,即该项为0。
5)输出两个多项式加减法的运算结果。
建立第一个链表储存a(x),
系数(ratio) 指数(index) 地址(next)
1 4
1 2
1 0 NULL

建立第二个链表储存b(x),
系数(ratio) 指数(index) 地址(next)
1 5
1 3
-1 2
1 0 NULL

利用排序函数将两链表进行排序
排序结果如下
系数(ratio) 指数(index) 地址(next)
1 0
1 2
1 4 NULL
多项式a(x):
多项式b(x):
系数(ratio) 指数(index) 地址(next)
1 0
-1 2
1 3
1 5 NULL

对两多项式进行加减法运算:
加法运算的结果如下:
系数(ratio) 指数(index) 地址(next)
2 0
0 2
1 3
1 4
1 5 NULL

对于系数为0的项,要将其删除,结果如下:
系数(ratio) 指数(index) 地址(next)
2 0
1 3
1 4
1 5 NULL

减法运算的结果如下:
系数(ratio) 指数(index) 地址(next)
0 0
2 2
-1 3
1 4
-1 5 NULL

对于系数为0的项,要将其删除,结果如下:
系数(ratio) 指数(index) 地址(next)
2 2
-1 3
1 4
-1 5 NULL
得到加减法运算的结果,输出运算结果即可。

#include <stdio.h>
#include <stdlib.h>
static struct poly *head1,*head2;
struct poly
{int index;float ratio;struct poly *next;
};//建立链表,index储存指数,ratio储存系数
struct poly *creat()
{struct poly *temp,*head,*rear,*t;int index;float ratio;int flag=0;head=rear=malloc(sizeof(struct poly));rear->next=NULL;printf("请输入多项式的各项的系数与指数,以空格隔开,输入0 0代表输入结束\n");while(1){scanf("%f %d",&ratio,&index);if(ratio==0&&index==0)return head;if(head->next==NULL){temp=malloc(sizeof(struct poly));rear->next=temp;rear=temp;rear->next=NULL;rear->index=index;rear->ratio=ratio;continue;}for(t=head->next; t!=NULL; t=t->next){if(t->index==index){flag=1;t->ratio=t->ratio+ratio;break;}}if(flag==0){temp=malloc(sizeof(struct poly));rear->next=temp;rear=temp;rear->next=NULL;rear->index=index;rear->ratio=ratio;}}return head;
};//输入数据,在输入时,可能存在输入两次相同系数的数据,此时将两组数据合并
struct poly sort(struct poly *head)
{struct poly *temp1,*temp2;temp1=head->next;temp2=NULL;while(temp1!=temp2){while(temp1->next!=temp2){if(temp1->index>temp1->next->index){temp1->index=temp1->index+temp1->next->index;temp1->next->index=temp1->index-temp1->next->index;temp1->index=temp1->index-temp1->next->index;temp1->ratio=temp1->ratio+temp1->next->ratio;temp1->next->ratio=temp1->ratio-temp1->next->ratio;temp1->ratio=temp1->ratio-temp1->next->ratio;}temp1=temp1->next;}temp2=temp1;temp1=head->next;}
};//将多项式进行排序,以符合我们通常状态下升幂的次序
struct poly add(struct poly *head1,struct poly *head2)
{struct poly *head,*rear,*temp1,*p,*q;head=rear=malloc(sizeof (struct poly));rear->next=NULL;p=head1->next;q=head2->next;while(p!=NULL||q!=NULL){if(p==NULL||q==NULL){if(p==NULL){while(q!=NULL){temp1=malloc(sizeof(struct poly));rear->next=temp1;temp1->next=NULL;temp1->index=q->index;temp1->ratio=q->ratio;rear=temp1;q=q->next;}}//求和时,若head1为空,则直接将head2复制到新结果中去else if(q==NULL){while(p!=NULL){temp1=malloc(sizeof(struct poly));rear->next=temp1;temp1->next=NULL;temp1->index=p->index;temp1->ratio=p->ratio;rear=temp1;p=p->next;}}//若head2为空,则将head1复制到结果中去}else{if(p->index<q->index){temp1=malloc(sizeof(struct poly));temp1->index=p->index;temp1->ratio=p->ratio;rear->next=temp1;rear=temp1;rear->next=NULL;p=p->next;}else if(p->index>q->index){temp1=malloc(sizeof(struct poly));temp1->index=q->index;temp1->ratio=q->ratio;rear->next=temp1;rear=temp1;rear->next=NULL;q=q->next;}else{temp1=malloc(sizeof(struct poly));temp1->index=p->index;temp1->ratio=q->ratio+p->ratio;if(temp1->ratio!=0){rear->next=temp1;rear=temp1;rear->next=NULL;p=p->next;q=q->next;}else{p=p->next;q=q->next;}}}//若head1和head2都不为空,则按照升幂的次序将其排序相加,系数为0的项不需要,注意,一定要按照次序来,少使用排序函数}print(head);
};//对两多项式进行求和,求和结果储存在另一个结构体中
struct poly sub(struct poly *head1,struct poly *head2)
{struct poly *head,*rear,*temp1,*temp2,*p,*q;head=rear=malloc(sizeof (struct poly));head->next=rear->next=NULL;p=head1->next;q=head2->next;while(p!=NULL||q!=NULL){if(p==NULL||q==NULL){if(p==NULL){while(q!=NULL){temp1=malloc(sizeof(struct poly));rear->next=temp1;temp1->next=NULL;temp1->index=q->index;temp1->ratio=-q->ratio;rear=temp1;q=q->next;}}else if(q==NULL){while(p!=NULL){temp1=malloc(sizeof(struct poly));rear->next=temp1;temp1->next=NULL;temp1->index=p->index;temp1->ratio=p->ratio;rear=temp1;p=p->next;}}}else{if(p->index<q->index){temp1=malloc(sizeof(struct poly));temp1->index=p->index;temp1->ratio=p->ratio;rear->next=temp1;rear=temp1;rear->next=NULL;p=p->next;}else if(p->index>q->index){temp1=malloc(sizeof(struct poly));temp1->index=q->index;temp1->ratio=-q->ratio;rear->next=temp1;rear=temp1;rear->next=NULL;q=q->next;}else{temp1=malloc(sizeof(struct poly));temp1->index=p->index;temp1->ratio=p->ratio-q->ratio;if(temp1->ratio!=0){rear->next=temp1;rear=temp1;rear->next=NULL;p=p->next;q=q->next;}else{p=p->next;q=q->next;}}}}print(head);
};//多项式求差,与求和同理
void print(struct poly *head)
{struct poly *temp;temp=head->next;if(temp==NULL){printf("0\n");return;}//多项式的输出,若多项式为NULL则直接输出0while(temp!=NULL){if(temp==head->next){printf(" %.2f",temp->ratio);if(temp->index!=0){if(temp->index==1)printf("x");elseprintf("x^%d",temp->index);}elseprintf(" ");}//在输出第一项时,系数为正,直接输出,系数为负,也直接输出else{if(temp->ratio>0){printf("+%.2f",temp->ratio);if(temp->index!=0){if(temp->index==1)printf("x");elseprintf("x^%d",temp->index);}elseprintf(" ");}else{printf("%.2f",temp->ratio);if(temp->index!=0){if(temp->index==1)printf("x");elseprintf("x^%d",temp->index);}elseprintf(" ");}//之后的每一项中,系数为正,则加上正号,即加号,系数为负,可直接输出}temp=temp->next;}printf("\n");};int main()
{head1=creat();head2=creat();sort(head1);sort(head2);printf("\n多项式a(x)为:\n");print(head1);printf("\n多项式b(x)为:\n");print(head2);printf("\n两多项式之和为:\n");add(head1,head2);printf("\n两多项式之差为:\n");sub(head1,head2);struct poly *temp;temp=head1;while(temp!=NULL){free(temp);temp=temp->next;}temp=head2;while(temp!=NULL){free(temp);temp=temp->next;}//释放空间return 0;
}

这是我自己写出来的代码,我也是正在学习的学生,供大家参考,若有什么问题,希望可以给我指出,谢谢!

一元多项式的加减 c语言链表实现相关推荐

  1. c语言字符串加减_C语言中指针的介绍

    C语言中指针的介绍 指针是C语言中广泛使用的一种数据类型. 运用指针编程是C语言最主要的风格之一.利用指针变量可以表示各种数据结构:能很方便地使用数组和字符串: 并能象汇编语言一样处理内存地址,从而编 ...

  2. 补码加减c语言,C语言计算器含二进制数加减原反补码(自己的期末作业)

    用C++6.0编写的计算器,内含整数小数加减乘除.二进制数组加减及求原码补码反码! #include #include int main() { void top(); void no1(),no2( ...

  3. c语言实现补码加减,C语言计算器含二进制数加减原反补码(自己的期末作业)

    用C++6.0编写的计算器,内含整数小数加减乘除.二进制数组加减及求原码补码反码! #include #include int main() { void top(); void no1(),no2( ...

  4. C语言实现一元多项式的加减运算

    #include <stdio.h> #include <stdlib.h> #include <malloc.h> struct Node { float coe ...

  5. 一元多项式的加减以及求导

    采用链式存储结构,将两个线性链表表示的一元多项式相加减,求导并输出. #include<stdio.h> #include<stdlib.h>  typedef struct ...

  6. c语言实现补码加减,C语言实现用位移运算符进行加减乘…

    最近,在百度知道上回答问题,然后看见有的人问如何用位移运算符去进行加减乘除运算,于是巩固今天就在这总结一下. 先讲讲总体思路: 加法运算:将一个整数用二进制表示,其加法运算就是:相异(^)时,本位为1 ...

  7. C语言算小数加减,C语言带小数加减乘除.doc

    C语言带小数加减乘除 /*Desgined by doBell-ConG*/ /*Function:simply add, subtract, multiply and divide*/ #inclu ...

  8. 一元多项式加减乘实现c/c++

    一.实验题目:  一元多项式简单的计算器 1.主要功能: (1)输入并建立多项式: (2)输出多项式: (3)两个多项式相加,建立并输出和多项式: (4)两个多项式相减,建立并输出差多项式. (5)算 ...

  9. 数据结构(C语言)多项式加减

    这是大二刚开始数据结构的平时作业,上次随便发了,没来得及写标题和内容.题目要求用链表来实现多项式的加减,其实就是设置一个两个指针进行遍历,在代码段里有一些注解,要是有不太清楚的地方可以在下面留言或是私 ...

最新文章

  1. 中值定理符号怎么读_微分、微分中值定理、泰勒公式
  2. memcached安装、使用
  3. Objective-C语法之Object对象的那些事儿(五)
  4. call和apply和bind的区别
  5. linux下覆盖文件命令,在Linux中使用命令行进行文件覆盖的操作
  6. 要不要引入新技术?先思考这几个问题
  7. 安装centos7步骤_Centos7下源码编译安装mysql5.7 详细步骤 小白也能安装
  8. ExtJs学习笔记(2)_Basic GridPanel[基本网格]
  9. Whctf 2017 -UNTITLED- Writeup
  10. Delphi读写UTF-8、Unicode格式文本文件
  11. 入门highchart 第一天—— 环形图
  12. 吴恩达机器学习作业代码(python)
  13. hdu 1598 find the most comfortable road 枚举+最小成生树 kruskal 解题报告
  14. 在鹅厂工作1到11年的程序媛
  15. Ubuntu18.04局域网共享文件夹,实现win7和Ubuntu本地访问
  16. Yan LeCun会是AI界的居里夫人吗?
  17. C++ | 动态分配内存 new和malloc的区别
  18. 推荐一款可以把VCD光盘转换成ISO文件的工具
  19. 你知道界面编程吗?一文带你了解界面编程【Java养成】
  20. 汉芯核心成员爆料:陈进与台湾公司秘密交易

热门文章

  1. Java8的一些常用新特性
  2. CS5801AN是一个HDMI2.0b到DP1.4a转换器
  3. Protocal Buffers
  4. 小小白对SPI的理解,基于米联客例程的FPGA实现
  5. 荒岛求生游戏显示服务器不行,荒岛求生一直进不去 无法进入游戏解决方法
  6. ie打开自动切换到ie5了_您会切换到IE8吗?
  7. css实现纹理条纹,波点背景效果
  8. java中integer的范围_java中integer取值范围是什么
  9. C语言 产生随机数rand()
  10. html5新标签 aside,Html5新标签之aside标签详解