定义一个复数类Complex,重载运算符+,-,*,/,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数之和、差、积和商。 用c语言
时间: 2024-11-22 10:43:29 浏览: 43
在C语言中,为了创建一个复数类(Complex),我们可以定义结构体来存储实部(real)和虚部(imaginary),然后通过重载`+`, `-`, `*`, `/`运算符来实现复数的算术操作。这里是一个简单的例子:
```c
#include <stdio.h>
#include <math.h>
typedef struct {
double real;
double imag;
} Complex;
// 运算符重载函数 - 加法
Complex operator+(const Complex &a, const Complex &b) {
Complex result = {a.real + b.real, a.imag + b.imag};
return result;
}
// 减法
Complex operator-(const Complex &a, const Complex &b) {
Complex result = {a.real - b.real, a.imag - b.imag};
return result;
}
// 乘法
Complex operator*(const Complex &a, const Complex &b) {
double real = a.real * b.real - a.imag * b.imag;
double imag = a.real * b.imag + a.imag * b.real;
Complex result = {real, imag};
return result;
}
// 除法,需要处理除数为零的情况
Complex operator/(const Complex &a, const Complex &b) {
double denominator = b.real * b.real + b.imag * b.imag;
if (denominator == 0) {
printf("Error: Division by zero is not allowed.\n");
return {NaN, NaN}; // 使用浮点数的Not-a-Number表示错误
}
double real = (a.real * b.real + a.imag * b.imag) / denominator;
double imag = (a.imag * b.real - a.real * b.imag) / denominator;
Complex result = {real, imag};
return result;
}
int main() {
Complex num1 = {3.0, 4.0}; // 第一个复数
Complex num2 = {1.0, -2.0}; // 第二个复数
// 操作示例
Complex sum = num1 + num2;
Complex diff = num1 - num2;
Complex product = num1 * num2;
Complex quotient;
quotient = num1 / num2; // 注意这里可能会有除零错误
printf("Sum: %f + %fi = (%f, %fi)\n", num1.real, num1.imag, sum.real, sum.imag);
printf("Difference: %f - %fi = (%f, %fi)\n", num1.real, num1.imag, diff.real, diff.imag);
printf("Product: %f * %fi = (%f, %fi)\n", num1.real, num1.imag, product.real, product.imag);
// 如果没有错误,输出商
if (!isnan(quotient.real)) {
printf("Quotient: %f / %fi = (%f, %fi)\n", num1.real, num2.real, quotient.real, quotient.imag);
}
return 0;
}
```
在这个示例中,我们定义了一个名为`Complex`的结构,并实现了`+`, `-`, `*`, 和 `/`运算符。在主函数中展示了如何计算两个复数的和、差、积和商。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.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)