请哪位高手指点啊,讲的尽量详细点谢谢
第一个多项式相加程序
#include<iostream.h>
class mploy //定义一个类
{
protected:
int coef; //多项式的系数
int exp; //多项式的指数
mploy *next; //下一个元素地址
public:
mploy *creat() //尾插法建立多项式链表
{
mploy *p,*s,*r;
p=r=new mploy; //申请 头结点
int x,y;
cout<<" 输入多项式的系数和指数(系数为0结束,指数从小到大)";
cin>>x>>y;
while(x)
{
s=new mploy;
s->coef=x;
s->exp=y;
r->next=s;
r=s;
cout<<" 输入多项式的系数和指数(系数为0结束,指数从小到大)";
cin>>x>>y;
}
r->next=NULL;
return p;
}
void print(mploy *h) //输出多项式
{
mploy *p=h->next;
//输出多项式第一个结点
if(p->coef!=0)
{
if(p->exp!=0)
cout<<p->coef<<"x**"<<p->exp;
else cout<<p->coef;
}
p=p->next;
while(p!=NULL) //输出多项式的其他结点
{
if(p->coef>0)
cout<<"+"; //如果为负,在输入数据时加" - "号
cout<<p->coef<<"x**"<<p->exp;
p=p->next;
}
cout<<endl;
}
//多项式相加
void ployadd(mploy *A,mploy *B)
{mploy *p,*q,*u,*pre;
int x;
p=A->next;
q=B->next;
while((p!=NULL)&&(q!=NULL))
if(p->exp<q->exp)
{pre=p;p=p->next;}
else if( p->exp==q->exp)
{x=p->coef+q->coef;
if(x!=0){p->coef=x;pre=p;}
else {pre->next=p->next;delete p;} //删除结点
p=pre->next;u=q;q=q->next;delete u;} //删除结点
else
{u=q->next;q->next=p;pre->next=q;
pre=q;
q=u;} //插入结点
if(q!=NULL) pre->next=q;}
};
void main()
{mploy *A,*B,c;
cout<<"建立第一个多项式链表"<<endl;
A=c.creat(); //建立第一个多项式链表
cout<<"建立第二个多项式链表"<<endl;
B=c.creat(); //建立第二个多项式链表
c.print(A); //输出第一个多项式
c.print(B); //输出第二个多项式
c.ployadd(A,B);
c.print(A); //输出合并后的多项式
第二个多项式相加程序
这是以前做的一个
运行了的
#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;
}
}
}
主函数看不明白为什么是mploy *A,*B,c;A B是指针但C不是 还有为什么是 A=c.creat(); B=c.creat();
为什么第二个多项式程序void creatpolyn(polynomail &p,int m) P前是地址符号i,但第一个程序里 void ployadd(mploy *A,mploy *B)
A,B前是指针我的C++基础差又该如何弥补求教啊