# 用C++实现简单地多项式运算。（因为是菜鸟，会有一些漏洞）

#include <iostream>
#include <cmath>
using namespace std;

class Data
{
public:
double coef;//系数
int exp;//指数
};

class Poly
{
public:
Data poly[40];//用数组表达多项式 ，设定多项式最高40次，可以更改
Poly()//初始化
{
for(int i=0;i<40;i++)
{
poly[i].coef=0;
poly[i].exp=i;
}
}
void input(double co,int ex)//对多项式进行赋值
{
this->poly[ex].coef=co;
}
void print()//输出多项式
{
int count=1;
for(int i=39;i>=0;i--)
{
if(this->poly[i].coef>0)
{
if(this->poly[i].exp!=0)
{
if(count==1) //使得第一项为正时，之前不输出“+”
{
cout<<this->poly[i].coef<<"x^"<<this->poly[i].exp;
count++;
}
else cout<<"+"<<this->poly[i].coef<<"x^"<<this->poly[i].exp;
}
else
{
if(count==1) //使得第一项为正时，之前不输出“+”
{
cout<<this->poly[i].coef;
count++;
}
else cout<<"+"<<this->poly[i].coef;
}
}
if(this->poly[i].coef<0)
{
if(this->poly[i].exp!=0)
{
cout<<this->poly[i].coef<<"x^"<<this->poly[i].exp;
}
else
cout<<this->poly[i].coef;
}
}
cout<<endl;
}
Poly plus(const Poly &R)const//加法
{
Poly C;
for(int i=0;i<40;i++)
{
C.poly[i].coef=this->poly[i].coef+R.poly[i].coef;
}
cout<<"相加结果为： "<<endl;
C.print();
return C;
}

Poly minus(const Poly &R)const//减法
{
Poly C;
for(int i=0;i<40;i++)
{
C.poly[i].coef=this->poly[i].coef-R.poly[i].coef;
}
cout<<"相减结果为： "<<endl;
C.print();
return C;
}
Poly multiply(const Poly &R)const//乘法 （多项式之间）
{
Poly C;
for(int i=0;i<40;i++)
{
for(int j=0;j<20;j++)
{
for(int k=0;k<20;k++)
{
if(j+k==i)
{
C.poly[i].coef+=this->poly[j].coef*R.poly[k].coef;
}
}
}
}
cout<<"相乘结果为： "<<endl;
C.print();
return C;
}

void multic (int n)// 多项式与常数的乘法
{
Poly C;
for(int i=0;i<40;i++)
{
C.poly[i].coef=this->poly[i].coef*n;
}
cout<<"运算结果为："<<endl;
C.print();

}

void clearzero()//清零
{
for(int i=0;i<40;i++)
{
this->poly[i].coef=0;
this->poly[i].exp=0;
}
cout<<"多项式已清零！"<<endl;
}

void caculate(double x)//确定值带入多项式运算
{
long num=0;
for(int i=0;i<40;i++)
{
num+=this->poly[i].coef*pow(x,i);
}
cout<<"计算结果为： "<<num<<endl;

}

} ;
Poly qiudao(const Poly&R)//求导
{
Poly C=R;
for(int i=0;i<39;i++)
{
C.poly[i].coef=C.poly[i+1].coef*C.poly[i+1].exp;
}
cout<<"求导结果为：" <<endl;
C.print();
return C;
}
void printx()//菜单页面
{
cout<<"  ******************************************************"<<endl;
cout<<"            1  输入要进行操作的两个多项式"<<endl;
cout<<"            2  对多项式进行加法运算"<<endl;
cout<<"            3  对多项式进行减法运算"<<endl;
cout<<"            4  对多项式进行乘法运算"<<endl;
cout<<"            5  多项式与常数的乘法运算"<<endl;
cout<<"            6  对多项式进行求导运算"<<endl;
cout<<"            7  显示创建的多项式"<<endl;
cout<<"            8  确定值带入运算"<<endl;
cout<<"            9  输出多项式"<<endl;
cout<<"            10 帮助"<<endl;
cout<<"            11 退出系统"<<endl;
cout<<"请输入你想进行的操作："<<endl;
}

int main()
{
cout<<"  ******************************************************"<<endl;
cout<<"  **********************多项式计算器********************"<<endl;
Poly A,B;
for(;1;){
int operate;
printx();
cin>>operate;
switch(operate)
{
case 1:
for(int i=0;i<40;i++)//用于新创建时对A.B重新初始化
{
A.poly[i].coef=0;
A.poly[i].exp=i;
B.poly[i].coef=0;
B.poly[i].exp=i;
}
cout<<"请输入第一个您需要计算的多项式的项数: "<<endl;
int num1;
cin>>num1;
cout<<"请输入该多项式：(每两个为一组，第一个为系数，第二个为指数，直接输入数字！)"<<endl;
cout<<"p=";
for(int i=1;i<=num1;i++)
{
char s,p,q;
double co;
int ex;
cin>>co>>ex;
A.input(co,ex);
}
cout<<"请输入第二个您需要计算的多项式的项数 "<<endl;
int num2;
cin>>num2;
cout<<"请输入该多项式："<<endl;;
cout<<"q=";
for(int i=1;i<=num2;i++)
{
char s,p,q;
double co;
int ex;
cin>>co>>ex;
B.input(co,ex);
};break;
case 2:
A.plus(B);break;
case 3:
A.minus(B);break;
case 4:
A.multiply(B);break;
case 5:
cout<<"请输入与之相乘的常数： "<<endl;
int m;
cin>>m;
A.multic(m);
B.multic(m);
break;
case 6:
qiudao(A);
qiudao(B);
break;
case 7:
cout<<"多项式为："<<endl;
A.print();
B.print();
break;
case 8:
cout<<"输入你想带入计算的未知数的值："<<endl;
double n;
cin>>n;
cout<<"带入p为： ";
A.caculate(n);
cout<<"带入q为:  ";
B.caculate(n);
break;
case 9:
A.print();
B.print();break;
case 10:
break;
case 11:
exit(0);

}

}
}

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客