C++Write a C++ program that defines a cmplx struct.The cmplx struct object type should include... Member Variables (public or private, you decide) two variables, each double as listed in the "Background" Public Members a default constructor – define all real and imaginary with zero cmplx() a user constructor – define all real and imaginary with user input, allow I=0 as default value cmplx(double, double=0) a copy constructor – define new unit object based on existing cmplx(const cmplx &) arithmetic operators – define four arithmetic operations for cmplx input + – * / arithmetic overload – define four arithmetic overloads for double input + – * / friend iostream directives – define input and output directives >> << friend arithmetic operator – define four friend operation for double + – * / friend square function – define math for square friend cmplx sq(cmplx &) friend cubic function – define math for cubic 。friend cmplx cu(cmplx &)
时间: 2024-02-14 15:06:49 浏览: 69
Here is a sample implementation of the cmplx struct in C++:
```c++
#include<iostream>
#include<cmath>
using namespace std;
struct cmplx {
private:
double real;
double imag;
public:
cmplx(): real(0), imag(0) {}
cmplx(double r, double i = 0): real(r), imag(i) {}
cmplx(const cmplx& c): real(c.real), imag(c.imag) {}
friend cmplx operator+(const cmplx& c1, const cmplx& c2) {
return cmplx(c1.real + c2.real, c1.imag + c2.imag);
}
friend cmplx operator-(const cmplx& c1, const cmplx& c2) {
return cmplx(c1.real - c2.real, c1.imag - c2.imag);
}
friend cmplx operator*(const cmplx& c1, const cmplx& c2) {
return cmplx(c1.real*c2.real - c1.imag*c2.imag, c1.real*c2.imag + c1.imag*c2.real);
}
friend cmplx operator/(const cmplx& c1, const cmplx& c2) {
double d = c2.real*c2.real + c2.imag*c2.imag;
return cmplx((c1.real*c2.real + c1.imag*c2.imag) / d, (c1.imag*c2.real - c1.real*c2.imag) / d);
}
friend cmplx operator+(const cmplx& c, double d) {
return cmplx(c.real + d, c.imag);
}
friend cmplx operator-(const cmplx& c, double d) {
return cmplx(c.real - d, c.imag);
}
friend cmplx operator*(const cmplx& c, double d) {
return cmplx(c.real*d, c.imag*d);
}
friend cmplx operator/(const cmplx& c, double d) {
return cmplx(c.real/d, c.imag/d);
}
friend cmplx operator+(double d, const cmplx& c) {
return c + d;
}
friend cmplx operator-(double d, const cmplx& c) {
return cmplx(d) - c;
}
friend cmplx operator*(double d, const cmplx& c) {
return c*d;
}
friend cmplx operator/(double d, const cmplx& c) {
double d2 = c.real*c.real + c.imag*c.imag;
return cmplx(d*c.real/d2, -d*c.imag/d2);
}
friend istream& operator>>(istream& is, cmplx& c) {
is >> c.real >> c.imag;
return is;
}
friend ostream& operator<<(ostream& os, const cmplx& c) {
os << c.real << " + " << c.imag << "i";
return os;
}
friend cmplx sq(cmplx& c) {
return c*c;
}
friend cmplx cu(cmplx& c) {
return c*c*c;
}
};
int main() {
cmplx a(1, 2), b(3, 4);
cmplx c = a + b;
cmplx d = a - b;
cmplx e = a * b;
cmplx f = a / b;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a + b = " << c << endl;
cout << "a - b = " << d << endl;
cout << "a * b = " << e << endl;
cout << "a / b = " << f << endl;
cmplx g = a + 2.5;
cmplx h = 3.7 - b;
cmplx i = a * 1.5;
cmplx j = b / 2.0;
cout << "a + 2.5 = " << g << endl;
cout << "3.7 - b = " << h << endl;
cout << "a * 1.5 = " << i << endl;
cout << "b / 2.0 = " << j << endl;
cmplx k = 2.5 + a;
cmplx l = 3.7 - b;
cmplx m = 1.5 * a;
cmplx n = 2.0 / b;
cout << "2.5 + a = " << k << endl;
cout << "3.7 - b = " << l << endl;
cout << "1.5 * a = " << m << endl;
cout << "2.0 / b = " << n << endl;
cout << "a^2 = " << sq(a) << endl;
cout << "b^3 = " << cu(b) << endl;
return 0;
}
```
In this implementation, the cmplx struct has two private member variables for the real and imaginary parts. It also has a default constructor, a user constructor with a default value for the imaginary part, and a copy constructor. The struct defines arithmetic operators (+, -, *, /) for cmplx input and overloaded arithmetic operators for double input. It also defines input and output directives and friend math functions for square and cube.
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)