#include<stdio.h>
struct Complex{
double Re, Im;
};
struct Complex add_Complex(struct Complex lhs, struct Complex rhs);
struct Complex sub_Complex(struct Complex lhs, struct Complex rhs);
struct Complex mul_Complex(struct Complex lhs, struct Complex rhs);
struct Complex div_Complex(struct Complex lhs, struct Complex rhs);
int main(void){
struct Complex x,y,z,u, result;
x.Re=2.0;
x.Im=3.1;
y.Re=-1.2;
y.Im=3.4;
z.Re=5.6;
z.Im=7.8;
u.Re=-9.1;
u.Im=2.3;
printf("x=(%.3e)+(%.3e)j\n", x.Re, x.Im);
printf("y=(%.3e)+(%.3e)j\n", y.Re, y.Im);
result=add_Complex(x, y);
printf("result=x+y=(%.3e)+(%.3e)j\n", result.Re, result.Im);
result=sub_Complex(x, y);
printf("result=x-y=(%.3e)+(%.3e)j\n", result.Re, result.Im);
result=mul_Complex(x, y);
printf("result=x*y=(%.3e)+(%.3e)j\n", result.Re, result.Im);
result=div_Complex(x, y);
printf("result=x/y=(%.3e)+(%.3e)j\n", result.Re, result.Im);
result=sub_Complex(add_Complex(x, div_Complex(mul_Complex(y, x), z)), u);
printf("result=x+y*x/z-u=(%.3e)+(%.3e)j\n", result.Re, result.Im);
return 0;
}
struct Complex add_Complex(struct Complex lhs, struct Complex rhs){
struct Complex result;
result.Re=lhs.Re+rhs.Re;
result.Im=lhs.Im+rhs.Im;
return result;
}
struct Complex sub_Complex(struct Complex lhs, struct Complex rhs){
struct Complex result;
result.Re=lhs.Re-rhs.Re;
result.Im=lhs.Im-rhs.Im;
return result;
}
struct Complex mul_Complex(struct Complex lhs, struct Complex rhs){
struct Complex result;
result.Re=lhs.Re*rhs.Re-lhs.Im*rhs.Im;
result.Im=lhs.Re*rhs.Im+lhs.Im*rhs.Re;
return result;
}
struct Complex div_Complex(struct Complex lhs, struct Complex rhs){
struct Complex result;
if(rhs.Re==0.0 && rhs.Im==0.0){
printf("Division by 0\n");
exit(1);
}
result.Re=(lhs.Re*rhs.Re+lhs.Im*rhs.Im)/(rhs.Re*rhs.Re+rhs.Im*rhs.Im);
result.Im=(lhs.Im*rhs.Re-lhs.Re*rhs.Im)/(rhs.Re*rhs.Re+rhs.Im*rhs.Im);
return result;
}
|