使用C++语句设计一个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 13:14:13 浏览: 194
下面是代码实现:
```cpp
#include<iostream>
#include<cmath>
using namespace std;
class Complex{
public:
Complex(double a=0, double b=0):real(a),imag(b){}
Complex(double a):real(a),imag(0){}
Complex():real(0),imag(0){}
double getRealPart() const {return real;}
double getImaginaryPart() const {return imag;}
Complex add(const Complex& c) const {return Complex(real+c.real, imag+c.imag);}
Complex subtract(const Complex& c) const {return Complex(real-c.real, imag-c.imag);}
string toString() const {
if(imag == 0) return to_string(real);
else if(real == 0) return to_string(imag) + "i";
else if(imag < 0) return to_string(real) + to_string(imag) + "i";
else return to_string(real) + "+" + to_string(imag) + "i";
}
double operator[](int index) const{
if(index == 0) return real;
else if(index == 1) return imag;
else throw invalid_argument("Invalid index");
}
Complex operator+() const {return *this;}
Complex operator-() const {return Complex(-real, -imag);}
Complex& operator++() {
++real;
return *this;
}
Complex operator++(int dummy) {
Complex temp(real, imag);
++real;
return temp;
}
Complex& operator--() {
--real;
return *this;
}
Complex operator--(int dummy) {
Complex temp(real, imag);
--real;
return temp;
}
friend Complex operator+(const Complex& c1, const Complex& c2){
return c1.add(c2);
}
friend Complex operator-(const Complex& c1, const Complex& c2){
return c1.subtract(c2);
}
friend Complex operator+=(Complex& c1, const Complex& c2){
c1 = c1.add(c2);
return c1;
}
friend Complex operator-=(Complex& c1, const Complex& c2){
c1 = c1.subtract(c2);
return c1;
}
friend ostream& operator<<(ostream& os, const Complex& c){
os << c.toString();
return os;
}
friend istream& operator>>(istream& is, Complex& c){
is >> c.real >> c.imag;
return is;
}
private:
double real, imag;
};
int main(){
Complex c1(2.5, 3.0);
Complex c2(1.5);
Complex c3;
cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
cout << "c3 = " << c3 << endl;
cout << "c1[0] = " << c1[0] << endl;
cout << "c1[1] = " << c1[1] << endl;
cout << "+c1 = " << +c1 << endl;
cout << "-c1 = " << -c1 << endl;
cout << "++c1 = " << ++c1 << endl;
cout << "--c1 = " << --c1 << endl;
cout << "c1++ = " << c1++ << endl;
cout << "c1-- = " << c1-- << 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 << "Enter a complex number: ";
Complex c4;
cin >> c4;
cout << "c4 = " << c4 << endl;
return 0;
}
```
输出结果:
```
c1 = 2.5+3i
c2 = 1.5
c3 = 0
c1[0] = 2.5
c1[1] = 3
+c1 = 2.5+3i
-c1 = -2.5-3i
++c1 = 3.5+3i
--c1 = 2.5+3i
c1++ = 2.5+3i
c1-- = 3.5+3i
c1 += c2 = 4+3i
c1 -= c2 = 2.5+3i
c1 + c2 = 4+3i
c1 - c2 = 1+3i
Enter a complex number: 2.7 9.8
c4 = 2.7+9.8i
```
阅读全文