仅供本人学习记录
复数形如k = a + bi, a是实部,b是虚部。定义类形如
class complex
{
public:
…
private:
double real ;
double imaginary ;
}
成员函数要求包括:
(a) 重载运算符”+”和”-”,实现两个复数的加减运算,重载 ”==”, ”!=” ,”<” ,” >” 实现两个复数的比较运算,注意复数的">“和”<"的比较,当虚部不等于0时,规定按复数的模的大小进行比较,当虚部为0时,则直接比较实部
(b) 设置实部函数 setReal(double realVal), 设置虚部函数setImag(double imagVal)
© 返回实部函数getReal() ,返回虚部函数getImag()
(d) 重载流运算符”>>”, ”<<” 实现复数的输入输出(注意不能用友元函数)
主函数的测试代码如下:
int main()
{
complex c1, c2, c3, c4, c5 ;
cin >> c1 >> c2 ;
c3 = c1 + c2 ;
c4 = c1 - c2 ;
if ( c3 != c4 )
{
if (c3 < c4)
{
cout << “c3 is less than c4” ;
c5 = c4 - c3 ;
}
if ( c3 > c4 )
{
cout << “c3 is greater than c4” ;
c5 = c3 - c4 ;
}
}
if ( c3 == c4 )
{
cout << “c3 and c4 are equal”;
c5 = c4 ;
}
cout<<endl;
cout << c5 ;
return 0;
}
Input
以a+bi的形式输入两个复数,一行输入一个复数,
Output
第1行输出c3和c4比较大小的结果,第2行输出c5, 实部,虚部均保留两位小数
Sample Input
3+4i
5-6i
Sample Output
c3 is less than c4
-10.00+12.00i
HINT
输入按 a+bi 的格式,可以参考以下方法(最好自己想想还有什么其他的方法进行输入)
double real, imag;
char c1,c2;
is>>real>>c1>>imag>>c2;
注意当读取的c1是 “-” 时,需要将imag变成负数
#include"iostream"
#include"iomanip"
using namespace std;
class complex
{
public:
complex(double x=0,double y=0):real(x),imaginary(y){}
complex operator+(const complex & m) const;
complex operator-(const complex & m) const;
bool operator==(const complex & m) const;
bool operator!=(const complex & m) const;
bool operator>(const complex & m) const;
bool operator<(const complex & m) const;
double setReal(double realVal){real=realVal;return 0;}
double setImag(double imagVal){imaginary=imagVal;return 0;}
double getReal(){return real;}
double getImag(){return imaginary;}
private:
double real ;
double imaginary ;
};
complex complex::operator+(const complex & m) const//复习第十章const & 的用法和含义
{
complex mid;
mid.real=real+m.real ;
mid.imaginary =imaginary+m.imaginary;
return mid;//试试返回&mid会怎么样
}
complex complex::operator-(const complex & m) const
{
complex mid;
mid.real=real-m.real ;
mid.imaginary =imaginary-m.imaginary;
return mid;
}
bool complex::operator==(const complex & m) const
{
if(real==m.real && imaginary==m.imaginary)
return true;
else
return false;
}
bool complex::operator!=(const complex & m) const
{
if(real==m.real && imaginary==m.imaginary)
return false;
else
return true;
}
bool complex::operator>(const complex & m) const
{
if(imaginary==0 ||m.imaginary==0)
{
if(real>m.real)
return true;
else
return false;
}
else
{
if(((real*real)+(imaginary*imaginary)) > ((m.real*m.real)+(m.imaginary*m.imaginary)))
return true;
else
return false;
}
}
bool complex::operator<(const complex & m) const
{
if(imaginary==0 ||m.imaginary==0)
{
if(real<m.real)
return true;
else
return false;
}
else
{
if(((real*real)+(imaginary*imaginary)) < ((m.real*m.real)+(m.imaginary*m.imaginary)))
return true;
else
return false;
}
}
ostream& operator<<(ostream& os,complex& m)
{
os<<fixed<<setprecision(2);
if(m.getImag()<0)
os<<m.getReal()<<m.getImag()<<"i"<<endl;
if(m.getImag()>0)
os<<m.getReal()<<"+"<<m.getImag()<<"i"<<endl;
return os;
}
istream& operator>>(istream& is,complex &m)
{
double real, imag;
char c1,c2;
is>>real>>c1>>imag>>c2;
if(c1=='-')
{
m.setReal(real);
m.setImag(-imag);
}
else
{
m.setReal(real);
m.setImag(imag);
}
return is;
}
int main()
{
complex c1, c2, c3, c4, c5 ;
cin >> c1 >> c2 ;
c3 = c1 + c2 ;
c4 = c1 - c2 ;
if ( c3 != c4 )
{
if (c3 < c4)
{
cout << "c3 is less than c4" ;
c5 = c4 - c3 ;
}
if ( c3 > c4 )
{
cout << "c3 is greater than c4" ;
c5 = c3 - c4 ;
}
}
if ( c3 == c4 )
{
cout << "c3 and c4 are equal";
c5 = c4 ;
}
cout<<endl;
cout << c5 ;
return 0;
}