91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

使用C語言處理復數有什么技巧

小樊
83
2024-07-17 22:50:53
欄目: 編程語言

處理復數時,可以使用結構體來表示復數的實部和虛部,然后定義相應的操作函數來實現復數的加減乘除等運算。下面是一個簡單的示例代碼:

#include <stdio.h>

typedef struct {
    double real;
    double imaginary;
} Complex;

Complex add(Complex c1, Complex c2) {
    Complex result;
    result.real = c1.real + c2.real;
    result.imaginary = c1.imaginary + c2.imaginary;
    return result;
}

Complex subtract(Complex c1, Complex c2) {
    Complex result;
    result.real = c1.real - c2.real;
    result.imaginary = c1.imaginary - c2.imaginary;
    return result;
}

Complex multiply(Complex c1, Complex c2) {
    Complex result;
    result.real = c1.real * c2.real - c1.imaginary * c2.imaginary;
    result.imaginary = c1.real * c2.imaginary + c1.imaginary * c2.real;
    return result;
}

void printComplex(Complex c) {
    if (c.imaginary >= 0) {
        printf("%.2f + %.2fi\n", c.real, c.imaginary);
    } else {
        printf("%.2f - %.2fi\n", c.real, -c.imaginary);
    }
}

int main() {
    Complex c1 = {3.0, 4.0};
    Complex c2 = {1.0, -2.0};

    Complex sum = add(c1, c2);
    Complex difference = subtract(c1, c2);
    Complex product = multiply(c1, c2);

    printf("Sum: ");
    printComplex(sum);

    printf("Difference: ");
    printComplex(difference);

    printf("Product: ");
    printComplex(product);

    return 0;
}

這段代碼定義了一個Complex結構體,包含了實部和虛部,然后定義了加法、減法、乘法等操作函數,并在main函數中測試了這些操作。通過結構體和函數的封裝,可以方便地處理復數的運算。

0
禄劝| 中牟县| 平湖市| 青铜峡市| 宁陕县| 英山县| 霞浦县| 广德县| 沁源县| 当雄县| 长治市| 桂阳县| 石渠县| 达尔| 马边| 梅河口市| 石门县| 漠河县| 张北县| 行唐县| 文安县| 和静县| 宽城| 镇安县| 北流市| 乐昌市| 桦南县| 楚雄市| 阳东县| 乌审旗| 都兰县| 凤台县| 白水县| 潍坊市| 额敏县| 中牟县| 仲巴县| 乡宁县| 平原县| 昭平县| 错那县|