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
    
  • 相关阅读:
    python按行读取并替换
    python 爬取网页内容
    file.write(str),file.writelines(sequence)
    04Spring_bean 后处理器(后处理Bean),BeanPostProcessor ,bean创建时序,动态代理
    03Spring_bean的创建和作用域以及生命周期
    02Spring_Ioc和DI介绍
    01Spring_基本jia包的导入andSpring的整体架构and怎么加入日志功能
    错题724-java
    05传智_jbpm与OA项目_部门模块中增加部门的jsp页面增加一个在线编辑器功能
    04传智_jbpm与OA项目_部门模块改进_直接在BaseAction中实现ModelDriven<T>
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078407.html
Copyright © 2011-2022 走看看