【java】多项式计算(中缀转后缀)

来源:互联网 发布:飞鱼网络电视版本介绍 编辑:程序博客网 时间:2024/05/16 17:24

Calculator.java:

package calculator;import java.util.Stack;public class Calculator {    /**     * 转为后缀     * @param exper 中缀表达式     * @return     * @throws Exception     */    public String postfixExpression(String exper) throws Exception {        String exper_h = "";        Stack<Character> symbol = new Stack<>();        for (int i = 0; i < exper.length(); i++) {            if (exper.charAt(i) >= '0' && exper.charAt(i) <= '9' || exper.charAt(i) == '.') {// 数字                exper_h += exper.charAt(i);            } else {// 符号                exper_h += " ";                if (exper.charAt(i) == '(') {                    symbol.push(exper.charAt(i));                    continue;                }                if (exper.charAt(i) == ')') {                    while (!symbol.empty() && symbol.peek() != '(')                        exper_h += symbol.pop();                } else                    while (!symbol.empty() && priority(symbol.peek()) >= priority(exper.charAt(i))) {                        exper_h += symbol.pop();                    }                symbol.push(exper.charAt(i));            }        }        while (!symbol.empty()) {            exper_h += symbol.pop();        }        exper_h = exper_h.replace("(", "").replace(")", "").replaceAll(" +", " ");        return exper_h;    }    /**     * 计算结果     * @param exper_h 后缀表达式     * @return     * @throws Exception     */    public Float calculation(String exper_h) throws Exception {        Stack<String> number = new Stack<>();        String temp = "";        for (int i = 0; i < exper_h.length(); i++) {            if (exper_h.charAt(i) >= '0' && exper_h.charAt(i) <= '9' || exper_h.charAt(i) == '.'                    || exper_h.charAt(i) == ' ') {                temp += exper_h.charAt(i);                if (exper_h.charAt(i + 1) == ' ') {                    number.push(temp);                    temp = "";                    i++;                } else if (isSymbol(exper_h.charAt(i + 1))) {                    number.push(temp);                    temp = "";                }            } else {                float val1 = Float.valueOf(number.pop());                float val2 = Float.valueOf(number.pop());                number.push(col(val2, val1, exper_h.charAt(i)).toString());            }        }        return Float.valueOf(number.pop());    }    /**     * 优先级     *      * @param c     * @return     * @throws Exception     */    public int priority(char c) throws Exception {        if (c == '(' || c == ')')            return -1;        if (c == '*' || c == '/')            return 2;        else if (c == '+' || c == '-')            return 1;        else if (c == ' ') {            return 0;        } else            throw new Exception();    }    /**     * 计算     * @param val1     * @param val2     * @param c     * @return     * @throws Exception     */    public Float col(Float val1, Float val2, char c) throws Exception {        if (c == '*') {            return val1 * val2;        } else if (c == '/') {            return val1 / val2;        } else if (c == '+') {            return val1 + val2;        } else if (c == '-') {            return val1 - val2;        } else            throw new Exception();    }    /**     * 符号检查     * @param c     * @return     */    public boolean isSymbol(char c) {        return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')';    }}

Main:

package calculator;import java.util.Stack;public class Main {    public static void main(String[] args) throws Exception {        String exper = "1+1-(1*2+3-4/2)-1";        Calculator ca =new Calculator();        String exper_h = ca.postfixExpression(exper);        System.out.println(ca.calculation(exper_h))```//输出:0.0    }}
阅读全文
0 0
原创粉丝点击