java的算术表达式程序,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 s = new Stack();// 放操作符
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 s = new Stack();
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的算术表达式程序,java计算数学表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LNMP 一键安装脚本阅读
- 下一篇: [导入]C++程序随笔