zoukankan      html  css  js  c++  java
  • 结构-06. 复数四则运算(15)

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

    输入格式:

    输入在一行中按照“a1 b1 a2 b2”的格式给出2个复数C1=a1+b1*i和C2=a2+b2*i的实部和虚部。题目保证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
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <iostream>
      4 #include <string.h>
      5 #include <string>
      6 #include <math.h>
      7 #define EPSILON 0.1
      8 
      9 using namespace::std; 
     10 int shuchu(double a,double b,double c,double d,char fuhao)
     11 {
     12     //if(b<EPSILON&&b>-EPSILON)b=0;
     13     //if(d<EPSILON&&d>-EPSILON)d=0;
     14     if(b<0)
     15     {
     16         if(d<0)
     17         {
     18             printf("(%.1f%.1fi) %c (%.1f%.1fi) = ",a,b,fuhao,c,d);
     19         }
     20         else
     21         {
     22             printf("(%.1f%.1fi) %c (%.1f+%.1fi) = ",a,b,fuhao,c,d);
     23         }
     24     }
     25     else
     26     {
     27         if(d<0)
     28         {
     29             printf("(%.1f+%.1fi) %c (%.1f%.1fi) = ",a,b,fuhao,c,d);
     30         }
     31         else
     32         {
     33             printf("(%.1f+%.1fi) %c (%.1f+%.1fi) = ",a,b,fuhao,c,d);
     34         }
     35     }
     36     return 0;
     37 }
     38 
     39 typedef struct {
     40     double shi;
     41     double xu;
     42 }fushu;
     43 
     44 void printResult(fushu c) {  
     45     if(fabs(c.shi)<EPSILON)c.shi=0;
     46     if(fabs(c.xu)<EPSILON)c.xu=0;  
     47     if(c.shi == 0 && c.xu == 0)  
     48         printf("%.1f
    ", c.shi); 
     49     if(c.shi == 0 && c.xu)  
     50         printf("%.1fi
    ", c.xu);   
     51     if(c.shi && c.xu > 0)  
     52         printf("%.1f+%.1fi
    ", c.shi, c.xu);  
     53     if(c.shi && c.xu < 0)  
     54         printf("%.1f%.1fi
    ", c.shi, c.xu);  
     55     if(c.shi && c.xu == 0)  
     56         printf("%.1f
    ", c.shi);  
     57     
     58 }  
     59 
     60 int main(){
     61     fushu a[2],temp;
     62     for(int i=0;i<2;i++)
     63     {
     64     scanf("%lf %lf",&a[i].shi,&a[i].xu);
     65     }
     66     
     67    //加法 
     68     shuchu(a[0].shi,a[0].xu,a[1].shi,a[1].xu,'+');
     69     temp.shi=a[0].shi+a[1].shi;
     70     temp.xu=a[0].xu+a[1].xu;
     71     //printResult(temp);
     72     if(fabs(temp.shi)<EPSILON)
     73     {
     74         if(temp.xu<EPSILON&&temp.xu>-EPSILON)
     75         {
     76          printf("0.0
    ");
     77         }
     78         else
     79         {
     80         printf("%.1fi
    ",temp.xu);    
     81         }
     82     }else
     83     {
     84        if(temp.xu<EPSILON&&temp.xu>-EPSILON)
     85         {
     86          printf("%.1f
    ",temp.shi);
     87         }
     88         else if(temp.xu>=EPSILON)
     89         {
     90         printf("%.1f+%.1fi
    ",temp.shi,temp.xu);    
     91         }
     92         else
     93         {
     94         printf("%.1f%.1fi
    ",temp.shi,temp.xu);    
     95         }
     96     }
     97     
     98     shuchu(a[0].shi,a[0].xu,a[1].shi,a[1].xu,'-');
     99     temp.shi=a[0].shi-a[1].shi;
    100     temp.xu=a[0].xu-a[1].xu;
    101     //printResult(temp);
    102      if(temp.shi<EPSILON&&temp.shi>-EPSILON)
    103     {
    104         if(temp.xu<EPSILON&&temp.xu>-EPSILON)
    105         {
    106          printf("0.0
    ");
    107         }
    108         else
    109         {
    110         printf("%.1fi
    ",temp.xu);    
    111         }
    112     }else
    113     {
    114         if(temp.xu<EPSILON&&temp.xu>-EPSILON)
    115         {
    116          printf("%.1f
    ",temp.shi);
    117         }
    118         else if(temp.xu>=EPSILON)
    119         {
    120         printf("%.1f+%.1fi
    ",temp.shi,temp.xu);    
    121         }
    122         else
    123         {
    124         printf("%.1f%.1fi
    ",temp.shi,temp.xu);    
    125         }
    126     }
    127     
    128     shuchu(a[0].shi,a[0].xu,a[1].shi,a[1].xu,'*');
    129     
    130     temp.shi=a[0].shi*a[1].shi-a[0].xu*a[1].xu;
    131     temp.xu =a[0].shi*a[1].xu+a[0].xu*a[1].shi;
    132     //printResult(temp);
    133    if(temp.shi<EPSILON&&temp.shi>-EPSILON)
    134     {
    135         if(temp.xu<EPSILON&&temp.xu>-EPSILON)
    136         {
    137          printf("0.0
    ");
    138         }
    139         else
    140         {
    141         printf("%.1fi
    ",temp.xu);    
    142         }
    143     }else
    144     {
    145         if(temp.xu<EPSILON&&temp.xu>-EPSILON)
    146         {
    147          printf("%.1f
    ",temp.shi);
    148         }
    149         else if(temp.xu>=EPSILON)
    150         {
    151         printf("%.1f+%.1fi
    ",temp.shi,temp.xu);    
    152         }
    153         else
    154         {
    155         printf("%.1f%.1fi
    ",temp.shi,temp.xu);    
    156         }
    157     }
    158     
    159     shuchu(a[0].shi,a[0].xu,a[1].shi,a[1].xu,'/');  
    160     temp.shi=(a[0].shi*a[1].shi+a[0].xu*a[1].xu)/(a[1].shi*a[1].shi+a[1].xu*a[1].xu);
    161     temp.xu=(a[0].xu*a[1].shi-a[0].shi*a[1].xu)/(a[1].shi*a[1].shi+a[1].xu*a[1].xu);
    162     //printResult(temp);
    163 if(temp.shi<EPSILON&&temp.shi>-EPSILON)
    164     {
    165         if(temp.xu<EPSILON&&temp.xu>-EPSILON)
    166         {
    167          printf("0.0
    ");
    168         }
    169         else
    170         {
    171         printf("%.1fi
    ",temp.xu);    
    172         }
    173     }else
    174     {
    175         if(temp.xu<EPSILON&&temp.xu>-EPSILON)
    176         {
    177          printf("%.1f
    ",temp.shi);
    178         }
    179         else if(temp.xu>=EPSILON)
    180         {
    181         printf("%.1f+%.1fi
    ",temp.shi,temp.xu);    
    182         }
    183         else
    184         {
    185         printf("%.1f%.1fi
    ",temp.shi,temp.xu);    
    186         }
    187     }
    188     return 0;
    189 }
  • 相关阅读:
    Django集成Markdown编辑器【附源码】
    Django+JWT实现Token认证
    Docker环境的持续部署优化实践
    2018-行远自迩,登高自卑
    SVN Hooks的介绍及使用
    Django开发密码管理表实例【附源码】
    Django+Echarts画图实例
    Django使用Signals监测model字段变化发送通知
    运维效率之数据迁移自动化
    Python之路(第三十四篇) 网络编程:验证客户端合法性
  • 原文地址:https://www.cnblogs.com/ligen/p/4296667.html
Copyright © 2011-2022 走看看