//数据结构作业
#include<iostream>
using namespace std;
class SingleList;
istream &operator>>(istream &in, SingleList &A);
ostream &operator<<(ostream &out,const SingleList &A);
class node
{
private:
int coef;
int exp;
node *next;
friend class SingleList;
friend void operator*(const SingleList &A,const SingleList &B);
};
class SingleList
{
private:
node *head;
public:
void creat(istream &in);
void print(ostream &out);
friend void operator*(const SingleList &A,const SingleList &B);
friend istream &operator>>(istream &in, SingleList &A);
friend ostream &operator<<(ostream &out,const SingleList &A);
void Delete();
};
void SingleList::creat(istream &in)
{
node *tail,*p;
int a,b;
head=tail=NULL;
while(cin>>a>>b&&b>=0)
{
p=new node;
p->coef=a;
p->exp=b;
p->next=NULL;
if (head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
return;
}
istream &operator>>(istream &in, SingleList &A)
{
A.creat(in);
return in;
}
void SingleList::Delete()
{
node *p,*q;
p=head;
while(p)
{
q=p;
p=p->next;
delete(q);
}
}
void SingleList::print(ostream &out)
{
node*p;
p=head;
if (p->coef==1)
{
if(p->exp==1)
cout<<"x";
else
if (p->exp==0)
cout<<"1";
else
cout<<"x"<<"^"<<p->exp;
}
else
{
if (p->coef==-1)
{
if (p->exp==1)
cout<<"-x";
else
if (p->exp==0)
cout<<"-1";
else
cout<<"-x"<<"^"<<p->coef;
}
else
if (p->coef!=0)
if (p->exp==1)
cout<<p->coef<<"x";
else
if (p->exp==0)
cout<<p->coef;
else
cout<<p->coef<<'x'<<'^'<<p->exp;
}
p=p->next;
while(p)
{
if (p->coef==1)
{
if (p->exp==1)
cout<<"+x";
else
if (p->exp==0)
cout<<"+1";
else
cout<<"+x"<<p->exp;
}
else
if (p->coef==-1)
{
if (p->exp==1)
cout<<"-x";
else
if (p->exp==0)
cout<<"-1";
else
cout<<"-x"<<'^'<<p->exp;
}
else
if (p->coef!=0)
if (p->coef>0)
{
if (p->exp==0)
cout<<"+"<<p->coef;
else
if (p->exp==1)
cout<<"+"<<p->coef<<"x";
else
cout<<'+'<<p->coef<<'x'<<'^'<<p->exp;
}
else
{
if(p->exp==0)
cout<<p->coef;
else
if (p->exp==1)
cout<<p->coef<<"x";
else
cout<<p->coef<<'x'<<'^'<<p->exp;
}
p=p->next;
}
cout<<endl;
}
ostream &operator<<(ostream &out, SingleList &B)
{
B.print(out);
return out;
}
void operator*(const SingleList &A,const SingleList &B)
{
node *chead,*a,*b,*p,*q,*temp;//*q为*p的前一个指针
chead=new node;//先建立一个表头节点
chead->coef=0;
chead->exp=-1;
chead->next=NULL;
a=A.head;//先从A开始扫描,每遇到一个新节点就将其与B中的每一个节点相乘(结果为temp),乘积结果在C中找出它相应的位置
while(a)
{
b=B.head;
q=chead;
while(b)
{
temp=new node;
temp->coef=a->coef*b->coef;
temp->exp=a->exp+b->exp;
temp->next=NULL;
p=chead->next;
while(p&&p->exp>temp->exp)//如果该节点的指数小于C中的每一个节点的指数,就向后移
{
q=p;
p=p->next;
}
if (p==NULL) //如果是空节点,就直接放在最后
{
q->next=temp;
}
else
if (p->exp==temp->exp)
{
p->coef=p->coef+temp->coef;//系数相加
if(p->coef==0)//如果相加后系数为0,就将该节点删去.
{
q->next=p->next;
delete(p);
}
}
else
{
if (p->exp<temp->exp)//如果系数小于temp,就将其插在q与o之间
{
q->next=temp;
temp->next=p;
}
}
b=b->next;
}
a=a->next;
}
p=chead->next;
if (p->coef==1)
cout<<"x"<<'^'<<p->exp;
else
if (p->coef==-1)
cout<<"-x"<<'^'<<p->exp;
else
if (p->coef==0)
p=p->next;
else
cout<<p->coef<<'x'<<'^'<<p->exp;
p=p->next;
while(p)
{
if (p->coef==1)
cout<<'+'<<'x'<<'^'<<p->exp;
else
if (p->coef==-1)
cout<<"-x"<<'^'<<p->exp;
else
if (p->coef==0)
p=p->next;
else
if (p->coef>0)
cout<<'+'<<p->coef<<'x'<<'^'<<p->exp;
else
cout<<p->coef<<'x'<<'^'<<p->exp;
p=p->next;
}
cout<<endl;
p=chead;
while(p)
{
q=p;
p=p->next;
delete(q);
}
}
int main()
{
SingleList A,B;
cout<<"*************************欢迎来到B14040312的小小多项式乘法计算器***************************"<<endl;
cout<<"请输入A的表达式"<<endl;
cin>>A;
cout<<"A的表达式为:";
cout<<A;
cout<<"请输入B的表达式"<<endl;
cin>>B;
cout<<"B的表达式为:";
cout<<B;
cout<<"计算结果为:";
A*B;
A.Delete();
B.Delete();
return 0;
}
多项式乘法
最新推荐文章于 2020-01-23 11:39:56 发布