展开全部
#include
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;
}