📄 lab_1_1.cpp
字号:
#include<iostream>
using namespace std;
struct Node
{
double a;
int e;
Node *next;
};
class Polynomial
{
public:
Node *Head;
Polynomial(){Head=NULL;}
void Input();
void Add(Node *HB);
void Output();
};
void Polynomial::Input()
{
cout<<"please input a Polynomial as (*,*) with 'e' at the end"<<endl;
double an;
int en;
char c;
Node *P=new Node,*N=new Node;
cin>>c;
while(c!='e'&&c!='\n')
{
cin>>an;
cin>>c;
cin>>en;
N=new Node;
N->a=an;
N->e=en;
if(Head==NULL)
Head=P=N;
else
{
P->next=N;
P=P->next;
}
cin>>c;
if(c==')')
cin>>c;
}
if(Head==NULL)
cout<<"There is something wrong with your input!"<<endl;
else
P->next=NULL;
}
void Polynomial::Add(Node *HB)
{
Node *PA=Head,*PB=HB,*HC=NULL,*P=NULL;
cout<<"a ";////////////////////////
while(PA!=NULL&&PB!=NULL)
{
if(PA->e>PB->e)
{
if(HC==NULL)
HC=P=PA,cout<<" b ";/////////////////
else
{
P->next=PA;
PA=PA->next;P=P->next;cout<<" c ";/////////////////
}
}
else if((PA->e)<(PB->e))
{
if(HC==NULL)
HC=P=PB;
else
{
P->next=PB;
PB=PB->next;P=P->next;
}
}
else
{
PA->a=PA->a+PB->a;
if(PA->a==0)
PA=PA->next,PB=PB->next;
else
{
if(HC==NULL)
HC=P=PA,cout<<" d ";/////////////////
else
{
P->next=PA;
PA=PA->next,PB=PB->next;P=P->next;
}
}
}
}
if(PA!=NULL)
{
if(HC!=NULL)
P->next=PA;
}
else if(PB!=NULL)
{
if(P==NULL)
HC=PB;
else
P->next=PB;
}
Head=HC;
}
void Polynomial::Output()
{
Node *P=Head;
if(Head==NULL)
cout<<'0';
else
do
{
if(P->e==0)
if(P->a<0)cout<<P->a;
else cout<<"+"<<P->a;
else if(P->a==1)
cout<<"+";
else if(P->a==-1)
cout<<"-";
else if(P->a>0)
cout<<"+"<<P->a;
else
cout<<P->a;
if(P->e==1)
cout<<"x";
else if(P->e!=0)
{
cout<<"x^";
if(P->e<0)
cout<<'('<<P->e<<')';
else
cout<<P->e;
}
P=P->next;
}
while(P!=NULL);
cout<<'\n';
}
void main()
{
Polynomial A,B;
do
{
A.Input();
}
while(A.Head==NULL);
A.Output();//////////
do
{
B.Input();
}
while(B.Head==NULL);
B.Output();///////////////
A.Add(B.Head);
A.Output();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -