想要定义一个复数类Complex,内有数据成员实部、虚部,另有成员函数,该怎么做?

定义一个复数类Complex,内有数据成员实部、虚部,另有成员函数:构造函数用于初始化数据成员,输出函数,关系运算符重载“==”的重载。编写主函数:创建Complex对象,判断两个复数是否相等(实部相等、虚部也相等)。
输入格式: 测试输入包含若干测试用例,每个测试用例占一行。当读入0 0 0 0时输入结束,相应的结果不要输出。
输入样例:(括号内为说明)
3 5 4 5(a=3+5i, b=4+5i)
3 -5 3 -5(a=3-5i, b=3-5i)
0 5 0 5(a=5i, b=5i)
0 0 0 0
输出样例:
a=3+5i,b=4+5i,a不等于b↙ 
a=3-5i,b=3-5i,a等于b↙ 
a=5i,b=5i,a等于b↙

千巷猫影
浏览 613回答 1
1回答

芜湖不芜

class complex{public:complex(){real=0;imag=0;}complex(double r){real=r;imag=0;}complex(double r,double i){real=r;imag=i;}friend istream & operator >> (istream &,complex &);friend ostream & operator << (ostream &,complex &);private:double real;double imag;};istream & operator >> (istream &input,complex &c){cout<<"please input real part and imaginary part of complex number:";input>>c.real>>c.imag;return input;}ostream & operator << (ostream &output,complex &c){if(c.imag>0)if(c.real==0)output<<c.imag<<"i";else output<<c.real<<"+"<<c.imag<<"i";else if(c.imag<0)if(c.real==0)output<<c.imag<<"i";else output<<c.real<<c.imag<<"i";else output<<c.real;return output;}
打开App,查看更多内容
随时随地看视频慕课网APP