c++多项式计算器
来源:互联网 发布:成都网络推广公司 编辑:程序博客网 时间:2024/06/10 02:25
#include<iostream>
#include<cmath>
#include<cstring>
#include<strstream>
#include<stdio.h>
#include<ctype.h>
using namespace std;
class xiang;
ostream &operator<<(ostream &os,xiang &t);
xiang operator+(const xiang &a,const xiang &b);
xiang operator-(const xiang &n,const xiang &m);
class xiang {
public:
float coef; //系数
int expn; //指数
friend ostream &operator<<(ostream &os,xiang &t);
friend xiang operator+(const xiang &a,const xiang &b);
friend xiang operator-(const xiang &n,const xiang &m);
};
ostream &operator<<(ostream &os,xiang &t) ////重载输出
{
if(t.coef>0)
os<<t.coef<<"x^"<<t.expn;
else if(t.coef<0)
os<<t.coef<<"x^"<<t.expn;
else
os<<'0';
return os;
}
xiang operator+(const xiang &a,const xiang &b) //重载加法运算
{
xiang t;
if(a.expn==b.expn)
{
t.coef=a.coef+b.coef;
t.expn=a.expn;
}
return t;
}
xiang operator-(const xiang &n,const xiang &m) //重载减法运算
{
xiang t;
if(n.expn==m.expn)
{
t.coef=n.coef-m.coef;
t.expn=n.expn;
}
return t;
}
void f1()
{
cout<<"=============欢迎进行多项式运算============="<<endl;
cout<<"********************************************"<<endl;
cout<<"想进行加法运算,请输入'1'"<<endl;
cout<<"想进行减法运算,请输入'2'"<<endl;
cout<<"加法的带入求值,请输入'3'"<<endl;
cout<<"减法的带入求值,请输入'4'"<<endl;
cout<<"想进行乘法运算,请输入'5'"<<endl;
cout<<"乘法的带入求值,请输入'6'"<<endl;
cout<<"想结束运算,请输入'9'"<<endl;
cout<<"如果想以上清除运算,请输入'0'"<<endl;
cout<<"********************************************"<<endl;
}
void f2(int n,xiang *A,int m,xiang *B) //////////////加法运算
{
int k;
xiang C[20];
for(k=1;k<=n;k++)
C[k]=A[k];
for(int j=1;j<=m;j++)
{
int g=0;
for(int i=1;i<=n;i++)
{
if(A[i].expn==B[j].expn)
C[i]=A[i]+B[j];
else
g++;
if(g==n)
C[k++]=B[j];
}
}
for(int i=1;i<k-1;i++)
{
cout<<C[i];
if(C[i+1].coef>0)
cout<<"+";
}
cout<<C[k-1];
cout<<endl;
}
void f3(int n,xiang *A,int m,xiang *B) ////////////////减法运算
{
int k;
xiang C[20];
for(k=1;k<=n;k++)
{
C[k]=A[k];
}
for(int j=1;j<=m;j++)
{
int g=0;
for(int i=1;i<=n;i++)
if(A[i].expn==B[j].expn)
C[i]=A[i]-B[j];
else
g++;
if(g==n)
{
C[k].coef=-B[j].coef;
C[k].expn=B[j].expn;
k++;
}
}
for(int i=1;i<k-1;i++)
{
cout<<C[i];
if(C[i+1].coef>0)
cout<<"+";
}
cout<<C[k-1];
cout<<endl;
}
void f4(float lol, int tt, int zz, float ar1[], int Ar1[], float ar2[], int Ar2[]) {
int i, j;
float jj, sum1 = 0.0, yy, sum2 = 0.0, sum;
for(i = 1; i <= tt; i++) {
jj=ar1[i]*pow(lol,Ar1[i]);
sum1+=jj;
}
for(j = 1; j <= zz; j++) {
yy = ar2[j]*pow(lol,Ar2[j]);
sum2+=yy;
}
sum = sum1 + sum2;
cout<<sum<<endl;
}
void f5(float lol, int tt, int zz, float ar1[], int Ar1[],float ar2[], int Ar2[]) {
int i, j;
float jj, sum1 = 0.0, yy, sum2 = 0.0, sum;
for(i = 1; i <= tt; i++) {
jj=ar1[i]*pow(lol,Ar1[i]);
sum1+=jj;
}
for(j = 1; j <= zz; j++) {
yy = ar2[j]*pow(lol,Ar2[j]);
sum2+=yy;
}
sum = sum1 - sum2;
cout<<sum<<endl;
}
void f6(int tt, float qq, float ar1[], int Ar1[]) {
int i, j;
for(i = 1; i <= tt; i++) {
ar1[i] = ar1[i] * qq;
}
for(j = 1; j <= tt-1; j++) {
cout<<ar1[j]<<"x^"<<Ar1[j]<<"+";
}
cout<<ar1[tt]<<"x^"<<Ar1[tt]<<endl;
}
void f7(int tt, float qq, float lol, float ar1[], int Ar1[]) {
int i;
float sum = 0.0, jj, aw;
for(i = 1; i <= tt; i++) {
jj = ar1[i]*pow(lol,Ar1[i]);
sum+=jj;
}
aw = sum * qq;
cout<<"答案是:"<<aw<<endl;
}
int main() {
f1();
float arr1[30], arr3[30];
int arr2[30], arr4[30];
K: int n,i,m,j,k;
int d;
float c;
cout<<"请写出你想输入的多项式的项数 n=";
cin>>n;
xiang A[20],B[20],C[50];
cout<<"请依次输入各项的系数与指数,并敲回车"<<endl;
for(i=1;i<=n;i++) ///////输入一个多项式
{
cin>>c;
arr1[i] = c;
cin>>d;
arr2[i] = d;
C[k].coef=A[i].coef=c;
C[k].expn=A[i].expn=d;
k++;
}
cout<<"请输入另一个多项式的项数m="<<endl;
cin>>m;
cout<<"请依次输入各项的系数与指数,并回车"<<endl;
for(j=1;j<=m;j++) /////输入另一个多项式
{
cin>>c;
arr3[j] = c;
cin>>d;
arr4[j] = d;
B[j].coef=c;
B[j].expn=d;
}
int x;
float lol,qq;
for(;;)
{
cout<<"输入你想进行运算的序号:";
cin>>x;
switch(x)
{
case 1:f2(n,A,m,B);break; ///////加法
case 2:f3(n,A,m,B);break; //////减法
case 3:cout<<"请赋值"<<endl;cin>>lol;f4(lol,n,m,arr1,arr2,arr3,arr4);break;
case 4:cout<<"请赋值"<<endl;cin>>lol;f5(lol,n,m,arr1,arr2,arr3,arr4);break;
case 5:arr3[30] = 0;arr4[30] = 0;cout<<"请输入常数"<<endl;cin>>qq;f6(n,qq,arr1,arr2);break; //////乘法
case 6:arr3[30] = 0;arr4[30] = 0;cout<<"请输入常数,然后对x赋值"<<endl;cin>>qq>>lol;f7(n,qq,lol,arr1,arr2);break;
case 0:
system("cls");
f1();
goto K;
break; //清除数据
}
if(x==9)
{
cout<<"======================================="<<endl;
break;
}
}
}
#include<cmath>
#include<cstring>
#include<strstream>
#include<stdio.h>
#include<ctype.h>
using namespace std;
class xiang;
ostream &operator<<(ostream &os,xiang &t);
xiang operator+(const xiang &a,const xiang &b);
xiang operator-(const xiang &n,const xiang &m);
class xiang {
public:
float coef; //系数
int expn; //指数
friend ostream &operator<<(ostream &os,xiang &t);
friend xiang operator+(const xiang &a,const xiang &b);
friend xiang operator-(const xiang &n,const xiang &m);
};
ostream &operator<<(ostream &os,xiang &t) ////重载输出
{
if(t.coef>0)
os<<t.coef<<"x^"<<t.expn;
else if(t.coef<0)
os<<t.coef<<"x^"<<t.expn;
else
os<<'0';
return os;
}
xiang operator+(const xiang &a,const xiang &b) //重载加法运算
{
xiang t;
if(a.expn==b.expn)
{
t.coef=a.coef+b.coef;
t.expn=a.expn;
}
return t;
}
xiang operator-(const xiang &n,const xiang &m) //重载减法运算
{
xiang t;
if(n.expn==m.expn)
{
t.coef=n.coef-m.coef;
t.expn=n.expn;
}
return t;
}
void f1()
{
cout<<"=============欢迎进行多项式运算============="<<endl;
cout<<"********************************************"<<endl;
cout<<"想进行加法运算,请输入'1'"<<endl;
cout<<"想进行减法运算,请输入'2'"<<endl;
cout<<"加法的带入求值,请输入'3'"<<endl;
cout<<"减法的带入求值,请输入'4'"<<endl;
cout<<"想进行乘法运算,请输入'5'"<<endl;
cout<<"乘法的带入求值,请输入'6'"<<endl;
cout<<"想结束运算,请输入'9'"<<endl;
cout<<"如果想以上清除运算,请输入'0'"<<endl;
cout<<"********************************************"<<endl;
}
void f2(int n,xiang *A,int m,xiang *B) //////////////加法运算
{
int k;
xiang C[20];
for(k=1;k<=n;k++)
C[k]=A[k];
for(int j=1;j<=m;j++)
{
int g=0;
for(int i=1;i<=n;i++)
{
if(A[i].expn==B[j].expn)
C[i]=A[i]+B[j];
else
g++;
if(g==n)
C[k++]=B[j];
}
}
for(int i=1;i<k-1;i++)
{
cout<<C[i];
if(C[i+1].coef>0)
cout<<"+";
}
cout<<C[k-1];
cout<<endl;
}
void f3(int n,xiang *A,int m,xiang *B) ////////////////减法运算
{
int k;
xiang C[20];
for(k=1;k<=n;k++)
{
C[k]=A[k];
}
for(int j=1;j<=m;j++)
{
int g=0;
for(int i=1;i<=n;i++)
if(A[i].expn==B[j].expn)
C[i]=A[i]-B[j];
else
g++;
if(g==n)
{
C[k].coef=-B[j].coef;
C[k].expn=B[j].expn;
k++;
}
}
for(int i=1;i<k-1;i++)
{
cout<<C[i];
if(C[i+1].coef>0)
cout<<"+";
}
cout<<C[k-1];
cout<<endl;
}
void f4(float lol, int tt, int zz, float ar1[], int Ar1[], float ar2[], int Ar2[]) {
int i, j;
float jj, sum1 = 0.0, yy, sum2 = 0.0, sum;
for(i = 1; i <= tt; i++) {
jj=ar1[i]*pow(lol,Ar1[i]);
sum1+=jj;
}
for(j = 1; j <= zz; j++) {
yy = ar2[j]*pow(lol,Ar2[j]);
sum2+=yy;
}
sum = sum1 + sum2;
cout<<sum<<endl;
}
void f5(float lol, int tt, int zz, float ar1[], int Ar1[],float ar2[], int Ar2[]) {
int i, j;
float jj, sum1 = 0.0, yy, sum2 = 0.0, sum;
for(i = 1; i <= tt; i++) {
jj=ar1[i]*pow(lol,Ar1[i]);
sum1+=jj;
}
for(j = 1; j <= zz; j++) {
yy = ar2[j]*pow(lol,Ar2[j]);
sum2+=yy;
}
sum = sum1 - sum2;
cout<<sum<<endl;
}
void f6(int tt, float qq, float ar1[], int Ar1[]) {
int i, j;
for(i = 1; i <= tt; i++) {
ar1[i] = ar1[i] * qq;
}
for(j = 1; j <= tt-1; j++) {
cout<<ar1[j]<<"x^"<<Ar1[j]<<"+";
}
cout<<ar1[tt]<<"x^"<<Ar1[tt]<<endl;
}
void f7(int tt, float qq, float lol, float ar1[], int Ar1[]) {
int i;
float sum = 0.0, jj, aw;
for(i = 1; i <= tt; i++) {
jj = ar1[i]*pow(lol,Ar1[i]);
sum+=jj;
}
aw = sum * qq;
cout<<"答案是:"<<aw<<endl;
}
int main() {
f1();
float arr1[30], arr3[30];
int arr2[30], arr4[30];
K: int n,i,m,j,k;
int d;
float c;
cout<<"请写出你想输入的多项式的项数 n=";
cin>>n;
xiang A[20],B[20],C[50];
cout<<"请依次输入各项的系数与指数,并敲回车"<<endl;
for(i=1;i<=n;i++) ///////输入一个多项式
{
cin>>c;
arr1[i] = c;
cin>>d;
arr2[i] = d;
C[k].coef=A[i].coef=c;
C[k].expn=A[i].expn=d;
k++;
}
cout<<"请输入另一个多项式的项数m="<<endl;
cin>>m;
cout<<"请依次输入各项的系数与指数,并回车"<<endl;
for(j=1;j<=m;j++) /////输入另一个多项式
{
cin>>c;
arr3[j] = c;
cin>>d;
arr4[j] = d;
B[j].coef=c;
B[j].expn=d;
}
int x;
float lol,qq;
for(;;)
{
cout<<"输入你想进行运算的序号:";
cin>>x;
switch(x)
{
case 1:f2(n,A,m,B);break; ///////加法
case 2:f3(n,A,m,B);break; //////减法
case 3:cout<<"请赋值"<<endl;cin>>lol;f4(lol,n,m,arr1,arr2,arr3,arr4);break;
case 4:cout<<"请赋值"<<endl;cin>>lol;f5(lol,n,m,arr1,arr2,arr3,arr4);break;
case 5:arr3[30] = 0;arr4[30] = 0;cout<<"请输入常数"<<endl;cin>>qq;f6(n,qq,arr1,arr2);break; //////乘法
case 6:arr3[30] = 0;arr4[30] = 0;cout<<"请输入常数,然后对x赋值"<<endl;cin>>qq>>lol;f7(n,qq,lol,arr1,arr2);break;
case 0:
system("cls");
f1();
goto K;
break; //清除数据
}
if(x==9)
{
cout<<"======================================="<<endl;
break;
}
}
}
1 0
- [C/C++]OJ练习题:一元稀疏多项式计算器
- 一元稀疏多项式计算器
- 一元稀疏多项式计算器
- c++多项式计算器
- java实现多项式计算器
- C++ 多项式计算器
- 【Web】Javascript多项式计算器
- QT多项式计算器UI
- 一元多项式计算器(链式结构)
- 一元稀疏多项式加减法计算器
- 用java的jni调用C语言的API,实现带图形界面的一元稀疏多项式计算器
- 数据结构课程设计一元稀疏多项式计算器
- 用栈结构实现多项式计算器
- 逆波兰计算器加强版:多项式
- 小小项目2:多项式四目计算器
- 计算器.c
- 【C#】计算器
- c 处理多项式问题
- 飞行员配对问题[网络流24题之1]
- 将本地的mongodb迁移到阿里云的mongodb上
- Extjs之自动消失提示框(Ext.example.msg)
- Spring Assert(方法入参检测工具类-断言)
- Java程序员新手老手都离不开八大开发工具
- c++多项式计算器
- List,Set,Map用法区别 以及 hashmap hashtable区别
- uploadifive 改进版 用于HTML5手机网站上传图片
- DMA传输中的内存对齐
- 如何在多台机器上共享IOS证书
- 全套CRC校验
- 线程池的理解
- 特征选择汇总(未完)
- android 应用中加入支付功能(支付宝集成)