我正在尝试用Java(GUI)编写科学计算器。如何为等号按钮编写命令以考虑字符串中*,/,+,-的优先级?
String[] st = s.split("(?<=[-+*/])|(?=[-+*/])");
char eql[] = s.toCharArray();
for (int i = 0; i <= st.length; i++) {
if ('*' == eql[i]) {
double a = eql[i + 1] * eql[i - 1];
jTextField1.setText(a+"");
}
答案 0 :(得分:0)
if (input is * || input is /)
//do multiplication and division left to right
else if (input is + || input is -)
//do addition and subtraction left to right
我还要补充一点,Shunting Yard Algorithm可能有用或有趣。希望这会有所帮助!