C++中求复数的乘方代码
时间: 2024-05-05 19:21:19 浏览: 134
以下是一个简单的 C++ 代码示例,用于计算复数的幂次方:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
struct complex_num {
double real;
double imag;
};
complex_num pow_complex(complex_num c, int n) {
complex_num result;
double modulus = sqrt(c.real * c.real + c.imag * c.imag);
double arg = atan2(c.imag, c.real);
double new_modulus = pow(modulus, n);
double new_arg = arg * n;
result.real = new_modulus * cos(new_arg);
result.imag = new_modulus * sin(new_arg);
return result;
}
int main() {
complex_num c;
int n;
cout << "Enter the real and imaginary parts of the complex number: ";
cin >> c.real >> c.imag;
cout << "Enter the power to raise the complex number to: ";
cin >> n;
complex_num result = pow_complex(c, n);
cout << "Result: " << result.real << " + " << result.imag << "i" << endl;
return 0;
}
```
该程序使用复数结构体存储实部和虚部,并定义了一个 `pow_complex` 函数,该函数接受一个复数和一个整数,并返回该复数的幂次方。函数首先计算复数的模数和幅角,然后将它们分别提高到幂次方,并使用三角函数计算新的实部和虚部,最后将它们组合成一个新的复数并返回。
在 `main` 函数中,用户被要求输入复数和幂次方,并使用 `pow_complex` 函数计算结果。最后,程序输出结果。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.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)