简单计算器(考虑小括号和运算符号优先级)

本文介绍了一个Java程序,演示了如何使用栈来处理带有小括号和运算符的简单计算器,通过优先级解析实现复杂算术表达式的计算。

简单计算器(考虑小括号和运算符号优先级)


例如:

输入: 1+((2+3)*4)-5*1.5
输出: 1+((2+3)*4)-5*1.5=13.5

输入: (2+1)*1.5
输出: (2+1)*1.5=4.5
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        List <String> arrayList = getArrayList(s);
       // System.out.println(arrayList);
        List <String> list = work(arrayList);
        //System.out.println(list);
        System.out.println(s+"="+finash(list));

    }

    public static List <String> getArrayList(String s) {
        List <String> list = new ArrayList <String>();
        String s2 = "";
        for (int i = 0; i < s.length(); i++) {
            //如果是数
            if (!isOper(s.charAt(i))) {
                s2 += s.charAt(i);
                //如果是多位数
                if (i == s.length() - 1 || isOper(s.charAt(i+1))) {
                    list.add(s2);
                    s2 = "";
                }
            } else {
                list.add("" + s.charAt(i));
            }
        }
        return list;
    }

    public static List <String> work(List <String> list) {
        Stack <String> s1 = new Stack <>();
        List <String> s2 = new ArrayList <>();

        for (String item : list) {
            if (item.matches("^(\\-|\\+)?\\d+(\\.\\d+)?$")) {
                s2.add(item);
            } else if (item.equals("(") || s1.size() == 0) {
                s1.push(item);
            } else if (item.equals(")")) {
                while (!s1.peek().equals("(")) {
                    s2.add(s1.pop());
                }
                s1.pop();//把”(“给清除
            } else {//item是加减乘除
                //如果item的优先级比较高
                if (op(item) > op(s1.peek()))
                    s1.push(item);
                    //如果item的优先级比较低
                else {
                    while (s1.size() != 0 && op(item) <= op(s1.peek())) {
                        s2.add(s1.pop());
                    }
                    s1.push(item);
                }
            }
        }
        while (s1.size() != 0) {
            s2.add(s1.pop());
        }
        return s2;
    }

    public static int op(String oper) {
        if (oper.equals("+") || oper.equals("-")) {
            return 1;
        } else if (oper.equals("*") || oper.equals("/")) {
            return 2;
        } else if (oper.equals("(")) {
            return 0;
        } else {
            throw new RuntimeException("ERROR!运算符号有误...");
        }
    }

    public static double finash(List <String> list) {
        Stack <Double> stringStack = new Stack <>();
        double num1 = 0;
        double num2 = 0;
        double result = 0;
        String op = "";
        for (String item : list) {
            if (item.matches("^(\\-|\\+)?\\d+(\\.\\d+)?$")) {
                stringStack.push(Double.parseDouble(item));
            } else {
                switch (item) {
                    case "+":
                        num1 = stringStack.pop();
                        num2 = stringStack.pop();
                        result = num2 + num1;
                        stringStack.push(result);
                        break;
                    case "-":
                        num1 = stringStack.pop();
                        num2 = stringStack.pop();
                        result = num2 - num1;
                        stringStack.push(result);
                        break;
                    case "*":
                        num1 = stringStack.pop();
                        num2 = stringStack.pop();
                        result = num2 * num1;
                        stringStack.push(result);
                        break;
                    case "/":
                        num1 = stringStack.pop();
                        num2 = stringStack.pop();
                        if (num1 == 0) {
                            throw new RuntimeException("ERROR: " + num2 + "/" + num1);
                        }
                        result = num2 / num1;
                        stringStack.push(result);
                        break;
                }
            }
        }
        return stringStack.pop();
    }

    public static Boolean isOper(char oper) {
        return oper == '+' || oper == '-' || oper == '*' || oper == '/' || oper == '(' ||oper == ')';
    }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值