多项式的修改问题
[code=C/C++]#include<iostream.h>#include<conio.h>
#include<stdlib.h>
struct Node
{
float coef;//结点类型
int exp;
};
typedef Node polynomial;
struct LNode
{
polynomial data;//链表类型
LNode *next;
};
typedef LNode* Link;
void CreateLink(Link &L,int n);
void PrintList(Link L);
void PolyAdd(Link &pc,Link pa,Link pb);
void PolySubstract(Link &pc,Link pa,Link pb);
void CopyLink(Link &pc,Link pa);
void PolyMultiply(Link &pc,Link pa,Link pb);
int JudgeIfExpSame(Link pa, int e);
void DestroyLink(Link &L);
void DestroyLink(Link &L)
{
Link p;
while(L)
{
p = L;
L = L->next;
delete p;
}
}
//创建含有n个链表类型结点的项,即创建一个n项多项式
void CreateLink(Link &L,int n)
{
Link p, t, tp;
float coef;
int exp;
int i=1;
if(L)
{
DestroyLink(L);
}
while(i <= n)
{
while(1)
{
cout << "请输入第" <<i << "项的系数和指数:" << endl;
cout << "系数:";
cin >> coef;
cout << "指数:";
cin >> exp;
if((exp < 0) || (coef == 0))
{
cout << "您输入有误,系数不能为零,指数不为负值,重新输入!" << endl;
}
else
{
if(JudgeIfExpSame(L, exp))
{
cout<<"输入的该项指数与多项式中已存在的某项相同,请重新输入" << endl;
}
else
{
break;
}
}
}
tp = NULL;
t = L;
while(t)
{
if(t->data.exp < exp) break;
tp = t;
t = t->next;
}
p = new LNode;
p->data.coef = coef;
p->data.exp = exp;
if(tp)
{
p->next = tp->next;
tp->next = p;
}
else
{
p->next = L;
L = p;
}
//
i++;
}
}
/*判断指数是否与多项式中已存在的某项相同*/
int JudgeIfExpSame(Link L, int e)
{
while(L)
{
if(L->data.exp == e)
{
return 1;
}
L = L->next;
}
return 0;
}
/*输出链表*/
void PrintList(Link L)
{
Link p;
if(!L)
cout<<"该一元多项式为空!"<<endl;
else
{
p = L;
while(p)
{
//项的系数大于0的5种情况
if((p->data).coef>0)
{
if(p != L) cout << " + ";
if((p->data).exp == 0)
cout << (p->data).coef;
else if(((p->data).coef == 1) && ((p->data).exp == 1))
cout <<"x";
else if(((p->data).coef == 1) && ((p->data).exp != 1))
cout << "x^" << (p->data).exp;
else if(((p->data).coef != 1) && ((p->data).exp == 1))
cout<<(p->data).coef<<"x";
else
cout<<(p->data).coef<<"x^"<<(p->data).exp;
}
//项的系数小于0的5种情况
if((p->data).coef<0)
{
if((p->data).exp == 0)
cout<<(p->data).coef;
else if((p->data.coef == -1) && (p->data.exp == 1))
cout<<"-x";
else if((p->data.coef == -1) && (p->data.exp != 1))
cout<<"-x^"<<p->data.exp;
else if(p->data.exp == 1)
cout<<p->data.coef<<"x";
else
cout<<(p->data).coef<<"x^"<<(p->data).exp;
}
if(p=p->next)
{
cout << "";
}
}
}
cout<<endl;
}
/*把一个链表的内容复制给另一个链表*/
void CopyLink(Link &pc,Link pa)
{
Link t, n;
if(pc) DestroyLink(pc);
//
t = NULL;
while(pa)
{
n = new LNode;
n->data = pa->data;
n->next = NULL;
//
if(!pc)
{
pc = n;
t = n;
}
else
{
t->next = n;
t = n;
}
pa = pa->next;
}
}
/*将两个一元多项式相加*/
void PolyAdd(Link &pc,Link pa,Link pb)
{
Link p, t;
unsigned int exp1, exp2;
t = NULL;
while(pa || pb)
{
if(pa) exp1 = pa->data.exp; else exp1 = 0xFFFFFFFF;
if(pb) exp2 = pb->data.exp; else exp2 = 0xFFFFFFFF;
//
p = new LNode;
p->next = NULL;
//
if(exp1 < exp2)
{
p->data = pa->data;
pa = pa->next;
}
else if(exp1 > exp2)
{
p->data = pb->data;
pb = pb->next;
}
else
{
p->data.exp = exp1;
p->data.coef = pa->data.coef + pb->data.coef;
pa = pa->next;
pb = pb->next;
}
//
if(t)
{
t->next = p;
}
else
{
pc = p;
}
t = p;
}
}
void main()
{
int n;
char ch;
Link L,La=NULL,Lb=NULL;//La,Lb分别为创建的两个多项式
do
{
cout<<"请输入你要运算的第一个一元多项式的项数:"<<endl;
cin>>n;
CreateLink(La,n);
cout<<"请输入你要运算的第二个一元多项式的项数:"<<endl;
cin>>n;
CreateLink(Lb,n);
PolyAdd(L,La,Lb);
cout<<""<<endl;
cout<<"待相加的两个一元多项式为:"<<endl;
cout<<""<<endl;
cout<<"A的多项式为:";
PrintList(La);
cout<<""<<endl;
cout<<"B的多项式为:";
PrintList(Lb);
cout<<""<<endl;
cout<<"相加后的结果为:";
PrintList(L);
cout<<""<<endl;
cout<<"\nContinue(Yes:anykey/Not:Enter n)?";
cin>>ch;
if(ch=='N') ch='n';
}while(ch!='n');
}
[/code]
改好改去还是改到这样的形式,C多项式结果还是有点问题。
(1)9x^8-7x^3+3x-5
(2)-9x^8+5x^3+2x^2-3x
(3)-2x^3+2x^2-5
(1)+(2)=(3)
结果还是错了。