本题要求编写程序,计算2个复数的和、差、积、商。

输入格式:

输入在一行中按照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:

  1. 2 3.08 -2.04 5.06

输出样例1:

  1. (2.0+3.1i) + (-2.0+5.1i) = 8.1i
  2. (2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i
  3. (2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i
  4. (2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i

输入样例2:

  1. 1 1 -1 -1.01

输出样例2:

  1. (1.0+1.0i) + (-1.0-1.0i) = 0.0
  2. (1.0+1.0i) - (-1.0-1.0i) = 2.0+2.0i
  3. (1.0+1.0i) * (-1.0-1.0i) = -2.0i
  4. (1.0+1.0i) / (-1.0-1.0i) = -1.0
  1. #include<stdio.h>
  2. #include<math.h>
  3. //当输入的复数虚部为-时 注意为 a-bi 而不是 a+-bi
  4. //应该还可以简化 但是现在没思路
  5. int compare(double x){
  6. return fabs(x)<0.05?0:1;
  7. }
  8. //void gogogo(double (a1); double (b1); double (a2); double (b2); char op){
  9. void output(double a1, double b1, double a2, double b2, char op){
  10. if(b1>=0 && b2>=0)
  11. printf("(%.1lf+%.1lfi) %c (%.1lf+%.1lfi) = ", a1, b1, op, a2, b2);
  12. else if(b1>=0 && b2<0)
  13. printf("(%.1lf+%.1lfi) %c (%.1lf%.1lfi) = ", a1, b1, op, a2, b2);
  14. else if(b1<0 && b2>=0)
  15. printf("(%.1lf%.1lfi) %c (%.1lf+%.1lfi) = ", a1, b1, op, a2, b2);
  16. else
  17. printf("(%.1lf%.1lfi) %c (%.1lf%.1lfi) = ", a1, b1, op, a2, b2);
  18. }
  19. void result(double a, double b){
  20. if(compare(a)==0){
  21. if(compare(b)==0)
  22. printf("0.0\n");
  23. else
  24. printf("%.1lfi\n", b);
  25. }
  26. else{
  27. if(compare(b)==0)
  28. printf("%.1lf\n", a);
  29. else if(b<0)
  30. printf("%.1lf%.1lfi\n", a, b);
  31. else
  32. printf("%.1lf+%.1lfi\n", a, b);
  33. }
  34. }
  35. int main(){
  36. double a1,b1;
  37. double a2,b2;
  38. scanf("%lf%lf%lf%lf", &a1, &b1, &a2, &b2);
  39. double a, b;//输出用 a为实部 b为虚部
  40. char op;//表达式符号位
  41. //加法
  42. op = '+';
  43. a = a1+a2;
  44. b = b1+b2;
  45. output( a1, b1, a2, b2, op);
  46. result( a, b);
  47. //减法
  48. op = '-';
  49. a = a1 - a2;
  50. b = b1 - b2;
  51. output( a1, b1, a2, b2, op);
  52. result( a, b);
  53. //乘法
  54. op = '*';
  55. a = a1*a2 - b1*b2;
  56. b = a1*b2 + a2*b1;
  57. output( a1, b1, a2, b2, op);
  58. result( a, b);
  59. //除法
  60. op = '/';
  61. a = (a1*a2+b1*b2)/(a2*a2+b2*b2);
  62. b = (a2*b1-a1*b2)/(pow(a2,2)+pow(b2,2));//取平方的方法 pow(x,幂次)
  63. output( a1, b1, a2, b2, op);
  64. result( a, b);
  65. return 0;
  66. }