7-5 复数四则运算 (15 分)
码农问题
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
double a;
double b;
}fu;
int pan(double a, double b)
{
if(a==0&& b!=0)
return 1;
else if(a!=0 && b==0)
return 2;
else if(a==0 && b==0)
return 3;
else if(a!=0 && b!=0)
return 0;
}
double qu(double a)
{
double b=(int)(a*10)/10;
double s=a*100;
int p=(int)s%10;
if(abs(p)>=5)
return a<0?(b-0.1):(b+0.1);
else if(b==0)
return 0;
else if(abs(p)<5 && abs(p)>0)
return b;
else if(abs(p)==0)
return a;
}
int main()
{
double a1, b1, a2, b2;
fu f[4];
char ch[4]={
'+', '-', '*', '/'};
scanf("%lf%lf%lf%lf", &a1, &b1, &a2, &b2);
f[0].a=a1+a2;
f[0].b=b1+b2;
f[1].a=a1-a2;
f[1].b=b1-b2;
f[2].a=a1*a2-b1*b2;
f[2].b=a1*b2+a2*b1;
f[3].a=(a1*a2+b1*b2)/(a2*a2+b2*b2);
f[3].b=(b1*a2-a1*b2)/(a2*a2+b2*b2);
for(int i=0; i<4; i++)
{
if(pan(qu(f[i].a), qu(f[i].b))==1)
printf("(%.1lf%+.1lfi) %c (%.1lf%+.1lfi) = %.1lfi\n", a1, b1, ch[i], a2, b2, f[i].b);
else if(pan(qu(f[i].a), qu(f[i].b))==2)
printf("(%.1lf%+.1lfi) %c (%.1lf%+.1lfi) = %.1lf\n", a1, b1, ch[i], a2, b2, f[i].a);
else if(pan(qu(f[i].a), qu(f[i].b))==3)
printf("(%.1lf%+.1lfi) %c (%.1lf%+.1lfi) = 0.0\n", a1, b1, ch[i], a2, b2);
else if(pan(qu(f[i].a), qu(f[i].b))==0)
printf("(%.1lf%+.1lfi) %c (%.1lf%+.1lfi) = %.1lf%+.1lfi\n", a1, b1, ch[i], a2, b2, f[i].a, f[i].b);
}
return 0;
}