多项式乘法中结果合并时加减号的问题
#include<iostream>using namespace std;
typedef struct node
{
char sign; //符号
float coef; //系数
int expn; //指数
node* next;
}
node,*dxscf;
void sort(dxscf& Head)
{
//采用冒泡排序对多项式进行排序
dxscf Q;
for(dxscf P1 = NULL; P1 != Head->next->next; P1 = Q)
for(dxscf P2 = Head->next; P2->next != P1; P2 = P2->next)
{
if(P2->expn > P2->next->expn) //如果指数前一个大于后一个,则互换指数和系数
{
int temp1;
double temp2;
char temp3; //temp1,2,3是三个交换变量
temp1 = P2->expn;
P2->expn = P2->next->expn;
P2->next->expn = temp1;
temp2 = P2->coef;
P2->coef = P2->next->coef;
P2->next->coef = temp2;
temp3 = P2->sign;
P2->sign = P2->next->sign;
P2->next->sign = temp3;
}
Q = P2->next; //此处让Q为本次执行后的P2的下一个,以便P1=Q让P1成为P2的下一个,作为下一次循环的结束标志。
}
}//冒泡排序(升序)
void PrintList(dxscf Head) //输出函数
{
dxscf p = Head ->next;
if(p != NULL )
{
if(p->coef != 0) //系数不为0
{
if(p->expn != 0) //指数不为0
{
if(p->expn != 1) //指数是否为1
{
if(p->coef == 1) //系数是否为1
cout << "x^" << p->expn;
else
cout << p->coef << "x^" << p->expn;
}
else //指数为1
{
if(p->coef == 1) //指数为1且系数为1
cout << "x";
else
cout << p->coef << "x";
}
}
else
cout << p->coef; //指数为0
} //输出第一项
p = p->next; //指向第二项
}
while(p != NULL)
{
if(p->sign == '+') //第一种情况:+
{
if(p->coef != 0) //系数不为0
{
if(p->expn != 0) //指数不为0
{
if(p->expn != 1) //指数不为1
{
if(p->coef == 1) // 系数为1
cout << '+' << "x^" << p->expn;
else
cout << '+' << p->coef << "x^" << p->expn;
}
else
{
if(p->coef == 1) //指数为1
cout << '+' << "x";
else
cout << '+' << p->coef << "x";
}
}
else
cout << '+' << p->coef;
}
else
{
p = p->next;
continue;
}
}
if(p->sign == '-') //第二种情况:-
{
if(p->coef != 0)
{
if(p->expn != 0)
{
if(p->expn != 1)
{
if(p->coef == -1)
cout << '-' << "x^" << p->expn;
else
cout << p->coef << "x^" << p->expn;
}
else
{
if(p->coef == -1)
cout << '-' << "x";
else
cout << p->coef << "x";
}
}
else
cout << p->coef;
}
}
p = p->next;
}
}//输出其余的项
void Creat_List(dxscf& Head) //创建多项式
{
cout << "请输入你的因式的项数:" << endl;
int n;
cin >> n;
dxscf p = Head;
cout << "开始输入因式的系数和指数:" << endl;
for(int i = 0; i < n; i++)
{
dxscf Node = new node;
cout<<"请输入第"<<i+1<<"系数:";
cin >> Node->coef;
if(Node->coef > 0) //判断符号
Node->sign = '+';
if(Node->coef < 0)
Node->sign = '-';
cout<<"请输入第"<<i+1<<"指数:";
cin >> Node ->expn;
Node->next = p->next; //在head后插入第一个节点
p->next = Node;
p = p->next;
}
p->next = NULL;
cout << "按升序排列的多项式是:";
sort(Head);
PrintList(Head);
cout << endl;
}//创建多项式
void DelList(dxscf& Head, int n) //删除多项式的第n个因式
{
dxscf p = Head;
for(int i = 1; i < n; i++) //p节点所值位置即为需要删除的前一位(有头节点)
p = p->next;
dxscf Q;
Q = p->next;
p->next = Q ->next;
delete Q;
}
void multip(dxscf Head1,dxscf Head2) //用P2的元素去乘P1的每个元素
{
dxscf Head3 = new node;
Head3->next = NULL;
dxscf P1, P2, P3;
P3 = Head3; //
for(P1 = Head1->next; P1!=NULL; P1 = P1->next)
for(P2 = Head2->next; P2!=NULL;P2 = P2->next)
{ dxscf Node = new node;
Node->expn = P1->expn + P2->expn;
Node->coef = P1->coef * P2->coef;
if(Node->coef >= 0)
Node->sign = '+';
else
Node->sign = '-';
Node->next = P3->next;
P3->next = Node;
P3 = P3->next; }
sort(Head3);//head3排序,为合并同类项做基础
int i = 1;
for(dxscf P = Head3->next; P->next != NULL;)
{
if(P->expn == P->next->expn)
{
P->coef += P->next->coef;
DelList(Head3,i+1);//把后面指数项加到与前面相同的指数项上面,然后删除该项
}
else
{
P = P->next;
i++;
}
}//end for 为了合并同类项 排完序后若有同类项,则一定是位置上相邻的
cout << "两多项式相乘的结果是: ";
PrintList(Head3); cout << endl; }
int main()
{
dxscf Head1 = new node;
dxscf Head2 = new node;
Head1->next = NULL;
Head2->next = NULL;
Creat_List(Head1);
Creat_List(Head2);
multip(Head1,Head2);
return 0;
}
为什么合并时加减号都存在
[local]1[/local]