对复数的除法运算有瑕疵;如何能在不更改show()函数的情况下使得复数输出能保留位数输出。。。。。
#include <iostream>
#include <cmath>
using namespace std;
class complex{
double real;
double imag;
public:
complex(double r=0,double i=0):real(r),imag(i){
}
double Real(){return real;}
double Imag(){return imag;}
complex operator+(const complex &c);
complex operator+(const double &d){real+=d; }
complex operator=(const complex &c);
complex operator-(const complex &c);
complex operator-(const double &d){real-=d; }
complex operator*(const complex &c);
complex operator*(const double &d){real*=d;imag*=d;}
complex operator/(const double &d){real/=d;imag/=d;}
complex operator/(const complex &c);
void show();
};
/*
//该法会改变第一个数据的数据,如c3=c2+c1;用此加法会使c2与c3相等(均是计算后的数据)
complex complex::operator+(const complex &c){
real+=c.real;
imag+=c.imag;
return *this; //
}
*/
//该法不会改变第一个数据的数据
complex complex::operator+(const complex &c){
complex test;
test.real=real+c.real;
test.imag=imag+c.imag;
return test;
}
/* //用该法定义赋值行为将不会改变*this的数据
complex complex::operator=(const complex &c){
complex test;
test.real=c.real;
test.imag=c.imag;
return test;
}
*/
complex complex::operator=(const complex &c){
real=c.real; //类似于拷贝构造函数
imag=c.imag;
return *this;
}
complex complex::operator-(const complex &c){
complex test;
test.real=real-c.real;
test.imag=imag-c.imag;
return test;
}
complex complex::operator*(const complex &c)
{
complex test;
test.real=real*c.real-imag*c.imag;
test.imag=real*c.imag+imag*c.real;
return test;
}
complex complex::operator/(const complex &c)
{
complex test;
test.real=(real*c.real+imag*c.imag)/(c.real*c.real+c.imag*c.imag);
test.imag=(real*c.imag-imag*c.real)/(c.real*c.real+c.imag*c.imag);
return test;
}
void complex::show(){
if(real==0&&imag==0)
cout<<0<<endl;
else if(real!=0&&imag==0)
cout<<real<<endl;
else if(real!=0) //与下面对imag的处理情况
cout<<real;
if(imag>0)
{
if(real!=0)
cout<<"+";
if(imag!=1)
cout<<imag<<"i"<<endl;
else
cout<<"i"<<endl;
}
else if(imag<0)
{
if(imag!=-1)
cout<<"-"<<abs(imag)<<"i"<<endl;
else
cout<<"-i"<<endl;
}
}
int main()
{
//complex c1(1,3),c2(0,1),c3(1,0),c4(-1,1),c5(1,-1),c6(-1,-1);
double a2,b2,a1,b1;
cin>>a1>>b1>>a2>>b2;
complex c1(a1,b1),c2(a2,b2),c;
c.show();
c1.show();
c2.show();
c=c1/c2;
c.show();
return 0;
}
带有菜单的简易复数计算器
#include <iostream>
#include <cmath>
using namespace std;
class complex{
double real;
double imag;
public:
complex(double r=0,double i=0):real(r),imag(i){
}
double Real(){return real;}
double Imag(){return imag;}
complex operator+(const complex &c);
complex operator+(const double &d){real+=d; }
complex operator=(const complex &c);
complex operator-(const complex &c);
complex operator-(const double &d){real-=d; }
complex operator*(const complex &c);
complex operator*(const double &d){real*=d;imag*=d;}
complex operator/(const double &d){real/=d;imag/=d;}
complex operator/(const complex &c);
void show();
};
/*
//该法会改变第一个数据的数据,如c3=c2+c1;用此加法会使c2与c3相等(均是计算后的数据)
complex complex::operator+(const complex &c){
real+=c.real;
imag+=c.imag;
return *this; //
}
*/
//该法不会改变第一个数据的数据
complex complex::operator+(const complex &c){
complex test;
test.real=real+c.real;
test.imag=imag+c.imag;
return test;
}
/* //用该法定义赋值行为将不会改变*this的数据
complex complex::operator=(const complex &c){
complex test;
test.real=c.real;
test.imag=c.imag;
return test;
}
*/
complex complex::operator=(const complex &c){
real=c.real; //类似于拷贝构造函数
imag=c.imag;
return *this;
}
complex complex::operator-(const complex &c){
complex test;
test.real=real-c.real;
test.imag=imag-c.imag;
return test;
}
complex complex::operator*(const complex &c)
{
complex test;
test.real=real*c.real-imag*c.imag;
test.imag=real*c.imag+imag*c.real;
return test;
}
complex complex::operator/(const complex &c)
{
complex test;
test.real=(real*c.real+imag*c.imag)/(c.real*c.real+c.imag*c.imag);
test.imag=(imag*c.real-real*c.imag)/(c.real*c.real+c.imag*c.imag);
return test;
}
void complex::show(){
if(real==0&&imag==0)
cout<<0;
else if(real!=0&&imag==0)
cout<<real;
else if(real!=0) //与下面对imag的处理情况
cout<<real;
if(imag>0)
{
if(real!=0)
cout<<"+";
if(imag!=1)
cout<<imag<<"i";
else
cout<<"i";
}
else if(imag<0)
{
if(imag!=-1)
cout<<"-"<<abs(imag)<<"i";
else
cout<<"-i";
}
}
int main()
{
int n;
while(1)
{
cout<<"菜单:"<<endl;
cout<<"结束计算请输入0"<<endl;
cout<<"进行加法运算请输入1"<<endl;
cout<<"进行减法运算请输入2"<<endl;
cout<<"进行乘法运算请输入3"<<endl;
cout<<"进行除法运算请输入4"<<endl;
cin>>n;
if(n==0)
break;
double a2,b2,a1,b1;
cin>>a1>>b1>>a2>>b2;
complex c1(a1,b1),c2(a2,b2),c;
cout<<"(";
c1.show();
switch(n)
{
case 1:cout<<")+(";c=c1+c2;;break;
case 2:cout<<")-(";c=c1-c2;break;
case 3:cout<<")*(";c=c1*c2;break;
case 4:cout<<")/(";c=c1/c2;break;
}
c2.show();
cout<<")=(";
c.show();
cout<<")"<<endl;
cout<<"*******************************"<<endl<<endl;;
}
cout<<"谢谢使用";
return 0;
}