线性表ADT应用
实验二:一元多项式的基本运算
实验目的:掌握用线性表实现一元多项式的基本运算。
实验内容:使用链式存储实现一元多项式的加法、减法、乘法和求导。即:
C(x)= A(x)+B(x);C(x)= A(x)-B(x) C(x)= A(x)*B(x) C(x)= A’(x)
菜单:
1)C :分别创建两个多项式A(x)和B(x),其中 输入时按照 指数的升序顺序输入,遇到系数为0则停止。例如:输入 :
1 2 3 4 5 6 7 8
0 2 3 4 5 6 7 0 则生成的多项式分别为:
A(x)=x2+3x4+5x6+7x8
B(x)=2x3+4x5+6x^7
2)P:计算C(x)= A(x)+B(x),计算完毕后输出C(x)的 结果
3)S: 计算C(x)= A(x)-B(x),计算完毕后输出C(x)的 结果
4)M: 计算C(x)= A(x)*B(x),计算完毕后输出C(x)的 结果
5)D: 计算C(x)= A’(x),计算完毕后输出C(x)的 结果
6)V: 首先输入一个 float型数据,然后计算 A(x)并输出计算的结果。
7)C: 分别清空A(x)、B(x)、C(x)三个多项式。
8)X: 退出程序。
例如:
样例输入1:
C
1 2 3 4 5 6 7 8 0
2 3 4 5 6 7 0
S
P
M
D
V
2
X
样例输出1:
C(x)=x^2-2x^3+3x^4-4x^5+5x^6-6x^7+7x^8
C(x)=x^2+2x^3+3x^4+4x^5+5x^6+6x^7+7x^8
C(x)=2x^5+10x^7+28x^9+52x^11+58x^13+42x^15
C(x)=2x+12x^3+30x^5+56x^7
2164.00
样例输入2:
C
2 2 0
2 2 3 3 0
P
S
M
D
V
3.1
X
样例输出2:
C(x)=4x^2+3x^3
C(x)=-3x^3
C(x)=4x^4+6x^5
C(x)=4x
19.22
样例输入3:
C
3 0 2 2 0
-2 2 3 3 0
M
V
2.1
P
S
D
X
样例输出3:
C(x)=-6x^2+9x^3-4x^4+6x^5
11.82
C(x)=3+3x^3
C(x)=3+4x^2-3x^3
C(x)=4x
友情提示:注意表达式相减后为0的情况。
这个题写的我。。。真的是一言难尽,本来想用C写,但是看了一下,要建立三个链表(A,B,-B),算了,还是用c++的类写吧。写下来整体还比较顺,就是最后提交的时候输出顺序和我自己调试的顺序不一样,估计是我用了cout和printf两种输出的原因吧(printf的输出结果会跑到最后面),所以改了一下。用以下方法可以用cout输出小数点后两位数(相当于printf的%.2f):
cout<<fixed<< setprecision(2) <<sum<<endl;
代码如下:
#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3)
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef queue<int> q_i;
typedef queue<string> q_s;
typedef queue<double> q_d;
typedef queue<ll> q_ll;
typedef queue<char> q_c;
typedef priority_queue<int> pq_i;
typedef priority_queue<string> pq_s;
typedef priority_queue<double> pq_d;
typedef priority_queue<ll> pq_ll;
typedef stack<int> s_i;
typedef stack<string> s_s;
typedef stack<double> s_d;
typedef stack<ll> s_ll;
typedef stack<char> s_c;
typedef map<ll,ll> m_ll_ll;
typedef map<int,ll> m_i_ll;
typedef map<string,ll> m_s_ll;
typedef map<char,int> m_c_i;
typedef map<char,ll> m_c_ll;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define per(i,l,r) for(ll i=r;i>=l;i--)
#define eif else if
#define N 1005
#define mm(dp) memset(dp,0,sizeof(dp))
#define mm1(dp) memset(dp,-1,sizeof(dp))
#define mm2(dp) memset(dp,0x3f,sizeof(dp))
#define IT set<int>::iterator
class List1
{
public:
struct node
{
int zhi;
int xi;
struct node *next;
};
struct node *head,*p,*q,*t;
void create(int n,int a[])
{
head=NULL;
for(int i=1;i<=n-1;i+=2)
{
p=(struct node *)malloc(sizeof(struct node));
p->xi=a[i];
p->zhi=a[i+1];
p->next=NULL;
if(head==NULL)
head=p;
else
{
q->next=p;
}
q=p;
}
}
void bianli()
{
t=head;
while(t!=NULL)
{
cout<< t->xi <<" "<< t->zhi <<endl;
t=t->next;
}
}
};
class Fun
{
friend class List1;
public:
typedef struct
{
int zhi,xi;
}STU;
STU stu[2*N+5];
void Add(List1 list1,List1 list2)
{
int i=1;
list1.t=list1.head;
while(list1.t!=NULL)
{
stu[i].zhi=list1.t->zhi;
stu[i].xi=list1.t->xi;
list1.t=list1.t->next;
i++;
}
list2.t=list2.head;
while(list2.t!=NULL)
{
stu[i].zhi=list2.t->zhi;
stu[i].xi=list2.t->xi;
list2.t=list2.t->next;
i++;
}
i--;
rep(j,1,i)
{
rep(k,j+1,i)
{
if(stu[j].zhi>stu[k].zhi)
{
STU stu1;
stu1=stu[j];
stu[j]=stu[k];
stu[k]=stu1;
}
}
}
int flag[i+1];
mm(flag);
rep(j,1,i)
{
if(flag[j]==1)
continue;
//cout<<stu[j].xi<<" "<<stu[j].zhi<<endl;
rep(k,j+1,i)
{
if(stu[k].zhi>stu[j].zhi)
break;
if(stu[j].zhi==stu[k].zhi)
{
stu[j].xi+=stu[k].xi;
flag[k]=1;
}
}
}
cout<<"C(x)=";
int v=0;
int flag1=0;
rep(j,1,i)
{
if(flag[j]==1)
continue;
if(stu[j].xi==0)
continue;
if(v==1&&stu[j].xi>0)
{
cout<<"+";
}
if(stu[j].xi!=1)
{
flag1=1;
cout<<stu[j].xi;
}
v=1;
if(stu[j].zhi==0)
continue;
else
{
if(stu[j].zhi==1)
cout<<"x";
else
cout<<"x^"<<stu[j].zhi;
}
}
if(flag1==0)
cout<<"0";
cout<<endl;
}
void Minus(List1 list1,List1 list3)
{
Add(list1,list3);
}
STU stu1[2*N+5];
STU stu2[2*N+5];
STU stu3[2*N+5];
void Cheng(List1 list1,List1 list2)
{
int i1=1;
list1.t=list1.head;
while(list1.t!=NULL)
{
stu1[i1].zhi=list1.t->zhi;
stu1[i1].xi=list1.t->xi;
list1.t=list1.t->next;
i1++;
}
i1--;
int i2=1;
list2.t=list2.head;
while(list2.t!=NULL)
{
stu2[i2].zhi=list2.t->zhi;
stu2[i2].xi=list2.t->xi;
list2.t=list2.t->next;
i2++;
}
i2--;
int i3=1;
rep(i,1,i1)
{
rep(j,1,i2)
{
stu3[i3].zhi=stu1[i].zhi+stu2[j].zhi;
stu3[i3].xi=stu1[i].xi*stu2[j].xi;
i3++;
}
}
i3--;
rep(j,1,i3)
{
rep(k,j+1,i3)
{
if(stu3[j].zhi>stu3[k].zhi)
{
STU stu0;
stu0=stu3[j];
stu3[j]=stu3[k];
stu3[k]=stu0;
}
}
}
int flag[i3+1];
mm(flag);
rep(j,1,i3)
{
if(flag[j]==1)
continue;
rep(k,j+1,i3)
{
if(stu3[k].zhi>stu3[j].zhi)
break;
if(stu3[j].zhi==stu3[k].zhi)
{
stu3[j].xi+=stu3[k].xi;
flag[k]=1;
}
}
}
cout<<"C(x)=";
int v=0;
int flag2=0;
rep(j,1,i3)
{
if(flag[j]==1)
continue;
if(stu3[j].xi==0)
continue;
if(v==1&&stu3[j].xi>0)
{
cout<<"+";
}
if(stu3[j].xi!=1)
{
flag2=1;
cout<<stu3[j].xi;
}
v=1;
if(stu3[j].zhi==0)
continue;
else
{
if(stu3[j].zhi==1)
cout<<"x";
else
cout<<"x^"<<stu3[j].zhi;
}
}
if(flag2==0)
cout<<"0";
cout<<endl;
}
STU stu4[2*N+5];
void Dao(List1 list1)
{
int i1=1;
list1.t=list1.head;
while(list1.t!=NULL)
{
stu4[i1].zhi=list1.t->zhi;
stu4[i1].xi=list1.t->xi;
list1.t=list1.t->next;
i1++;
}
i1--;
rep(i,1,i1)
{
stu4[i].xi*=stu4[i].zhi;
stu4[i].zhi--;
}
cout<<"C(x)=";
int v=0;
int flag=0;
rep(j,1,i1)
{
if(stu4[j].xi==0)
continue;
if(v==1&&stu4[j].xi>0)
{
cout<<"+";
}
if(stu4[j].xi!=1)
{
cout<<stu4[j].xi;
flag=1;
}
v=1;
if(stu4[j].zhi==0)
continue;
else
{
if(stu4[j].zhi==1)
cout<<"x";
else
cout<<"x^"<<stu4[j].zhi;
}
}
if(flag==0)
cout<<"0";
cout<<endl;
}
void Num(List1 list1,float num)
{
float sum=0;
list1.t=list1.head;
while(list1.t!=NULL)
{
sum=sum+1.0*list1.t->xi*pow(num,list1.t->zhi);
list1.t=list1.t->next;
}
cout<<fixed<< setprecision(2) <<sum<<endl;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
List1 list1;
List1 list2;
List1 list3;
Fun fun;
char ch;
cin>>ch;
while(1)
{
if(ch=='C')
{
int num=0;
int k;
cin>>k;
num++;
int a[N];
while(1)
{
if(k==0&&num%2==1)
break;
a[num]=k;
cin>>k;
num++;
}
num--;
list1.create(num,a);
//list1.bianli();
int aa[N];
num=0;
cin>>k;
num++;
while(1)
{
if(k==0&&num%2==1)
break;
a[num]=k;
if(num%2==1)
aa[num]=-1*k;
else
aa[num]=k;
cin>>k;
num++;
}
num--;
list2.create(num,a);
list3.create(num,aa);
//list2.bianli();
}
eif(ch=='X')
break;
eif(ch=='P')
{
fun.Add(list1,list2);
}
eif(ch=='S')
{
fun.Minus(list1,list3);
}
eif(ch=='M')
{
fun.Cheng(list1,list2);
}
eif(ch=='D')
{
fun.Dao(list1);
}
eif(ch=='V')
{
float num;
cin>>num;
float ans;
fun.Num(list1,num);
}
cin>>ch;
}
return 0;
}