主题:求:一元多项式的加减乘实验报告
eidolon01
[专家分:0] 发布于 2006-11-26 22:13:00
[em5]急需一元多项式的加减乘实验报告,希望哪位好心的高手能够帮一下忙,真的是太谢谢啦
回复列表 (共2个回复)
沙发
eidolon01 [专家分:0] 发布于 2006-11-27 08:33:00
想问一下,这个程序在编译的时候都没错误的,为什么在输入完计算的时候却出错了了呢?希望有人可以帮一下忙,谢谢啦先
#include<iostream.h>
#include<stdlib.h>
struct Polynomail{
float coef;
int expn;
struct Polynomail*next;
};
void CreatNode(Polynomail *p, float a, int b){
struct Polynomail *q=new struct Polynomail;
q->coef=a;
q->expn=b;
p->next=q;
}
void CreatPolynomail(Polynomail *p,int m){
struct Polynomail *p1;
p1=p;
float a;
int b;
int i;
for(i=1;i<=m;i++){
cout<<"请输入第"<<i<<"项的系数和次数"<<endl;
cin>>a>>b;
if(b<=p1->expn){cout<<"输入有误";exit(0);}
CreatNode(p1,a, b);
p1=p1->next;
}
p1=p1->next;
}
void PrintPolynomail(Polynomail *p){
struct Polynomail *p1;
p1=p->next;
if(p1->expn==0)cout<<p1->coef;
else cout<<p1->coef<<"x^"<<p1->expn;
p1=p1->next;
while(p1!=NULL){
if(p1->coef<=0) cout<<p1->coef<<"x^"<<p1->expn;
else cout<<"+"<<p1->coef<<"x^"<<p1->expn;
p1=p1->next;
}
}
int CMPare(int a, int b){
if(a<b)return -1;
if(a==b)return 0;
if(a>b)return 1;
}
void AddPolynomail(Polynomail *pa, Polynomail *pb){
struct Polynomail *ha, *hb, *p1, *p2;
p1=ha=pa;
p2=hb=pb;
struct Polynomail *qa=ha->next;
struct Polynomail *qb=hb->next;
int a,b;
float sum;
while(qa&&qb){
a=qa->expn;
b=qb->expn;
switch(CMPare(a,b)){
case -1:p1=qa;qa=qa->next; break;
case 0 :sum=qa->coef+qb->coef;
if(sum!=0.0){qa->coef=sum; p1=qa;qa=qa->next;}
else{p1->next=qa->next;delete pa;qa=p1->next;}
p2->next=qb->next;delete qb; qb=p2->next; break;
case 1 :p2->next=qb->next; p1->next=qb;qb->next=qa;qb=p2->next;p1=p1->next;break;
}
}
if(qb!=NULL)p1->next=qb;
delete hb;
}
void MultPolynomail(Polynomail *pc, Polynomail *pa, Polynomail *pb){
pc->next=NULL;
struct Polynomail *p1, *p2, *p3, *q;
p2=pb->next;
while(p2!=NULL){
p1=pa->next;
struct Polynomail *p4=new struct Polynomail;
p3=p4;
while(p1!=NULL){
q=new struct Polynomail;
q->coef=p1->coef*p2->coef;
q->expn=p1->expn+p2->expn;
p3->next=q;
p3=p3->next;
p1=p1->next;
}
p3->next=NULL;
AddPolynomail(pc,p4);
p2=p2->next;
}
}
int main(){
int fa, fb;
struct Polynomail *pa=new struct Polynomail; pa->expn=-1;
struct Polynomail *pb=new struct Polynomail; pb->expn=-1;
cout<<"请输入多项式f的项数"<<endl;
cin>>fa;
CreatPolynomail(pa, fa);
cout<<"请输入多项式g的项数"<<endl;
cin>>fb;
CreatPolynomail(pb, fb);
cout<<"f(x)=";
PrintPolynomail(pa);
cout<<endl;
cout<<"g(x)=";
PrintPolynomail(pb);
cout<<endl;
struct Polynomail *pc=new struct Polynomail; pc->expn=-1;
MultPolynomail(pc, pa, pb );
cout<<"f(x)*g(x)=";
PrintPolynomail(pc);
cout<<endl;
AddPolynomail(pa, pb);
cout<<"f(x)+g(x)=";
PrintPolynomail(pa);
cout<<endl;
return 0;
}
板凳
lhq8782 [专家分:0] 发布于 2007-03-26 15:01:00
答非所问[em20]
我来回复