如何在C++中用单链表实现多项式的加减乘等操作

最好在编译的时候可以直接看得到运算后的结果... 最好在编译的时候可以直接看得到运算后的结果 展开
 我来答
yangxiaoluck
2010-06-27 · 超过12用户采纳过TA的回答
知道答主
回答量:55
采纳率:0%
帮助的人:38.6万
展开全部
这是以前做的一个
运行了的
#include <stdio.h>
#include <malloc.h>
#include<iostream.h>
#include<string.h>

typedef struct LNode //多项式的存储结构定义
{
int coef;
int expn;
struct LNode *next;
}LNode,*polynomail;

void creatpolyn(polynomail &p,int m) //头插法创建多项式
{
int i;
int coef;
int expn;
polynomail s;
p=(polynomail)malloc(sizeof(LNode));
p->next=NULL;

for(i=1;i<=m;++i)
{
s=(polynomail)malloc(sizeof(LNode));
cout<<"请输入一元多项式的系数和指数:";
cin>>coef>>expn;
s->coef=coef;
s->expn=expn;
s->next=p->next;
p->next=s;
}
}

void printpolyn(polynomail p) //输出多项式单链表的元素值
{
p=p->next;
while(p!=NULL)
{
if(p->coef!=0)
cout<<p->coef<<"X^"<<p->expn<<"+ ";
p=p->next;
}
cout<<endl;
}

/*void SortPloy(polynomail L) //升幂排序
{
polynomail p=L->next,q,r;
if(p!=NULL)
{
r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r=p->next;
q=L;
while(q->next!=NULL&&q->next->expn<p->expn)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}*/

void SortPloy(polynomail L) //升幂排序
{
polynomail p=L->next,q=p->next;
int coef,expn;
while(p!=NULL&&p->next!=NULL)
{
while(q!=NULL)
{
if(p->expn>q->expn)
{
coef=p->coef;expn=p->expn;
p->coef=q->coef;p->expn=q->expn;
q->coef=coef;q->expn=expn;
}
q=q->next;
}
p=p->next;
q=p->next;
}
}

void polyvalue(polynomail p,int x0) //求多项式的值
{ int i,s=1,t,r=0;
p=p->next;
while(p!=NULL)
{
for(i=1;i<=p->expn;i++)
{ s=s*x0;
}
t=s*p->coef;
r+=t;
s=1;
p=p->next;
}
cout<<r<<endl;
}

polynomail AddPoly(polynomail pa,polynomail pb) /*求两个多项式的和*/
{
SortPloy(pa);
SortPloy(pb);
polynomail rn,p1=pa->next,p2=pb->next,p,tc,s;
rn=(polynomail)malloc(sizeof(LNode)); /*新建头结点*rn*/
rn->next=NULL; /*rn为新建单链表的头结点*/
tc=rn; /*tc始终指向新建单链表的最后结点*/
while (p1!=NULL && p2!=NULL)
{
if (p1->expn<p2->expn) /*将*p1结点复制到*s并链到rn尾*/
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p1->coef;s->expn=p1->expn;s->next=NULL;
tc->next=s;tc=s;
p1=p1->next;
}
else if (p1->expn>p2->expn) /*将*p2结点复制到*s并链到rn尾*/
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p2->coef;s->expn=p2->expn;s->next=NULL;
tc->next=s;tc=s;
p2=p2->next;
}
else /*p1->expn=p2->expn的情况*/
{

s=(polynomail)malloc(sizeof(LNode));
s->coef=p1->coef+p2->coef;s->expn=p1->expn;
s->next=NULL;
tc->next=s;tc=s;

p1=p1->next;p2=p2->next;
}
}
if (p1!=NULL) p=p1; /*将尚未扫描完的余下结点复制并链接到rn单链表之后*/
else p=p2;
while (p!=NULL)
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p->coef;s->expn=p->expn;s->next=NULL;
tc->next=s;tc=s;
p=p->next;
}
tc->next=NULL; /*新建单链表最后结点的next域置空*/
return rn;
}

polynomail DelPoly(polynomail pa,polynomail pb) /*求两个多项式的差*/
{ SortPloy(pa);
SortPloy(pb);
polynomail tn,p1=pa->next,p2=pb->next,p,tc,s;
tn=(polynomail)malloc(sizeof(LNode)); /*新建头结点*tn*/
tn->next=NULL; /*tn为新建单链表的头结点*/
tc=tn; /*tc始终指向新建单链表的最后结点*/
while (p1!=NULL && p2!=NULL)
{
if (p1->expn<p2->expn) /*将*p1结点复制到*s并链到tn尾*/
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p1->coef;s->expn=p1->expn;s->next=NULL;
tc->next=s;tc=s;
p1=p1->next;
}
else if (p1->expn>p2->expn) /*将*p2结点复制到*s并链到tn尾*/
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=-p2->coef;s->expn=p2->expn;s->next=NULL;
tc->next=s;tc=s;
p2=p2->next;
}
else /*p1->expn=p2->expn的情况*/
{

s=(polynomail)malloc(sizeof(LNode));
s->coef=p1->coef-p2->coef;s->expn=p1->expn;
s->next=NULL;
tc->next=s;tc=s;

p1=p1->next;p2=p2->next;
}
}
if (p1!=NULL) p=p1; /*将尚未扫描完的余下结点复制并链接到tn单链表之后*/
else
{ if(p2!=NULL)
{ s=(polynomail)malloc(sizeof(LNode));
s->coef=-p2->coef;s->expn=p2->expn;s->next=NULL;
tc->next=s;tc=s;
p2=p2->next;
p=p2;
}
else
{
p=NULL;
s->next=NULL;
tc->next=s;tc=s;
}

}
while (p!=NULL)
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p->coef;s->expn=p->expn;s->next=NULL;
tc->next=s;tc=s;
p=p->next;
}
tc->next=NULL; /*新建单链表最后结点的next域置空*/
return tn;
}

void save(polynomail p,int m) /*将多项式的系数和指数保存到外部文件中*/
{FILE *fp;
polynomail s;
char filename[10];
cout<<"请输入要保存的文件名,例如:c:\\score\n";
cin>>filename;
if((fp=fopen(filename,"w+"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("打不开文件Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\n保存中...Saving the file......\n");
s=(polynomail)malloc(sizeof(LNode));
s=p->next;
while(s!=NULL)
{
fprintf(fp,"%d,%d\n",s->coef,s->expn); /*写入一条记录*/
s=s->next;
}
fclose(fp); /*关闭文件*/
printf("保存成功....Save the file successfully!\n");
}

polynomail read(int m) /*从保存的外部文件中读取多项式的系数和指数*/
{ int i;
int coef,expn;
char filename[20];
polynomail s,p;
p=(polynomail)malloc(sizeof(LNode));
p->next=NULL;
cout<<"请输入要读取的文件名,例如:c:\\score\n";
cin>>filename;
FILE* fp;
fp=fopen(filename, "r+");
for(i=1;i<=m;i++)
{s=(polynomail)malloc(sizeof(LNode));
fscanf(fp, "%d,%d", &coef, &expn);
cout<<coef<<","<<expn<<endl;
s->coef=coef;
s->expn=expn;
s->next=p->next;
p->next=s;
}
printpolyn(p);
return p;
}

void main()
{ int a=0;
while(a!=8)
{cout<<"--------------------菜单选项--------------------------------------------\n";
cout<<"******** 1、创建一元多项式Pn(x)和Qn(x) *********************************\n";
cout<<"******** 2、输出一元多项式Pn(x)和Qn(x) *********************************\n";
cout<<"******** 3、对一元多项式Pn(x)和Qn(x)进行升幂排序 ***********************\n";
cout<<"******** 4、对一元多项式Pn(x)和Qn(x)进行求值 ***************************\n";
cout<<"******** 5、对一元多项式Pn(x)和Qn(x)进行求和、差 ***********************\n";
cout<<"******** 6、将一元多项式Pn(x)和Qn(x)的系数和指数保存到外部文件 *********\n";
cout<<"******** 7、读取外部文件并创建一元多项式Pn(x)和Qn(x) *******************\n";
cout<<"******** 8、退出菜单 ***************************************************\n";
cout<<"------------------------------------------------------------------------\n";
cout<<"请输入你要选择的菜单选项:";
cin>>a;
switch(a)
{case 1: int m,n,x0,x1;
polynomail Pn,Qn,Rn,Tn;
cout<<"请输入一元多项式Pn的项数:";
cin>>m;
creatpolyn(Pn,m);
cout<<"已创建一元多项式Pn\n";
cout<<"请输入一元多项式Qn的项数:";
cin>>n;
creatpolyn(Qn,n);
cout<<"已创建一元多项式Qn\n";
break;

case 2: cout<<"原一元多项式Pn:";
printpolyn(Pn);
cout<<"原一元多项式Qn:";
printpolyn(Qn);
cout<<endl;
break;

case 3: SortPloy(Pn);
SortPloy(Qn);
cout<<"排序后的一元多项式Pn:";
printpolyn(Pn);
cout<<"排序后的一元多项式Qn:";
printpolyn(Qn);
cout<<endl;
break;

case 4: cout<<"请输入一元多项式Pn中的x0的值:";
cin>>x0;
cout<<"一元多项式Pn(x0)的值:";
polyvalue(Pn,x0);
cout<<endl;
cout<<"请输入一元多项式Qn中的x1的值:";
cin>>x1;
cout<<"一元多项式Qn(x1)的值:";
polyvalue(Qn,x1);
cout<<endl;
break;

case 5: Rn=AddPoly(Pn,Qn);
cout<<"一元多项式Pn,Qn相加结果Rn:";
printpolyn(Rn);
cout<<endl;
Tn=DelPoly(Pn,Qn);
cout<<"一元多项式Pn,Qn相减结果Tn:";
printpolyn(Tn);
cout<<endl;
break;

case 6: save(Pn,m);
save(Qn,n);
break;

case 7: cout<<"请输入一元多项式Pn的项数:";
cin>>m;
Pn=read(m);
cout<<"请输入一元多项式Qn的项数:";
cin>>n;
Qn=read(n);
break;

case 8: break;

default: cout<<"没有该菜单选项,请重新选择\n";
break;

}
}
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
vbtraz
2010-06-22 · TA获得超过5532个赞
知道大有可为答主
回答量:4152
采纳率:0%
帮助的人:4437万
展开全部
跟大数运算基本上一回事
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式