中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java計算數學表達式

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
 
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; // 這里要重新循環(huán),因為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() == "√")
                    // 優(yōu)先級比較,與棧頂比較,
                    PFX[j++] = s.pop();// 當前操作符優(yōu)先級大于等于棧頂的彈出棧頂
                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() == "√"))
                    // 優(yōu)先級比較,與棧頂,大于等于的彈出
                    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 = "));
    }
}
 

標簽:

版權申明:本站文章部分自網絡,如有侵權,請聯系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯系。

上一篇:Java下通過發(fā)送http post請求的代碼

下一篇:純HTML5 Canvas實現的餅圖