天梯题集——复数四则运算(fabs)
程序员文章站
2022-06-12 22:48:16
...
复数四则运算
#include<bits/stdc++.h>
using namespace std;
double a1, b1, a2, b2;
void print(char ch, double ans1, double ans2){
printf("(%.1lf", a1);
if(b1>=0)
printf("+");
printf("%.1lfi) %c (%.1lf", b1, ch, a2);
if(b2>=0)
printf("+");
printf("%.1lfi) = ", b2);
//精确到小数点后1位,当ans<0.05时判断ans=0
if(fabs(ans1)<0.05&&fabs(ans2)<0.05)
printf("0.0");
else if(fabs(ans1)<0.05)
printf("%.1lfi", ans2);
else if(fabs(ans2)<0.05)
printf("%.1lf", ans1);
else{
printf("%.1lf", ans1);
if(ans2>0)
printf("+");
printf("%.1lfi", ans2);
}
printf("\n");
return ;
}
int main(){
scanf("%lf%lf%lf%lf", &a1, &b1, &a2, &b2);
print('+', a1+a2, b1+b2);
print('-', a1-a2, b1-b2);
print('*', a1*a2-b1*b2, a1*b2+a2*b1);
print('/', (a1*a2+b1*b2)/(a2*a2+b2*b2), (a2*b1-a1*b2)/(a2*a2+b2*b2));
return 0;
}
上一篇: python3函数