一个计算复数四则运算的小程序

该博客展示了一个用Java编写的程序,用于处理复数的四则运算。程序通过命令行接收输入,格式为运算符加两个复数,可提取复数的实部和虚部,实现加、减、乘、除运算,并对输入异常进行处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//使用程序的输入格式为:
//*   3+4i    5+8i
//运算符+n个空格+第一个复数+n个空格+第二个复数
/*
*auther starshus
*
*Date 04/11/20
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
//5.7.10
public class Command
{
    private String commandLine;
    public Command(String line)//构造方法,得到输入
    {
        commandLine = line.trim();//.trim()用来去掉两边的空格
    }
    public String getCommand()//得到+、-、*、/中的一个运算符
    {
        String cmd = "";
        int index = commandLine.indexOf(' ');
        if(index >= 0)
            cmd = commandLine.substring(0,index);
        else cmd = commandLine;
        return cmd;//.toUpperCase();
    }
    //假设输入格式为  +  a+bi c+di 
    //首先提取数字a
    public String getA()
    {
        String a = "";
        int index1 = commandLine.indexOf(' ');
        if (index1 >= 0)
        {
            a=commandLine.substring(index1+1,commandLine.length()).trim();
            int index2 = a.indexOf(' ');
            if (index2 >=0)
                a=a.substring(0,index2);
            int index3 = a.indexOf('+');
            if (index3 >=0)
                a=a.substring(0,index3);
        }else a ="";
        return a;
    }
    //提取b
    public String getB()
    {
        String b ="";
        int index1 = commandLine.indexOf(' ');
        if (index1 >= 0)
        {
            b=commandLine.substring(index1+1,commandLine.length()).trim();
            int index2 = b.indexOf(' ');
            if (index2 >=0)
                b=b.substring(0,index2);
            int index3 = b.indexOf('+');
            int index4 = b.indexOf('i');
            if ((index3+1)==index4)
            {
            b="1";
            return b;
            }
        else if(index3>=0&&index4>=0)
            b=b.substring(index3+1,index4);
        else
            b="0";
        }else b="";
        return b;
    }
    //提取c
    public String getC()
    {
        String c ="";
        int index1 = commandLine.indexOf(' ');
        if (index1 >= 0)
        {
            c=commandLine.substring(index1+1,commandLine.length()).trim();
            int index2 = c.indexOf(' ');
            if (index2>=0)
                c=c.substring(index2+1,c.length()).trim();
            int index3= c.indexOf('+');
            if(index3>=0)
                c=c.substring(0,index3);
        }else c="";
        return c;
    }
    //提取d
    public String getD()
    {
        String d="";
        int index1 = commandLine.indexOf(' ');
        if (index1 >= 0)
        {
            d=commandLine.substring(index1+1,commandLine.length()).trim();
            int index2 = d.indexOf(' ');
            if (index2>=0)
                d=d.substring(index2+1,d.length()).trim();
            int index3= d.indexOf('+');
            int index4= d.indexOf('i');
            if ((index3+1)==index4)
            {
            d="1";
            return d;
            }
        else if(index3>=0&&index4>=0)
            d=d.substring(index3+1,index4);
        else
            d="0";
        }else d="";
        return d;
    }
    public static String add(double a,double b,double c,double d)//加法
    {
        double realPart,imaginaryPart;
        realPart = a+c;
        imaginaryPart = b+d;
        if(imaginaryPart>0)
        return(realPart + "+"+imaginaryPart+"i");
        else
            return(realPart+""+imaginaryPart+"i");
    }
    public static String sub(double a,double b,double c,double d)//减法
    {
        double realPart,imaginaryPart;
        realPart = a-c;
        imaginaryPart=b-d;
        if(imaginaryPart>0)
            return(realPart + "+"+imaginaryPart+"i");
            else
                return(realPart+""+imaginaryPart+"i");
    }
    public static String mult(double a,double b,double c,double d)//乘法
    {
        double realPart,imaginaryPart;
        realPart = a*c-b*d;
        imaginaryPart = a*d+b*c;
        if(imaginaryPart>0)
            return(realPart + "+"+imaginaryPart+"i");
            else
                return(realPart+""+imaginaryPart+"i");
    }
    public static String division(double a,double b,double c,double d)//除法
    {
        double realPart,imaginaryPart;
        realPart = (a*c+b*d)/(c*c+d*d);
        imaginaryPart = (b*c-a*d)/(c*c+d*d);
        if(imaginaryPart>0)
            return(realPart + "+"+imaginaryPart+"i");
            else
                return(realPart+""+imaginaryPart+"i");
    }
    public static void main(String[] args)//主方法
    {
        System.out.println("Welcome!Press 'q' or 'Q' to quit.");
        System.out.println("You can use this program like the follow example:");
        System.out.println("*   20+30i  15+35i");
        System.out.println("+   20+-30i  15+-35i");
        System.out.println("the first character can be '*' or '/' or '+' or '-'.");
        for(;;)
        {
            System.out.print("$ ");
            String inputLine;
            try{
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                inputLine = in.readLine();
            }catch (Exception exc){
                System.out.println("Your input can't be used.");
                continue;
            }
            Command commander = new Command(inputLine);//用Command类生成对象
            String cmd = commander.getCommand();
            if (cmd.equals("q")||cmd.equals("Q"))
                break;
            else if(cmd.equals("+")||cmd.equals("-")||cmd.equals("*")||cmd.equals("/"))
            {
                double a1,b1,c1,d1;
                try{           
                    String a=commander.getA();
                    a1=Double.valueOf(a).doubleValue();
                    String b=commander.getB();
                    b1=Double.valueOf(b).doubleValue();
                    String c=commander.getC();
                    c1=Double.valueOf(c).doubleValue();
                    String d=commander.getD();
                    d1=Double.valueOf(d).doubleValue();
                   }catch(Exception exc){
                      System.out.println("Sorry,your input can't be used.");
                      System.out.println("It should be looks like : * 1+2i 3+4i");
                      continue;
                   }
                 if(cmd.equals("+"))
                 {
                     System.out.println("The result is: "+add(a1,b1,c1,d1));
                 }
                 else if(cmd.equals("-"))
                 {
                     System.out.println("The result is: "+sub(a1,b1,c1,d1));
                 }
                 else if(cmd.equals("*"))
                 {
                     System.out.println("The result is: "+mult(a1,b1,c1,d1));
                 }
                 else if(cmd.equals("/"))
                 {
                     System.out.println("The result is: "+division(a1,b1,c1,d1));
                 }
            }else
                System.out.println("Sorry,the first character can be '*' or '/' or '+' or '-'.");
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值