int main() { Cmycomplex z1(3,4),z2(7),z3,z4(z1); double x,y; cin>>x>>y; z3.Set(x,y); cout<<endl; z3=z3+z2; z3.Show(); z4=2+z4; cout<<endl; z4.Show(); z4=z4+2; cout<<endl; z4.Show(); } //StudybarCommentEnd 如题,要求实现: 1、输入一个复数 2、按后缀那样计算 3、Show出结果。 例如输入: 7 8 输出 (14+8i) (5+4i) (7+4i)
时间: 2024-03-06 19:50:37 浏览: 30
这段代码并不是实现输入三个复数按照题目要求计算的代码,而是一个类 Cmycomplex 的测试代码。如果你想实现输入三个复数按照题目要求计算的代码,可以参考下面的实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
class Complex {
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
Complex operator*(const Complex& other) const {
double r = real * other.real - imag * other.imag;
double i = real * other.imag + imag * other.real;
return Complex(r, i);
}
void Show() const {
cout << "(" << real << "+" << imag << "i)";
}
};
int main() {
double x, y;
cin >> x >> y;
Complex z1(14, 8);
Complex z2(5, 4);
Complex z3(x, y);
Complex result = z1 * z2 * z3;
result.Show();
return 0;
}
```
这个代码中,我们定义了一个 Complex 类,表示复数。它包含了两个成员变量,分别表示实部和虚部。我们定义了加法运算符和乘法运算符,分别表示两个复数的加法和乘法。Show 函数用于输出复数。
在 main 函数中,我们首先读入一个复数,然后按照题目要求计算出结果,最后输出结果。