#include<malloc.h>
#include<iostream.h>
typedef struct Polynomialnomial
{
int coef,ex;
Polynomialnomial *next;
}Polynomial;
//创建多项式
Polynomial *create_Polynomial(void);
//多项式加法
Polynomial *addition_of_Polynomialnomial(Polynomial *,Polynomial *);
//多项式乘法
Polynomial *Polynomialnomial_Multiplication(Polynomial *,Polynomial *);
//输入多项式
void print_Polynomial(Polynomial *);
//多项式系数取反
Polynomial *negate_Polynomial(Polynomial *head);
int main(void)
{
static Polynomial *heada2,*headb2,*heada=NULL,*headb=NULL,*headsum=NULL,*headmul=NULL;
Polynomial *headsubduction=NULL,*headsubduction2=NULL;
int choose;
int flag=1;
do{
cout<<"+++++++++++++++++++++++++"<<endl;
cout<<"+
欢迎使用!
+"<<endl;
cout<<"+
请创建多项式
+"<<endl;
cout<<"+++++++++++++++++++++++++"<<endl;
cout<<"
创建多项式A:\n";
heada=create_Polynomial();
cout<<"\n
创建多项式B:\n";
headb=create_Polynomial();
cout<<"\n多项式A:\t";
print_Polynomial(heada);
cout<<"多项式B:\t";
print_Polynomial(headb);
int t=1;
do{
cout<<"\n请选择你要进行的操作:"<<endl;
cout<<"1.A+B
2.A-B \n3.B-A
4.A*B"<<endl;
cout<<"选择:";
cin>>choose;
switch(choose)
{
case 1:
cout<<"A+B:"<<endl;
headsum=addition_of_Polynomialnomial(heada,headb);
cout<<"两个多项式的和:\t";
print_Polynomial(headsum);
break;
case 2:
cout<<"A-B:\n";
cout<<"第一个多项式减去第二个多项式:\t";
headb2=negate_Polynomial(headb);
headsubduction=addition_of_Polynomialnomial(heada,headb2);
print_Polynomial(headsubduction);
negate_Polynomial(headb);
break;
case 3:
cout<<"B-A:"<<endl;
cout<<"第二个多项式";
cout<<"减去第一个多项式:\t";
heada2=negate_Polynomial(heada);
headsubduction2=addition_of_Polynomialnomial(headb,heada2);
print_Polynomial(headsubduction2);
negate_Polynomial(heada);
break;
case 4:
headmul=Polynomialnomial_Multiplication(heada, headb);
cout<<"两个多项式的积:\t";
print_Polynomial(headmul);
break;
}//switch
cout<<"\n继续操作?\n";
cout<<"1.是的
0.不是\n";
cout<<"选择:";
cin>>t;
}while(t==1);
cout<<"+++++++++++++++++++++++++"<<endl;
cout<<"+
继续创建多项式? +\n";
cout<<"+
1.是的
0.不是
+\n";
cout<<"+++++++++++++++++++++++++"<<endl;
cout<<"
选择:";
cin>>flag;
cout<<endl;
}while(flag==1);
cout<<"+++++++++++++++++++++++++"<<endl;
cout<<"+
您已经退出!
+"<<endl;
cout<<"+++++++++++++++++++++++++"<<endl;
return 0;
}
//创建
Polynomial *create_Polynomial()
{
int coefficient=0,exponent=0;
Polynomial *head=NULL,*temp=NULL,*rear=NULL;
head=(Polynomial *)malloc(sizeof(Polynomial));
rear=head;
cout<<"请输入系数 指数(指数降序排列,以 0 0 结束):\n";
cin>>coefficient>>exponent;
while(coefficient != 0)
{
temp=(Polynomial *)malloc(sizeof(Polynomial));
temp->coef=coefficient;
temp->ex=exponent;
rear->next=temp;
rear=temp;
cin>>coefficient>>exponent;
}
rear->next=NULL;
return head;
}
Polynomial *addition_of_Polynomialnomial(Polynomial *p1, Polynomial *p2) //多项式相加
{
Polynomial *head, *tail, *temp;
int value;
p1=p1->next;
p2=p2->next;
head=tail=(Polynomial*)malloc(sizeof(Polynomial));
head->next=NULL;
while(p1 && p2)
{
if((p1->ex) == (p2->ex))
{
value=(p1->coef)+(p2->coef);
if(value != 0)
{
temp=(Polynomial *)malloc(sizeof(Polynomial));
temp->coef=value;
temp->ex=p1->ex;
temp->next=NULL;
}
if(value==0 && !p1->next && !p2->next)
{
p1=p1->next;
p2=p2->next;
break;
}
if(value==0 && p1->next && p2->next)
{
p1=p1->next;
p2=p2->next;
continue;
}
p1=p1->next;
p2=p2->next;
}
else if((p1->ex) > (p2->ex))
{
temp=(Polynomial *)malloc(sizeof(Polynomial));
temp->coef=p1->coef;
temp->ex=p1->ex;
temp->next=NULL;
p1=p1->next;
}
else
{
temp=(Polynomial *)malloc(sizeof(Polynomial));
temp->coef=p2->coef;
temp->ex=p2->ex;
temp->next=NULL;
p2=p2->next;
}
if(head->next==NULL)
{
head->next=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
tail->next=p1?p1:p2; //有剩余项的接在后面。
return head;
}
//相乘
Polynomial *Polynomialnomial_Multiplication(Polynomial *p1, Polynomial *p2)
{
Polynomial *head;
Polynomial *t,*q,*s,*r;
head=(Polynomial *)malloc(sizeof(Polynomial));
head->next=NULL;
r=(Polynomial *)malloc(sizeof(Polynomial));
r->next=NULL;
for(t=p1->next;t;t=t->next)
{
for(q=p2->next;q;q=q->next)
{
s=(Polynomial *)malloc(sizeof(Polynomial));
r->next=s;
s->coef=q->coef * t->coef;
s->ex=q->ex + t->ex;
s->next=NULL;
head=addition_of_Polynomialnomial(r,head);//调用加法函数
}
}
return head;
}
//输出
void print_Polynomial(Polynomial *head)
{
Polynomial *p=NULL;
p=head->next;
if(p == NULL)
{
cout<<"0\n";
return;
}
else
{
do
{
if(p->coef>=0 && p->ex!=0)
cout<<"+"<<p->coef<<"x^"<<p->ex;
if(p->coef>=0 && p->ex==0)
cout<<"+"<<p->coef;
if(p->coef<0 && p->ex!=0)
cout<<p->coef<<"x^"<<p->ex;
if(p->coef<0 && p->ex==0)
cout<<p->coef;
p=p->next;
}while(p != NULL);
cout<<endl;
}
}
//多项式的取反
Polynomial *negate_Polynomial(Polynomial *head)
{
Polynomial *p=NULL;
p=head->next;
if(p == NULL)
{
cout<<"这个多项式是空的.\n";
return NULL;
}
else
{
while(p)
{
p->coef=0-(p->coef);
p=p->next;
}
}
return head;
}