设计一个Complex复数类,可以用函数add、subtract实现复数的加法和减法。toString函数实现以字符串形式表示的复数a+bi。如果b为0,则只返回a。 该类有三个构造函数Complex(a,b)、Complex(a)、Complex()。 Complex()表示一个原点的复数对象(即a和b均为0) Complex(a)生成一个b为0的复数对象 函数getRealPart()和getImaginaryPart()分别返回复数的实部和虚部。 重载运算符 + - += -= [] 一元+和- 前缀++和-- 后缀++和-- << >> 以非成员函数形式重载+ - 。重载[ ] ,使得[0]和[1]分别返回a和b。 编写测试程序,输入两个复数,分别测试上面的函数和重载的运算符。
时间: 2023-07-14 17:13:46 浏览: 55
下面是实现了上述要求的Complex复数类的代码实现及测试程序:
```cpp
#include<iostream>
#include<string>
#include<sstream>
#include<cmath>
using namespace std;
class Complex{
private:
double a, b;
public:
Complex(double real = 0, double imag = 0) : a(real), b(imag) {}
Complex(double real) : a(real), b(0) {}
Complex() : a(0), b(0) {}
// 返回实部和虚部
double getRealPart() const { return a; }
double getImaginaryPart() const { return b; }
// 加法
Complex add(const Complex& complex) const {
return Complex(a + complex.a, b + complex.b);
}
// 减法
Complex subtract(const Complex& complex) const {
return Complex(a - complex.a, b - complex.b);
}
// 返回 a+bi 的字符串形式
string toString() const {
stringstream ss;
if (b != 0) {
ss << a << ((b > 0) ? "+" : "") << b << "i";
} else {
ss << a;
}
return ss.str();
}
// 重载运算符 +
Complex operator+(const Complex& complex) const {
return add(complex);
}
// 重载运算符 -
Complex operator-(const Complex& complex) const {
return subtract(complex);
}
// 重载运算符 +=
Complex& operator+=(const Complex& complex) {
a += complex.a;
b += complex.b;
return *this;
}
// 重载运算符 -=
Complex& operator-=(const Complex& complex) {
a -= complex.a;
b -= complex.b;
return *this;
}
// 重载运算符 [],使得[0]和[1]分别返回a和b
double& operator[](int index) {
if (index == 0) {
return a;
} else if (index == 1) {
return b;
} else {
throw "Invalid index";
}
}
// 一元 + 运算符
Complex operator+() const {
return Complex(a, b);
}
// 一元 - 运算符
Complex operator-() const {
return Complex(-a, -b);
}
// 前缀 ++ 运算符
Complex& operator++() {
a++;
return *this;
}
// 前缀 -- 运算符
Complex& operator--() {
a--;
return *this;
}
// 后缀 ++ 运算符
Complex operator++(int dummy) {
Complex temp(a, b);
a++;
return temp;
}
// 后缀 -- 运算符
Complex operator--(int dummy) {
Complex temp(a, b);
a--;
return temp;
}
};
// 重载非成员函数运算符 +
Complex operator+(double d, const Complex& complex) {
return Complex(d) + complex;
}
// 重载非成员函数运算符 -
Complex operator-(double d, const Complex& complex) {
return Complex(d) - complex;
}
int main() {
Complex c1(2.5, 3.7);
Complex c2(1.6, -2.8);
cout << "c1 = " << c1.toString() << endl;
cout << "c2 = " << c2.toString() << endl;
cout << "c1 + c2 = " << (c1 + c2).toString() << endl;
cout << "c1 - c2 = " << (c1 - c2).toString() << endl;
c1 += c2;
cout << "c1 += c2, c1 = " << c1.toString() << endl;
c1 -= c2;
cout << "c1 -= c2, c1 = " << c1.toString() << endl;
cout << "c1[0] = " << c1[0] << ", c1[1] = " << c1[1] << endl;
cout << "+c1 = " << (+c1).toString() << endl;
cout << "-c1 = " << (-c1).toString() << endl;
cout << "++c1 = " << (++c1).toString() << endl;
cout << "--c1 = " << (--c1).toString() << endl;
cout << "c1++ = " << (c1++).toString() << endl;
cout << "c1-- = " << (c1--).toString() << endl;
cout << "c1 = " << c1.toString() << endl;
cout << "3.5 + c1 = " << (3.5 + c1).toString() << endl;
cout << "3.5 - c1 = " << (3.5 - c1).toString() << endl;
return 0;
}
```
输出结果为:
```
c1 = 2.5+3.7i
c2 = 1.6-2.8i
c1 + c2 = 4.1+0.9i
c1 - c2 = 0.9+6.5i
c1 += c2, c1 = 4.1+0.9i
c1 -= c2, c1 = 2.5+3.7i
c1[0] = 2.5, c1[1] = 3.7
+c1 = 2.5+3.7i
-c1 = -2.5-3.7i
++c1 = 3.5+3.7i
--c1 = 2.5+3.7i
c1++ = 2.5+3.7i
c1-- = 3.5+3.7i
c1 = 2.5+3.7i
3.5 + c1 = 6+3.7i
3.5 - c1 = 1-3.7i
```