#include<iostream> using namespace std; 3日 class complexf private: double real; double imag; public: complex (double r, double i) : real (r), imag(i)() friend complex operator* (complex &a, complex &b); friend complex operator/ (complex &a, complex &b); void print() LET MA 17 - 13 "<<imag<<")"<<endl: 15 complex operator* (complex &a, complex &b) 17日 18 21 complex |temp; temp.real=a.real*b. real-a.imag *b imag,temp.1mag=a.realtb.imagta.imag*b. real return temp: complex operator/(complex &a complex &b 24₴ ( 28 complex temp; double th t=1/(b.realtb.realtb.imag*b.imag) temp.reassla.realtb.realta.imag*b.imag)*t eaLa.lnag-a-realb.imag*t return temp; int main() 330 1 35 complex c1(2.5,3.7); c1.print(); complex c2(4.2,6.5); cz.printo: return 0:
时间: 2023-12-03 15:42:53 浏览: 197
这段代码有一些语法错误,我帮你修正一下:
```c++
#include<iostream>
using namespace std;
class complex {
private:
double real;
double imag;
public:
complex (double r, double i) : real(r), imag(i) {}
friend complex operator* (complex &a, complex &b);
friend complex operator/ (complex &a, complex &b);
void print() {
cout<<"("<<real<<" + "<<imag<<"i)"<<endl;
}
};
complex operator* (complex &a, complex &b) {
complex temp;
temp.real = a.real * b.real - a.imag * b.imag;
temp.imag = a.real * b.imag + a.imag * b.real;
return temp;
}
complex operator/ (complex &a, complex &b) {
double t = 1 / (b.real * b.real + b.imag * b.imag);
complex temp;
temp.real = (a.real * b.real + a.imag * b.imag) * t;
temp.imag = (a.imag * b.real - a.real * b.imag) * t;
return temp;
}
int main() {
complex c1(2.5, 3.7);
c1.print();
complex c2(4.2, 6.5);
c2.print();
complex c3 = c1 * c2;
c3.print();
complex c4 = c1 / c2;
c4.print();
return 0;
}
```
这个程序实现了复数类 `complex` 的乘法和除法运算符重载。在 `main` 函数中,我们创建了两个复数对象 `c1` 和 `c2`,并分别调用了它们的 `print` 方法打印出实部和虚部。然后,我们使用 `*` 运算符计算了它们的乘积,并使用 `/` 运算符计算了它们的商,最后打印出结果。
阅读全文