早教吧作业答案频道 -->其他-->
C++一元多项式相加问题.数据结构.为什么不行啊?通过键盘输入两个形如P0+P1X^1+P2X^2+……+PnX^n的多项式、代码://一元多项式相加.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdafx.h"#in
题目详情
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
通过键盘输入两个形如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
▼优质解答
答案和解析
你的函数 参数传递的不对
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
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
看了 C++一元多项式相加问题.数...的网友还看了以下: