# 谁能帮我改下这个程序

yangzhiling 2008-03-15 02:16:17
//多项式相加
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
float coef;
int expn;
struct node *next;
}PolyNode;
//创建一个多项式
void GreatPoly(PolyNode *&sq)
{
PolyNode*q=sq,*s;
int n,y,i=0;
float x;
sq=(PolyNode*)malloc(sizeof(PolyNode));
sq->next=NULL;
cout<<"请输入所要多项式项数:";
cin>>n;
cout<<endl;
cout<<"输入多项式:";
while(i<n)
{
cin>>x;
cin>>y;
s=(PolyNode*)malloc(sizeof(PolyNode));
s->coef=x;s->expn=y;
s->next=NULL;
q->next=s;
q=s;
i++;
}
cout<<"成功"<<endl;
}
//打印一个多项式
void DisPoly(PolyNode*sq)
{
PolyNode *p=sq->next;
while(p!=NULL)
{
cout<<"("<<p->coef<<","<<p->expn<<")";//<<"\t";
p=p->next;
}
cout<<endl;
}
//排序
void SortPoly(PolyNode*&sq)
{
PolyNode*p=sq->next,*pre,*q;
sq->next=NULL;
while(p!=NULL)
{
if(sq->next==NULL)
{
sq->next=p;
p=p->next;
sq->next->next=NULL;
}
else
{
pre=sq,q=pre->next;
while(q!=NULL&&p->expn>q->expn)
{
pre=q;q=q->next;
}
q=p->next;
p->next=pre->next;
pre->next=p;
p=q;
}
}
}
//相加
{
PolyNode*pc,*p1=pa->next,*p2=pb->next,*tc,*s,*p;
pc=(PolyNode*)malloc(sizeof(PolyNode));
pc->next=NULL;
tc=pc;
while(p1!=NULL&&p2!=NULL)
{
if(p1->expn<p2->expn)
{
s=(PolyNode*)malloc(sizeof(PolyNode));
s->next=NULL;
s->coef=p1->coef;
s->expn=p1->expn;
tc->next=s;
tc=s;
p1=p1->next;
}
else if(p1->expn>p2->expn)
{
s=(PolyNode*)malloc(sizeof(PolyNode));
s->next=NULL;
s->coef=p2->coef;
s->expn=p2->expn;
tc->next=s;
tc=s;
p2=p2->next;
}
else
{
if(p1->coef+p2->coef!=0)
{
s=(PolyNode*)malloc(sizeof(PolyNode));
s->next=NULL;
s->coef=p1->coef+p2->coef;
s->expn=p1->expn;
tc->next=s;
tc=s;
}
p1=p1->next;p2=p2->next;
}
}
if(p1!=NULL) p=p1;
else p=p2;
while(p!=NULL)
{
s=(PolyNode*)malloc(sizeof(PolyNode));
s->next=NULL;
s->coef=p->coef;s->expn=p->expn;
tc->next=s;
tc=s;
p=p->next;
}
//tc->next=NULL;
return p;
}
void main()
{
PolyNode*sq1,*sq2,*sq3;
cout<<"两多项式相加运算:"<<endl;

GreatPoly(sq1);
SortPoly(sq1);
cout<<"排序后多项式A:";DisPoly(sq1);

GreatPoly(sq2);
SortPoly(sq2);
cout<<"排序后多项式B:";DisPoly(sq2);

cout<<"两多项式相加结果为:";DisPoly(sq3);
}
...全文
41 3 打赏 收藏 举报

3 条回复

guanlei000 2008-03-15
• 打赏
• 举报

ttkk_2007 2008-03-15
• 打赏
• 举报

``````
//格式太乱，我就看了看创建的
void GreatPoly(PolyNode *&sq)
{
PolyNode*q=sq,*s;               //这个地方错了，你让q指向sq，太早了，改为PolyNode*q,*s;
int n,y,i=0;
float x;
sq=(PolyNode*)malloc(sizeof(PolyNode));
sq-> next=NULL;                         //放在这q=sq；
cout < <"请输入所要多项式项数:";
cin> > n;
cout < <endl;
cout < <"输入多项式:";
while(i <n)
{
cin> > x;
cin> > y;
s=(PolyNode*)malloc(sizeof(PolyNode));
s-> coef=x;s-> expn=y;
s-> next=NULL;
q-> next=s;
q=s;
i++;
}
cout < <"成功" < <endl;
}

``````
guanlei000 2008-03-15
• 打赏
• 举报

void GreatPoly(PolyNode *&sq)
{
PolyNode* q=sq,*s;
int n,y,i=0;
float x;
sq=(PolyNode*)malloc(sizeof(PolyNode));
sq-> next=NULL;
q=sq;//这里是一处错误，所以创建总出错，sq这个指针开始也应该在主函数中初始化为空(null)
cout<<"请输入所要多项式项数:";
cin>>n;
cout<<endl;
cout<<"输入多项式:";
while(i <n)
{
cin>> x;
cin>> y;
s=(PolyNode*)malloc(sizeof(PolyNode));
s-> coef=x;s-> expn=y;
s-> next=NULL;
q-> next=s; //这句是上面要修改的依据，不加上面那一句，q开始时空，q->next是什么呢，所以要再把sq新的链表头付给q，q才能指向下面
q=s;
i++;
}
cout <<"成功" <<endl;
}

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-03-15 02:16

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下