一元多项式计算器(链式结构)
Posted fulianzhou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一元多项式计算器(链式结构)相关的知识,希望对你有一定的参考价值。
//*一元多项式计算器(链式结构)
/*问题描述:
设有一元多项式Am(x) 和Bn(x).
Am(x) = A0+A1x1+A2x2+A3x3+… +Amxm
Bn(x) = B0+B1x1+B2x2+B3x3+… +Bnxn
试求M(x)= Am(x)+Bn(x)、M(x)= Am(x)-Bn(x)和M(x)= Am(x)×Bn(x)。
基本要求:
⑴首先判定多项式是否稀疏;
⑵分别采用顺序和链式结构实现;
⑶结果M(x)中无重复阶项和无零系数项;
⑷要求输出结果的升幂和降幂两种排列情况。
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 30
typedef int Status;
typedef struct
int coef; //系数
int expo; //指数
ElemType;
typedef struct LNode
ElemType data;
struct LNode *next;
LNode,*LinkList;
typedef LinkList Polynomial;
//初始化
Status InitPolynomial(Polynomial &P)
P=(Polynomial)malloc(sizeof(LNode));
P->next=NULL;
P->data.coef=0;
P->data.expo=0;
return OK;
//长度
int PolynomialLength(Polynomial P)
int l=0;
while(P->next!=NULL)
l++;
P=P->next;
return l;
//输出一个多项式
Status PrintPolynomial(Polynomial P)
bool first=1;
while(P->next!=NULL)
P=P->next;
if(first) first=0;
else printf("+ ");
printf("%dX%d",P->data.coef,P->data.expo);
printf("\\n");
return OK;
//添加
Status AddNode(Polynomial &P,ElemType &e)
Polynomial s = P;
while(s->next!=NULL) s=s->next;
Polynomial Pnew=(Polynomial)malloc(sizeof(LNode));
Pnew->next=NULL;
Pnew->data=e;
s->next=Pnew;
return OK;
//追加
Status SupperaddNode(Polynomial &P,ElemType e)
Polynomial pp=P;
while(pp->next!=NULL) pp=pp->next;
Polynomial b=(Polynomial)malloc(sizeof(LNode));
b->data=e;
b->next=NULL;
pp->next=b;
return OK;
//删除结点
Status DeletePolynomial(Polynomial &P,int n)
int i=1,l=PolynomialLength(P);
Polynomial Pa=P;
if(n>l)
printf("n=%d,l=%d\\n",n,l);
printf("Error!\\n");
return ERROR;
while(Pa->next!=NULL && i++!=n) Pa=Pa->next;
Polynomial s = Pa->next;
Pa->next=s->next;
printf("Delet:%dX%d\\n",s->data.coef,s->data.expo);
free(s);
return OK;
//创建
Status CreatePolynomial(Polynomial &Pa)
printf("____________ 输入为 0 0 时结束 __________\\n");
for(int i=0;;i++)
printf("\\t输入系数和指数:");
int Coeff,Expon;
ElemType e;
scanf("%d%d",&Coeff,&Expon);
if(!Coeff && !Expon) break;
if(!Coeff) continue;
e.coef=Coeff; e.expo=Expon;
SupperaddNode(Pa,e);
return OK;
//调整(类似插入排序)
//成功诀窍:细心细心再细心,画图模拟,一边又一遍,纸上电脑上代码同时进行,相互比较
/********************************************/
Status Addjust(Polynomial &P,bool flag) //flag==1->小 , flag==0->大
Polynomial pd=P,pb=P;
while(pb->next!=NULL && pd->next!=NULL)
Polynomial pe=pd->next;
Polynomial pc=pb->next;
if(pc == pe || pd == pb)
pb=pb->next;
pd=P;
else if((flag == 0 && pc->data.expo < pe->data.expo) || (flag == 1 && pc->data.expo > pe->data.expo))
//将pc插入到pd后
//先处理pb
pb->next=pc->next;
//再处理pc
pc->next=pd->next;
//再处理pd
pd->next=pc;
pd=P;
else if(pc->data.expo == pe->data.expo)
//如果相等就相加
pe->data.coef+=pc->data.coef;
//删除pc所指的结点
Polynomial t1=pc;
pb->next=t1->next;
pc=pb->next;
free(t1);
//如果相加为零,删除该点
if(pe->data.coef == 0)
Polynomial t2=pe;
pd->next=pe->next;
free(t2);
pe=pd->next;
pd=P;
else if((flag == 0 && pc->data.expo > pe->data.expo) || (flag == 1 && pc->data.expo < pe->data.expo))
pd=pd->next;
PrintPolynomial(P);
return OK;
/********************************************/
//随机生成多项式
Status RandomCreate(Polynomial &P)
printf("\\n产生多少项:");
int n,m;
scanf("%d",&n);
printf("\\n输入 系数 和 指数 的 范围 m。将产生[-m,m]之间的系数和指数,输入m:");
scanf("%d",&m);
int M=m*2;
for(int i=0;i<n;i++)
ElemType e;
int C=rand()%M-m;
int E=rand()%M-m;
if(!C) i--; continue;
e.coef=C; e.expo=E;
SupperaddNode(P,e);
return OK;
//多项式相加,相加后的值给P1
Status Polynomial_add(Polynomial &P1,Polynomial &P2)
Polynomial p=P1;
printf("\\nA(x)=");
PrintPolynomial(P1);
printf("\\nB(x)=");
PrintPolynomial(P2);
while(p->next!=NULL) p=p->next;
p->next=P2;
Polynomial s=p->next;
p->next=s->next;
free(s);
printf("\\n由 M(x) = A(x) + B(x) \\n\\nM(x)=");
PrintPolynomial(P1);
printf("\\n调整后\\n\\nM(x)=");
Addjust(P1,0);
P1->next=NULL;
P2->next=NULL;
printf("\\n\\t\\t多项式A(x),B(x)已置空,请重新创建.\\n");
system("pause");
system("cls");
return OK;
//多项式相减 P1 - P2
//方法:P2系数全部取反,再相加
Status Polynomial_sub(Polynomial &P1,Polynomial &P2)
Polynomial p=P2;
printf("\\nM(x) = A(x) + ( - B(x) )\\n");
printf("\\nB(x)=");
PrintPolynomial(P2);
while(p->next!=NULL)
p=p->next;
p->data.coef=-p->data.coef;
printf("\\n-B(x)=");
PrintPolynomial(P2);
printf("\\n令 B(x) = - B(x)\\n");
system("pause");
Polynomial_add(P1,P2);
return OK;
//生成菜单
int Create_menu()
printf("\\t\\t 生成两个一元多项式\\n");
printf("\\t\\t_____________________\\n\\n");
printf("\\t\\t 1.手动输入. \\n\\n");
printf("\\t\\t 2.自动生成. \\n\\n");
printf("\\t\\t_____________________\\n");
printf("-----------请选择:");
char chi[5];
scanf("%s",chi);
return atoi(chi);
//生成多项式
Status Create(Polynomial &A,Polynomial &B)
int chiose=Create_menu();
if(chiose == 1)
printf("\\n建立多项式 A(x)\\n");
CreatePolynomial(A);
printf("\\nA(x)=");
PrintPolynomial(A);
system("pause");
printf("\\n建立多项式 B(x)\\n");
CreatePolynomial(B);
printf("\\nB(x)=");
PrintPolynomial(B);
else if(chiose == 2)
printf("_________________________\\n");
printf("\\n随机生成多项式 A(x) \\n");
RandomCreate(A);
printf("\\n已产生随机多项式.\\nA(x)=");
PrintPolynomial(A);
printf("\\n整理后\\nA(x)=");
Addjust(A,0);
system("pause");
printf("_________________________\\n");
printf("\\n随机生成多项式 B(x) \\n");
RandomCreate(B);
printf("\\n已产生随机多项式.\\nB(x)=");
PrintPolynomial(B);
printf("\\n整理后\\nB(x)=");
Addjust(B,0);
printf("_________________________\\n");
return OK;
//多项式升幂
Status Polynomial_ascend(Polynomial &A,Polynomial &B)
printf("________________________________\\n");
printf("\\n原来:\\nA(x)=");
PrintPolynomial(A);
printf("\\n调整后:\\n");
printf("A(x)=");
Addjust(A,0);
printf("\\n原来:\\nB(x)=");
PrintPolynomial(B);
printf("\\n调整后:\\n");
printf("B(x)=");
Addjust(B,0);
printf("________________________________\\n");
return OK;
//多项式降幂
Status Polynomial_decline(Polynomial &A,Polynomial &B)
printf("________________________________\\n");
printf("\\n原来:\\nA(x)=");
PrintPolynomial(A);
printf("\\n调整后:\\n");
printf("A(x)=");
Addjust(A,1);
printf("\\n原来:\\nB(x)=");
PrintPolynomial(B);
printf("\\n调整后:\\n");
printf("B(x)=");
Addjust(B,1);
printf("________________________________\\n");
return OK;
//乘法
Status Polynomial_multiply(Polynomial &A,Polynomial &B)
Polynomial M,pa=A;
InitPolynomial(M);
printf("乘法运算方法: \\nA(x)=A1(x)+A2(x)+....+An(x)\\n");
printf("\\nB(x)=B1(x)+B2(x)+....+Bn(x)\\n\\n");
printf("M(x)=B(x)*A1(x)+B(x)*A2(x)+....+B(x)*An(x)\\n");
system("pause");
int i=0;
while(pa->next!=NULL)
pa=pa->next;
ElemType Ea=pa->data;
Polynomial pb=B;
printf("\\nM%d(x) = A%d(x) * B(x)\\n",++i,i+1);
printf("\\nA%d(x)=%dX%d\\n",i,Ea.coef,Ea.expo);
int j=0;
printf("\\nB(x)=");
PrintPolynomial(B);
printf("\\n");
while(pb->next!=NULL)
pb=pb->next;
ElemType Eb=pb->data;
ElemType Em;
Em.coef=Ea.coef*Eb.coef;
Em.expo=Ea.expo+Eb.expo;
AddNode(M,Em);
printf("M%d(x)=",i);
Addjust(M,0);
system("pause");
printf("\\n运算结果为\\nM(x)=");
PrintPolynomial(M);
printf("\\n");
A->next=NULL;
B->next=NULL;
printf("\\n\\t\\t多项式A(x),B(x)已置空,请重新创建.\\n");
system("pause");
system("cls");
return OK;
//主菜单
int Main_menu()
printf("\\t\\t_____________________________________\\n");
printf("\\t\\t 主菜单 \\n");
printf("\\t\\t_____________________________________\\n");
printf("\\t\\t 1. 生成多项式 \\n");
printf("\\t\\t_____________________________________\\n");
printf("\\t\\t 2. 多项式升幂 \\n");
printf("\\t\\t_____________________________________\\n");
printf("\\t\\t 3. 多项式降幂 \\n");
printf("\\t\\t_____________________________________\\n");
printf("\\t\\t 4. M(x) = A(x) + B(x) \\n");
printf("\\t\\t_____________________________________\\n");
printf("\\t\\t 5. M(x) = A(x) - B(x) \\n");
printf("\\t\\t_____________________________________\\n");
printf("\\t\\t 6. M(x) = A(x) * B(x) \\n");
printf("\\t\\t_____________________________________\\n");
printf("\\t\\t 7. 退出 \\n");
printf("\\t\\t_____________________________________\\n");
printf("---------请选择:");
char select[5];
scanf("%s",select);
return atoi(select);
int main()
srand(time(0));
Polynomial A,B;
InitPolynomial(A);
InitPolynomial(B);
printf("\\t*********** 欢迎使用一元多项式计算器软件 ***********\\n");
while(true)
switch(Main_menu())
case 1:Create(A,B);break;
case 2:Polynomial_ascend(A,B);break;
case 3:Polynomial_decline(A,B);break;
case 4:Polynomial_add(A,B);break;
case 5:Polynomial_sub(A,B);break;
case 6:Polynomial_multiply(A,B);break;
case 7:printf("谢谢使用,再见!\\n");return 0;
default:printf("Error! Input Again!\\n");break;
system("pause");
return 0;
以上是关于一元多项式计算器(链式结构)的主要内容,如果未能解决你的问题,请参考以下文章