java計算數學表達式

g2b4 9年前發布 | 2K 次閱讀 Java

 
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 = "));
}

}

</pre>

 本文由用戶 g2b4 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!