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
km818 1年前 已收到1个回答 举报

绿满悠然 幼苗

共回答了18个问题采纳率:83.3% 举报

你的函数 参数传递的不对
PolyList input(int x,int y)
{
PolyNode *p,*q;
head_a=new PolyNode;
q=head_a;
coutx>>y;//这一行去掉
这部分改成
PolyList input(int xx,int yy)
{
PolyNode *p,*q;
int x,y;
head_a=new PolyNode;
q=head_a;
x=xx;y=yy;//按照你的意思第一项是通过参数指定的,而不是输入确定的
couty;
void output (PolyList c)
{
PolyNode *p;
p=head_c;
while(p!=NULL)
{
cout

1年前

1
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.994 s. - webmaster@yulucn.com