加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java计算数学表达式

发布时间:2020-12-14 23:31:10 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.util.EmptyStackException;import java.util.Stack; public class CaculateFunction { private static String[] TrnsInToSufix(String IF

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

 
import java.util.EmptyStackException;
import java.util.Stack;
  
public class CaculateFunction {
    private static String[] TrnsInToSufix(String IFX)// PFX放后缀表达式,IFX为中缀表达式
    {
        String PFX[] = new String[IFX.length()];
        StringBuffer numBuffer = new StringBuffer();// 用来保存一个数的
        Stack<String> s = new Stack<String>();// 放操作符
        String a;
        s.push("=");// 第一个为等号
        int i = 0,j = 0;
        char ch;
        for (i = 0; i < IFX.length();) {
            ch = IFX.charAt(i);
            switch (ch) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                while (Character.isDigit(ch) || ch == '.')// 拼数
                {
                    numBuffer.append(ch); // 追加字符
                    ch = IFX.charAt(++i);
                }
                PFX[j++] = numBuffer.toString();// break;
                numBuffer = new StringBuffer(); // 清空已获取的运算数字
                continue; // 这里要重新循环,因为i已经增加过了
            case '(':
                s.push("(");
                break;
            case ')':
                while (s.peek() != "(")
                    PFX[j++] = s.pop();
                break;
            case '+':
            case '-':
                while (s.size() > 1 && s.peek() != "(")
                    PFX[j++] = s.pop();
                a = String.valueOf(ch);
                s.push(a);
                break;
            case '*':
            case '/':
                while (s.size() > 1 && (s.peek() == "*") || s.peek() == "/"
                        || s.peek() == "s" || s.peek() == "c"
                        || s.peek() == "t" || s.peek() == "^"
                        || s.peek() == "√")
                    // 优先级比较,与栈顶比较,
                    PFX[j++] = s.pop();// 当前操作符优先级大于等于栈顶的弹出栈顶
                a = String.valueOf(ch);
                s.push(a);
                break;
            case 's':
            case 'c':
            case 't':// 三角函数
                while (s.size() > 1
                        && (s.peek() == "s" || s.peek() == "c"
                                || s.peek() == "t" || s.peek() == "^" || s
                                .peek() == "√"))
                    // 优先级比较,与栈顶,大于等于的弹出
                    PFX[j++] = s.pop();
                a = String.valueOf(ch);
                s.push(a);
                break;
            case '^':// 幂
            case '√':// 开方
                while (s.size() > 1 && (s.peek() == "^" || s.peek() == "√"))
                    PFX[j++] = s.pop();
                a = String.valueOf(ch);
                s.push(a);
                break;
            }
            i++;
        }
        while (s.size() > 1)
            PFX[j++] = s.pop();
        PFX[j] = "=";
  
        return PFX;
    }
  
    public static String Evaluate(String IFX)// 后缀表达式求值
    {
        String PFX[] = null;
        try {
            PFX = TrnsInToSufix(IFX);
        } catch (EmptyStackException e) {
            return "syntax error";
        }
        int i = 0;
        double x1,x2,n;
        String str;
        Stack<String> s = new Stack<String>();
        while (PFX[i] != "=") {
            str = PFX[i];
            switch (str.charAt(0)) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                s.push(str);
                break;
            case '+':
                x1 = Double.parseDouble(s.pop());
                x2 = Double.parseDouble(s.pop());
                n = x1 + x2;
                s.push(String.valueOf(n));
                break;
            case '-':
                x1 = Double.parseDouble(s.pop());
                x2 = Double.parseDouble(s.pop());
                n = x2 - x1;
                s.push(String.valueOf(n));
                break;
            case '*':
                x1 = Double.parseDouble(s.pop());
                x2 = Double.parseDouble(s.pop());
                n = x1 * x2;
                s.push(String.valueOf(n));
                break;
            case '/':
                x1 = Double.parseDouble(s.pop());
                x2 = Double.parseDouble(s.pop());
                n = x2 / x1;
                s.push(String.valueOf(n));
                break;
            case 's':
                x1 = Double.parseDouble(s.pop());
                n = Math.sin(x1 * Math.PI / 180);
                s.push(String.valueOf(n));
                break;
            case 'c':
                x1 = Double.parseDouble(s.pop());
                n = Math.cos(x1 * Math.PI / 180);
                s.push(String.valueOf(n));
                break;
            case 't':
                x1 = Double.parseDouble(s.pop());
                n = Math.tan(x1 * Math.PI / 180);
                s.push(String.valueOf(n));
                break;
            case '√':
                x1 = Double.parseDouble(s.pop());
                n = Math.sqrt(x1);
                s.push(String.valueOf(n));
                break;// 开方
            case '^':
                x1 = Double.parseDouble(s.pop());
                x2 = Double.parseDouble(s.pop());
                n = Math.pow(x2,x1);
                s.push(String.valueOf(n));
                break;
            }
            i++;
        }
        String result = s.pop();
        return result;
    }
  
    public static void main(String args[]) {
        System.out.println(Evaluate("(31 + 21) * 51 - (21 + 33) / 2 = "));
    }
}
 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读