zoukankan      html  css  js  c++  java
  • 【java】多项式计算(中缀转后缀)

    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
        }
    }
    
  • 相关阅读:
    找不到 .NETFramework,Version=v5.0 的引用程序集。要解决此问题,请为此框架版本安装开发人员工具包(SDK/目标包)或者重新定向应用程序。
    从pfx私钥证书中提取私钥
    更改域密码
    realtek高清晰音频管理器 WIN10
    LINQ to Entities does not recognize the method 'System.String ToString()' method
    C# ML.NET 使用GPU遇到 Failed to get convolution algorithm.This is probably because cuDNN failed to initialize
    8款开源自动化测试框架
    质量管理大师:戴明、克劳士比、朱兰、菲根堡姆
    接口测试数据建模
    稻盛和夫:一场高效的会议,一定是让员工多说
  • 原文地址:https://www.cnblogs.com/cnsec/p/13286784.html
Copyright © 2011-2022 走看看