C++课程设计……一元多项式简单的计算器
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
os<<t.coef<<"a^"<<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;
}
int x;
for(;;)
{
cout<<"输入你想进行运算的序号:";
cin>>x;
switch(x)
{
case 1:f2(n,A,m,B);break; /////////加法
case 2:f3(n,A,m,B);break; //////、减法
case 0:
system("cls");f1();goto K;break; //清除数据
#include<iostream>
#include<cstring>
#include<strstream>
#include<stdio.h>
using namespace std;
class xiang;
ostream &operator<<(ostream &os,xiang &t);
xiang operator+(const xiang &a,const xiang &b);
{
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 f2(int n,xiang *A,int m,xiang *B) //////////////加法运算
friend xiang operator-(const xiang &n,const xiang &m);
};
ostream &operator<<(ostream &os,xiang &t) ////重载输出
{
if(t.coef>0)
os<<t.coef<<"a^"<<t.expn;
else
if(t.coef<0)
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;
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 main()
{ f1();
K:int n,i,m,j;
cout<<"请写出你想输入的多项式的项数(不要超过十项)n=";
}
void f1()
{
cout<<"========欢迎进行多项式运算=================="<<endl;
cout<<"********************************************"<<endl;
cout<<"想进行加法运算,请输入‘1’"<<endl;
cout<<"想进行减法运算,请输入‘2’"<<endl;
cout<<"想结束运算,请输入‘9’"<<endl;
cout<<"如果想以上清除运算,请输入‘0’"<<endl;
cout<<"********************************************"<<endl;
}
void f3(int n,xiang *A,int m,xiang *B) ////////////////减法运算
}
if(x==9)
{cout<<"======================================="<<endl;break;}
}
}
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 &源自文库);
{
int k;
xiang C[10];
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)
k++;
}
cout<<"请输入另一个多项式的项数m="<<endl;
cin>>m;
cout<<"请依次输入各项的系数与指数,并回车"<<endl;
for(j=1;j<=m;j++) /////输入另一个多项式
{
float c;cin>>c;
int d;cin>>d;
B[j].coef=c;
B[j].expn=d;
cin>>n;
xiang A[10],B[10],C[20];
cout<<"请依次输入各项的系数与指数,并回车"<<endl;
for(i=1;i<=n;i++) ///////输入一个多项式
{
float c;cin>>c;
int d;cin>>d;
C[k].coef=A[i].coef=c;
C[k].expn=A[i].expn=d;
{
int k;
xiang C[10];
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)
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;
}
int x;
for(;;)
{
cout<<"输入你想进行运算的序号:";
cin>>x;
switch(x)
{
case 1:f2(n,A,m,B);break; /////////加法
case 2:f3(n,A,m,B);break; //////、减法
case 0:
system("cls");f1();goto K;break; //清除数据
#include<iostream>
#include<cstring>
#include<strstream>
#include<stdio.h>
using namespace std;
class xiang;
ostream &operator<<(ostream &os,xiang &t);
xiang operator+(const xiang &a,const xiang &b);
{
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 f2(int n,xiang *A,int m,xiang *B) //////////////加法运算
friend xiang operator-(const xiang &n,const xiang &m);
};
ostream &operator<<(ostream &os,xiang &t) ////重载输出
{
if(t.coef>0)
os<<t.coef<<"a^"<<t.expn;
else
if(t.coef<0)
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;
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 main()
{ f1();
K:int n,i,m,j;
cout<<"请写出你想输入的多项式的项数(不要超过十项)n=";
}
void f1()
{
cout<<"========欢迎进行多项式运算=================="<<endl;
cout<<"********************************************"<<endl;
cout<<"想进行加法运算,请输入‘1’"<<endl;
cout<<"想进行减法运算,请输入‘2’"<<endl;
cout<<"想结束运算,请输入‘9’"<<endl;
cout<<"如果想以上清除运算,请输入‘0’"<<endl;
cout<<"********************************************"<<endl;
}
void f3(int n,xiang *A,int m,xiang *B) ////////////////减法运算
}
if(x==9)
{cout<<"======================================="<<endl;break;}
}
}
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 &源自文库);
{
int k;
xiang C[10];
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)
k++;
}
cout<<"请输入另一个多项式的项数m="<<endl;
cin>>m;
cout<<"请依次输入各项的系数与指数,并回车"<<endl;
for(j=1;j<=m;j++) /////输入另一个多项式
{
float c;cin>>c;
int d;cin>>d;
B[j].coef=c;
B[j].expn=d;
cin>>n;
xiang A[10],B[10],C[20];
cout<<"请依次输入各项的系数与指数,并回车"<<endl;
for(i=1;i<=n;i++) ///////输入一个多项式
{
float c;cin>>c;
int d;cin>>d;
C[k].coef=A[i].coef=c;
C[k].expn=A[i].expn=d;
{
int k;
xiang C[10];
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)