c++多项式计算器
2016-05-18 13:40
369 查看
#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;
}
}
}
相关文章推荐
- [C++ && Python]简单模拟代码评测系统
- 150809234 赵帅 (宿舍管理系统)2
- C/C++中的switch使用
- C++ 设置基类的析构函数为虚函数
- 关于C语言的指针、链表的原理和各类操作
- C++ 相关知识总结
- C语言编程---socket基础
- C语言中的预处理详解
- SM2算法第十三篇:SM2密钥协商协议的C语言实现
- C语言程序设计飞机游戏的体会
- C++ primerplus 第12章课本上例题
- C++递归求二叉树镜像
- 递归求二叉树的深度
- C++11新特性— auto 和 decltype 区别和联系
- 重新学习《C++Primer5》第11章-关联容器
- C++的iostream标准库介绍+使用详解(转)
- vc2008中ProEssentials控件使用
- c++代码通过JNI调用Java代码时用到的签名信息如何获取
- ofstream的使用方法--超级精细。C++文件写入、读出函数(转)
- c++ error LNK2019问题