下面贴代码
#include <iostream>
#include <stdlib.h>
#include <cstdio>
using namespace std;
typedef struct term
{
int coef;//系数
int expn;//指数
struct term *next;
}term ,*LinkList;
void InsertList(LinkList &L,LinkList &s)//指数降序插入
{
if(s->coef==0)
return ;
LinkList p,q;
p=L;
while(((p->next!=NULL)&&(p->next->expn>s->expn)))//如果当前指数大的话,继续遍历。
p=p->next;
if((p->next!=NULL)&&(p->next->expn==s->expn))//指数相等的话,系数相加
{
p->next->coef+=s->coef;
if(p->next->coef==0) //相加后系数为0,删除节点。
{
q=p->next;
p->next=q->next;
free(q);
}
}
else //指数比L中都小或当前为空表直接插入。
{
s->next=p->next;
p->next=s;
}
}
//创建链表
LinkList CreateList(LinkList L)
{
LinkList p,q,head;
int i=1;
head=(LinkList)malloc(sizeof(term));
head->next=NULL;
p=(LinkList)malloc(sizeof(term));
q=(LinkList)malloc(sizeof(term));
do
{
printf("请输入第%d项的系数和指数,以空格隔开:",i);
cin>>p->coef>>p->expn;
i++;
q=p;
InsertList(head,p);
p=(LinkList)malloc(sizeof(term));
}while(q->coef!=0);
free(p);
return head;
}
//输出
void PrintList(LinkList L)
{
LinkList p;
int flag=0;
if(!L)
{
cout<<"多项式为空"<<endl;
return ;
}
p=L->next;
cout<<"多项式为:";
while(p)
{
if(flag==0) //插入第一个节点
{
if(p->expn==0) //指数为0直接输出指数
cout<<p->coef;
else
cout<<p->coef<<"x^"<<p->expn;
flag=1;
}
else //插入非第一个节点
{
if(p->coef>0)
{
if(p->expn==0)
cout<<"+"<<p->coef;
else
cout<<"+"<<p->coef<<"x^"<<p->expn;
}
else
{
if(p->expn==0)
cout<<p->coef;
else
cout<<p->coef<<"x^"<<p->expn;
}
}
p=p->next;
}
cout<<endl;
}
//升序输出
LinkList UpPrintList(LinkList &L)
{
LinkList p=L;
p=p->next;
LinkList q,s;
q=(LinkList)malloc(sizeof(term));
q->next=NULL;
while(p)
{
s=(LinkList)malloc(sizeof(term));
s->coef=p->coef;
s->expn=p->expn;
s->next=q->next;
q->next=s;
p=p->next;
}
return q;
}
//复制链表
LinkList CopyLinkList(LinkList p) //复制链表。
{
LinkList L,s;
L=(LinkList)malloc(sizeof(term));
L->next=NULL; //头节点置空
p=p->next;
while(p)
{
s=(LinkList)malloc(sizeof(term));
s->coef=p->coef;
s->expn=p->expn;
InsertList(L,s); //插入
p=p->next;
}
return L; //复制成功
}
//相加
LinkList AddLinkList(LinkList &p,LinkList &q)
{
LinkList x,y;
x=CopyLinkList(p);
y=CopyLinkList(q);
y=y->next;
while(y)
{
InsertList(x,y);
y=y->next;
}
return x;
}
//相减取反
void Opposite(LinkList &L)
{
LinkList p=L;
p=p->next;
while(p)
{
p->coef=0-p->coef;
p=p->next;
}
}
//清空链表
void DestoryList(LinkList &L)
{
LinkList q;
while(L)
{
q=L;
L=L->next;
free(q);
}
}
//相减
LinkList SubLinkList(LinkList &p,LinkList &q)
{
LinkList x=CopyLinkList(q);
Opposite(x);
LinkList L=AddLinkList(p,x);
return L;
}
//相乘
LinkList MultiplyList(LinkList &x,LinkList &y)
{
LinkList p,q,r,s;
p=x->next;
q=y->next;
r=(LinkList)malloc(sizeof(term));
r->next=NULL;
while(p)
{
while(q)
{
s=(LinkList)malloc(sizeof(term));
s->coef=p->coef*q->coef;
s->expn=p->expn+q->expn;
InsertList(r,s);
q=q->next;
}
p=p->next;
q=y->next;
}
return r;
}
void menu()
{
cout<<"\t\t欢迎来到zyx的一元多项式运算系统\t\t"<<endl;
cout<<"\t\t 请选择你要进行的操作 \t\t"<<endl;
cout<<"\t\t 1.创建多项式 \t\t"<<endl;
cout<<"\t\t 2.多项式相加 \t\t"<<endl;
cout<<"\t\t 3.多项式相减 \t\t"<<endl;
cout<<"\t\t 4.多项式相乘 \t\t"<<endl;
cout<<"\t\t 5.多项式逆序输出 \t\t"<<endl;
cout<<"\t\t 6.多项式正序输出 \t\t"<<endl;
cout<<"\t\t 7.退出系统 \t\t"<<endl;
}
int main()
{
LinkList a[20]={NULL};
LinkList L1,L4,L3,L6;
int i,j;
while(1)
{
menu();
int n;
cin>>n;
switch(n)
{
case 1:
cout<<"请输入你要创建多项式的序号:"<<endl;
cin>>i;
a[i]=CreateList(a[i]);
break;
case 2:
cout<<"请输入你要相加的两个多项式序号"<<endl;
cin>>i>>j;
cout<<"第一个多项式为:"<<endl;
PrintList(a[i]);
cout<<"第二个多项式为:"<<endl;
PrintList(a[j]);
L1=AddLinkList(a[i],a[j]);
cout<<"相加后的多项式为:"<<endl;
PrintList(L1);
DestoryList(L1);
break;
case 3:
cout<<"请输入你要相减的两个多项式序号"<<endl;
cin>>i>>j;
L3=SubLinkList(a[i],a[j]);
cout<<"相减后的多项式为:"<<endl;
PrintList(L3);
DestoryList(L3);
break;
case 4:
cout<<"请输入你要相乘的两个多项式序号"<<endl;
cin>>i>>j;
L4=MultiplyList(a[i],a[j]);
PrintList(L4);
DestoryList(L4);
break;
case 5:
cout<<"请输入你要逆序输出的编号"<<endl;
cin>>i;
PrintList(a[i]);
break;
case 6:
cout<<"请输入你要正序输出的编号"<<endl;
cin>>i;
L6=UpPrintList(a[i]);
PrintList(L6);
DestoryList(L6);
break;
case 7:
cout<<"感谢您的使用."<<endl;
exit(0);
break;
default :
cout<<"您的输入有误,请重新输入"<<endl;
break;
}
//system("pause");
//system("cls");
}
return 0;
}