一元多项式的加法C++
2015-03-05 14:29
218 查看
#include<iostream.h>
#include<stdlib.h>
typedef struct term{
int z; //指数
float x; //系数
struct term *next;
}term;
struct term *creatlink(); //创建多项式
int cmp(int a,int b); //指数比较
struct term * addin(struct term *a,struct term *b); //加法运算
void list(struct term *a);
void main(){
struct term *A=creatlink();
list(A);
struct term *B=creatlink();
list(B);
struct term * C=addin(A,B);
cout<<endl<<"加法运算后:"<<endl;
list(C);
}
void list(struct term *a){
struct term *aa;
aa=a->next;
while(aa){
cout<<aa->x<<"X"<<aa->z;
if(aa->next!=0)
cout<<"+";
aa=aa->next;
}
}
struct term * addin(struct term *a,struct term *b){ //加法
struct term *ahead;
struct term *bhead;
struct term *aa;
struct term *bb;
ahead=a; bhead=b;
aa=ahead->next; bb=bhead->next;
while(aa&&bb){
int result=cmp(aa->z,bb->z);
switch(result){
case 2:ahead=aa;aa=aa->next;break; //指数小
case 1: bhead->next=bb->next;ahead->next=bb;bb->next=aa;ahead=ahead->next;bb=bhead->next;break;
case 0: aa->x+=bb->x;
if(aa->x!=0)
{ahead=aa;}
else
{ahead->next=aa->next;free(aa);}
bhead->next=bb->next;free(bb);bb=bhead->next;aa=ahead->next;break;
}
}
if(bb)
{while(bb)
{bhead->next=bb->next;ahead->next=bb;ahead=bb;bb=bhead->next;}
free(bhead);}
if(aa)
{while(aa)
{ahead=aa;aa=aa->next;}
}
ahead->next=NULL;
return a;
}
int cmp(int a,int b){
if(a==b)
return 0;
if(a>b)
return 1;
else
return 2;
}
struct term *creatlink(){ //创建多项式
struct term *p;
struct term *head;
struct term *q;
p=(struct term *)malloc(sizeof(struct term));
p->z=-1;
p->x=0;
p->next=NULL;
head=p;
cout<<endl<<"请输入多项式的项数:"<<endl;
int number0;
cin>>number0;
for(int i=1;i<=number0;i++){
q=(struct term *)malloc(sizeof(struct term));
cout<<"请输入第"<<i<<"项的系数和指数:"<<endl;
int n;
float m;
cin>>m>>n;
q->x=m;
q->z=n;
p->next=q;
p=q;
}
p->next=NULL;
return head;
}
#include<stdlib.h>
typedef struct term{
int z; //指数
float x; //系数
struct term *next;
}term;
struct term *creatlink(); //创建多项式
int cmp(int a,int b); //指数比较
struct term * addin(struct term *a,struct term *b); //加法运算
void list(struct term *a);
void main(){
struct term *A=creatlink();
list(A);
struct term *B=creatlink();
list(B);
struct term * C=addin(A,B);
cout<<endl<<"加法运算后:"<<endl;
list(C);
}
void list(struct term *a){
struct term *aa;
aa=a->next;
while(aa){
cout<<aa->x<<"X"<<aa->z;
if(aa->next!=0)
cout<<"+";
aa=aa->next;
}
}
struct term * addin(struct term *a,struct term *b){ //加法
struct term *ahead;
struct term *bhead;
struct term *aa;
struct term *bb;
ahead=a; bhead=b;
aa=ahead->next; bb=bhead->next;
while(aa&&bb){
int result=cmp(aa->z,bb->z);
switch(result){
case 2:ahead=aa;aa=aa->next;break; //指数小
case 1: bhead->next=bb->next;ahead->next=bb;bb->next=aa;ahead=ahead->next;bb=bhead->next;break;
case 0: aa->x+=bb->x;
if(aa->x!=0)
{ahead=aa;}
else
{ahead->next=aa->next;free(aa);}
bhead->next=bb->next;free(bb);bb=bhead->next;aa=ahead->next;break;
}
}
if(bb)
{while(bb)
{bhead->next=bb->next;ahead->next=bb;ahead=bb;bb=bhead->next;}
free(bhead);}
if(aa)
{while(aa)
{ahead=aa;aa=aa->next;}
}
ahead->next=NULL;
return a;
}
int cmp(int a,int b){
if(a==b)
return 0;
if(a>b)
return 1;
else
return 2;
}
struct term *creatlink(){ //创建多项式
struct term *p;
struct term *head;
struct term *q;
p=(struct term *)malloc(sizeof(struct term));
p->z=-1;
p->x=0;
p->next=NULL;
head=p;
cout<<endl<<"请输入多项式的项数:"<<endl;
int number0;
cin>>number0;
for(int i=1;i<=number0;i++){
q=(struct term *)malloc(sizeof(struct term));
cout<<"请输入第"<<i<<"项的系数和指数:"<<endl;
int n;
float m;
cin>>m>>n;
q->x=m;
q->z=n;
p->next=q;
p=q;
}
p->next=NULL;
return head;
}
相关文章推荐
- C++ 02-线性结构2 一元多项式的乘法与加法运算
- PAT 一元多项式的乘法与加法运算(链表 c++版)
- c++实现链式存储结构实现一元多项式的加法运算
- 【数据结构】C++链表实现一元多项式的加法
- PAT 一元多项式的乘法与加法运算(链表 c++版)
- C++实现多项式加法
- 浙大PAT 3-04. 一元多项式的乘法与加法运算 (解题思路)
- 实习一 线性表及其应用 (题目:一元稀疏多项式的加法运算 )
- 一元多项式加法
- 我用c++写的一个多项式的处理器的核心部分和输出部分。实现多项式的加法,减法,乘法,除法,及其求余。
- 一元稀疏多项式加法运算
- C++版数据结构课程设计(一元多项式)
- 【C++】PAT(basic level)1010. 一元多项式求导 (25)
- 数据结构的应用——一元N次多项式的加法
- PAT 3-04 一元多项式的乘法与加法运算(C语言实现)
- 多项式的加法与乘法(C++实现)
- PAT3-04. 一元多项式的乘法与加法运算
- 数据结构课程设计:顺序结构、动态链表结构下的一元多项式的加法、减法、乘法的实现
- 浙江大学PAT上机题解析之3-04. 一元多项式的乘法与加法运算
- 用单链表实现一元多项式相加 C++代码