C++一元多项式相加问题.数据结构.为什么不行啊?
C++一元多项式相加问题.数据结构.为什么不行啊?
通过键盘输入两个形如P0+P1X^1+P2X^2+……+PnX^n的多项式、
代码:
// 一元多项式相加.cpp :Defines the entry point for the console application.
//
#include "stdafx.h"
#include
using namespace std;
typedef struct node{
float coef;
int exp;
int flag;
struct node *next;
}PolyNode,*PolyList;
PolyNode *head_a,*head_b,*head_c;
PolyList a,b,c;
PolyList input(int x,int y)
{
PolyNode *p,*q;
head_a=new PolyNode;
q=head_a;
coutx>>y;
while(x!=0&&y!=0)
{
p=new PolyNode;
q->coef=x;
q->exp=y;
q->flag=0;
q->next=p;
q=p;
coutx>>y;
}
a=head_a;
return a;
}
PolyList add(PolyList a,PolyList b)
{
PolyNode *p,*q,*r,*s;
p=head_a;
q=head_b;
head_c=new PolyNode;
r=head_c;
if(p->exp==q->exp)
{
s=new PolyNode;
r->coef=p->coef+q->coef;
r->exp=p->exp;
p->flag=1;
p=p->next;
q=head_b;
r->next=s;
r=s;
}
else
{
s=new PolyNode;
r->coef=p->coef;
r->exp=p->exp;
p->flag=1;
p=p->next;
q=head_b;
r->next=s;
r=s;
}
c=head_c;
return c;
}
void output (PolyList c)
{
PolyNode *p;
p=head_c;
while(p!=NULL)
{
cout