java计算器

package calculator;

import java.util.Stack;

/**
 * @author xiaobailing
 */
public class GetResult {
    public GetResult(){

    }
    static char zero = '0';
    static char nine = '9';
    static char leftBracket = '(';
    static char rightBracket = ')';
    static char point = '.';
    static char plus = '+';
    static char subtraction = '-';
    static char multiply = '*';
    static char divide = '/';

    static Stack<Double> num = new Stack<>();
    static Stack<Character> op = new Stack<>();
    public static String main(String args) {
        try {
            args = format(args);
            stackProcessing(args);
            while (op.size() > 0) {
                eval();
            }

            return String.valueOf(num.peek());
        }catch (Exception e){
            return "Error";
        }
        finally {
            num.clear();
            op.clear();
        }
    }
    public static void stackProcessing(String str){
        for(int i=0;i<str.length();i++){
            if((str.charAt(i)>=zero&&str.charAt(i)<=nine)) {
                i = getNum(str,i);
            }
            else if(str.charAt(i) == leftBracket){
                op.push(str.charAt(i));
            }
            else if(str.charAt(i) == rightBracket){
                while (op.size()>0&&op.peek()!=leftBracket){
                    eval();
                }
                op.pop();
            }
            else{
                while (op.size()>0&&getPriority(str.charAt(i))<=getPriority(op.peek())) {
                    eval();
                }
                    op.push(str.charAt(i));
            }
        }

    }
    public static int  getNum(String str,int i){
        int j = i;
        while(j<str.length()&&(str.charAt(j)>=zero&&str.charAt(j)<=nine||(str.charAt(j)==point))){
                    j++;
        }
        String s = str.substring(i,j);
        double a = Double.parseDouble(s);
        num.push(a);
        return j - 1;
    }
    public static void eval(){
        double a = num.peek();
        num.pop();
        double b = num.peek();
        num.pop();
        char c = op.peek();
        op.pop();
        double r = 0;
        if (c == plus) {
            r = b + a;
        }
        if (c == subtraction) {
            r = b - a;
        }
        if (c == multiply) {
            r = b * a;
        }
        if (c == divide) {
            r = b / a;
        }
        num.push(r);

    }
    public static int getPriority(char s){
        if(s == divide) {
            return 2;
        }
       else if(s == multiply) {
            return 2;
        }
       else if (s == plus) {
            return 1;
        }
       else if(s == subtraction) {
            return 1;
        }
       else if (s == leftBracket){
           return 0;
        } else {
            return 1;
        }
    }
    public static String format (String args){
        for(int i = 0;i < args.length();i++){
            if(args.charAt(i) == subtraction&&i==0){
                args = "0"+args;
            }
            if (args.charAt(i)=='-'&&(args.charAt(i+1)=='-')){
                String temp = args.substring(0,i);
                String temp2 = args.substring(i+2);
                args = temp + "+"+temp2;
            }
            if (args.charAt(i)=='-'&&(args.charAt(i-1)=='+'||args.charAt(i-1)=='-'||args.charAt(i-1)=='(')){
                    String temp = args.substring(0,i);
                    String temp2 = args.substring(i);
                    args = temp + "0"+temp2;
            }
            if(args.charAt(i)=='-'&&(args.charAt(i-1)=='*'||args.charAt(i-1)=='/')){
                String temp = args.substring(0,i);
                int j = i + 1;
                while ((j<args.length())&&(args.charAt(j)>=zero&&args.charAt(j)<=nine)||args.charAt(i)==point) {
                   j++;
                }
                String temp2 = args.substring(i,j);
                String temp3 = args.substring(j);
                args = temp+"(0"+temp2+")"+temp3;

            }
        }
        System.out.println(args);
        return args;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值