BJFU-OJ 程序设计 C++实验题
Complex类
描述
实现以下复数类Complex,通过运算符重截,实现复数的输入输出以及相关运算。
class Complex
{
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0);
Complex & operator+=(const Complex &);
Complex & operator-=(const Complex &);
Complex & operator*=(const Complex &);
Complex & operator/=(const Complex &);
friend Complex operator+(const Complex &, const Complex &);
friend Complex operator-(const Complex &, const Complex &);
friend Complex operator*(const Complex &, const Complex &);
friend Complex operator/(const Complex &, const Complex &);
friend bool operator==(const Complex &, const Complex &);
friend bool operator!=(const Complex &, const Complex &);
friend ostream & operator<<(ostream &, const Complex &);
friend istream & operator>>(istream &, Complex &);
};
通过以下主函数测试:
int main()
{
Complex c1, c2;
cin >> c1 >> c2;
cout << "c1 = " << c1 << “\n” << "c2 = " << c2 << endl;
cout << "c1+c2 = " << c1 + c2 << endl;
cout << "c1-c2 = " << c1 - c2 << endl;
cout << "c1*c2 = " << c1 * c2 << endl;
cout << "c1/c2 = " << c1 / c2 << endl;
cout << (c1 += c2) << endl;
cout << (c1 -= c2) << endl;
cout << (c1 *= c2) << endl;
cout << (c1 /= c2) << endl;
cout << (c1 == c2) << " " << (c1 != c2) << endl;
return 0;
}
输入
输入有两行,每行输入两个表示复数c1和c2的浮点数。
输出
输出一共有11行,分别表示复数之间的各项操作,具体参见主函数和输出样例
输入样例 1
-4 6
2 5
输出样例 1
c1 = -4 + 6i
c2 = 2 + 5i
c1+c2 = -2 + 11i
c1-c2 = -6 + 1i
c1*c2 = -38 + -8i
c1/c2 = 0.758621 + 1.10345i
-2 + 11i
-4 + 6i
-38 + -8i
-4 + 6i
0 1
提示
复数加法公式:(a + bi) + (c + di) = (a + c) + (b + d)i
复数减法公式:(a + bi) - (c + di) = (a - c) + (b - d)i
复数乘法公式:(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
复数除法公式:(a + bi) / (c + di) = [(ac + bd) / (c * c + d * d)] + [(bc - ad) / (c * c + d * d)]i
#include <iostream>
using namespace std;
class Complex
{
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0);
Complex& operator+=(const Complex&);
Complex& operator-=(const Complex&);
Complex& operator*=(const Complex&);
Complex& operator/=(const Complex&);
<