zoukankan      html  css  js  c++  java
  • Java实现复数运算

    1 问题描述
    编程实现两个复数的运算。设有两个复数 和 ,则他们的运算公式为:

    要求:(1)定义一个结构体类型来描述复数。
      (2)复数之间的加法、减法、乘法和除法分别用不用的函数来实现。
      (3)必须使用结构体指针的方法把函数的计算结果返回。
      说明:用户输入:运算符号(+,-,*,/) a b c d.
      输出:a+bi,输出时不管a,b是小于0或等于0都按该格式输出,输出时a,b都保留两位。

    输入:
      - 2.5 3.6 1.5 4.9
    输出:
      1.00±1.30i

    2 解决方案

    package com.liuzhen.systemExe;
    
    
    import java.io.IOException;
    import java.util.Scanner;
    
    
    public class Main{
        
        public void complexOperation(char operation,double a,double b,double c,double d){
            if(operation == '+'){
                double temp1 = a + c;
                double temp2 = b + d;
                System.out.printf("%.2f",temp1);
                System.out.print("+");
                System.out.printf("%.2f",temp2);
                System.out.print("i");
            }
            
            if(operation == '-'){
                double temp1 = a - c;
                double temp2 = b - d;
                System.out.printf("%.2f",temp1);
                System.out.print("+");
                System.out.printf("%.2f",temp2);
                System.out.print("i");
            }
            
            if(operation == '*'){
                double temp1 = a*c - b*d;
                double temp2 = a*d + b*c;
                System.out.printf("%.2f",temp1);
                System.out.print("+");
                System.out.printf("%.2f",temp2);
                System.out.print("i");
            }
            
            if(operation == '/'){
                double temp1 = (a*c + b*d)/(c*c + d*d);
                double temp2 = (b*c - a*d)/(c*c + d*d);
                System.out.printf("%.2f",temp1);
                System.out.print("+");
                System.out.printf("%.2f",temp2);
                System.out.print("i");
            }
        }
        
        
        public static void main(String[] args){
            Main test = new Main(); 
            Scanner in = new Scanner(System.in);
            //System.out.println("请输入一个运算符和四个数字:");
            //此处重点在于单个字符的输入问题
            char operation = 0;  
            try {  
                operation = (char)System.in.read();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            double[] temp = new double[4];
            for(int i = 0;i < 4;i++){
                temp[i] = in.nextDouble();
            }
            test.complexOperation(operation, temp[0], temp[1], temp[2], temp[3]);
        }
    }
    

    运行结果:

    请输入一个运算符和四个数字:
    + 1 2 3 4
    4.00+6.00i
    
    请输入一个运算符和四个数字:
    - 1 2 3 4
    -2.00+-2.00i
    
  • 相关阅读:
    BZOJ1001 狼抓兔子 终于过了!
    BZOJ 1901: Zju2112 Dynamic Rankings( 树状数组套主席树 )
    BZOJ 2302: [HAOI2011]Problem c( dp )
    BZOJ 1407: [Noi2002]Savage( 数论 )
    BZOJ 2661: [BeiJing wc2012]连连看( 费用流 )
    BZOJ 1021: [SHOI2008]Debt 循环的债务( dp )
    BZOJ 3170: [Tjoi 2013]松鼠聚会( sort )
    BZOJ 2301: [HAOI2011]Problem b( 数论 )
    BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
    BZOJ 3231: [Sdoi2008]递归数列( 矩阵快速幂 )
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078407.html
Copyright © 2011-2022 走看看