C语言关于复数

Python012

C语言关于复数,第1张

#include <stdio.h>

typedef struct {

    float r

    float i

}Complex

Complex readComlexNumber() {

    Complex n

    printf("Input real part:")

    scanf("%f", &n.r)

    printf("Input imaginary part:")

    scanf("%f", &n.i)

    return n

}

Complex sumComplex(Complex a, Complex b) {

    Complex c

    c.r = a.r + b.r

    c.i = a.i + b.i

    return c

}

Complex differeneComplex(Complex a, Complex b) {

    Complex c

    c.r = a.r-b.r

    c.i = a.i-b.i

    return c

}

Complex multiplyComplex(Complex a, Complex b) {

    Complex c

    c.i = a.r * b.i + a.i*b.r

    c.r = a.r * b.r - a.i*b.i

    return c

}

Complex divideComplex(Complex a, Complex b) {

    Complex c

    c.r = (a.r*b.r+a.i*b.i)/(b.r*b.r+b.i*b.i)

    c.i = (a.i*b.r-a.r*b.i)/(b.r*b.r+b.i*b.i)

    return c

}

void printComplex(Complex n) {

    printf("%.2f+%.2fi", n.r, n.i)

}

int main(){

    Complex a, b, c

    printf("Input Complex number a:\n")

    a = readComlexNumber()

    printf("Input Complex number b:\n")

    b = readComlexNumber()

    printf("The 2 Complex a & b is :\n")

    printComplex(a) printf("   and   ") printComplex(b)

    //sum

    c = sumComplex(a, b)

    printf("\n (a+b)=") printComplex(c)

    //diff

    c = differeneComplex(a, b)

    printf("\n (a-b)=") printComplex(c)

    //multiply

    c = multiplyComplex(a, b)

    printf("\n (a*b)=") printComplex(c)

    //divide

    c = divideComplex(a, b)

    printf("\n (a/b)=") printComplex(c)

    return 0

}

在FORTRAN语言中是有复数的。(最早的语言)

表示方法为(a,b)---a实部b虚部。FORTRAN语言可以复数计算。

在C语言中,在VC程序中可能是没有的

你要用可以自己在C语言中用复数计算规则自己扩展。VC程序扩展更方便了,类型与运算操作符重载。