输入格式:
输入在一行中按照a1 b1 a2 b2
的格式给出2个复数C1=a1+b1i
和C2=a2+b2i
的实部和虚部。题目保证C2不为0。
输出格式:
分别在4行中按照(a1+b1i) 运算符 (a2+b2i) = 结果
的格式顺序输出2个复数的和、差、积、商,数字精确到小数点后1位。如果结果的实部或者虚部为0,则不输出。如果结果为0,则输出0.0。
输入样例1:
2 3.08 -2.04 5.06
输出样例1:
(2.0+3.1i) + (-2.0+5.1i) = 8.1i
(2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
(2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
(2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i
输入样例2:
1 1 -1 -1.01
输出样例2:
(1.0+1.0i) + (-1.0-1.0i) = 0.0
(1.0+1.0i) - (-1.0-1.0i) = 2.0+2.0i
(1.0+1.0i) * (-1.0-1.0i) = -2.0i
(1.0+1.0i) / (-1.0-1.0i) = -1.0
#include<stdio.h>
#include<math.h>
//当输入的复数虚部为-时 注意为 a-bi 而不是 a+-bi
//应该还可以简化 但是现在没思路
int compare(double x){
return fabs(x)<0.05?0:1;
}
//void gogogo(double (a1); double (b1); double (a2); double (b2); char op){
void output(double a1, double b1, double a2, double b2, char op){
if(b1>=0 && b2>=0)
printf("(%.1lf+%.1lfi) %c (%.1lf+%.1lfi) = ", a1, b1, op, a2, b2);
else if(b1>=0 && b2<0)
printf("(%.1lf+%.1lfi) %c (%.1lf%.1lfi) = ", a1, b1, op, a2, b2);
else if(b1<0 && b2>=0)
printf("(%.1lf%.1lfi) %c (%.1lf+%.1lfi) = ", a1, b1, op, a2, b2);
else
printf("(%.1lf%.1lfi) %c (%.1lf%.1lfi) = ", a1, b1, op, a2, b2);
}
void result(double a, double b){
if(compare(a)==0){
if(compare(b)==0)
printf("0.0\n");
else
printf("%.1lfi\n", b);
}
else{
if(compare(b)==0)
printf("%.1lf\n", a);
else if(b<0)
printf("%.1lf%.1lfi\n", a, b);
else
printf("%.1lf+%.1lfi\n", a, b);
}
}
int main(){
double a1,b1;
double a2,b2;
scanf("%lf%lf%lf%lf", &a1, &b1, &a2, &b2);
double a, b;//输出用 a为实部 b为虚部
char op;//表达式符号位
//加法
op = '+';
a = a1+a2;
b = b1+b2;
output( a1, b1, a2, b2, op);
result( a, b);
//减法
op = '-';
a = a1 - a2;
b = b1 - b2;
output( a1, b1, a2, b2, op);
result( a, b);
//乘法
op = '*';
a = a1*a2 - b1*b2;
b = a1*b2 + a2*b1;
output( a1, b1, a2, b2, op);
result( a, b);
//除法
op = '/';
a = (a1*a2+b1*b2)/(a2*a2+b2*b2);
b = (a2*b1-a1*b2)/(pow(a2,2)+pow(b2,2));//取平方的方法 pow(x,幂次)
output( a1, b1, a2, b2, op);
result( a, b);
return 0;
}