在java中将字符串转换为运算符(+*/-)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26969080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert String to operator(+*/-) in java
提问by
I am using Stack class to calculate simple arithmetic expressions involving integers, such as 1+2*3.your program would execute operations in the order given,without regarding to the precedence of operators. *Thus, the expression 1+2*3 should be calculated (1+2)*3=9,not 1+(2*3)=7.
我正在使用 Stack 类来计算涉及整数的简单算术表达式,例如 1+2*3。您的程序将按照给定的顺序执行操作,而不考虑运算符的优先级。*因此,表达式 1+2*3 应计算为 (1+2)*3=9,而不是 1+(2*3)=7。
If i get the input as 1+2*3,i know how to convert the string 1,2,3 to Integer.but i don't know how to covert +,* from string type to operator.
如果我得到 1+2*3 的输入,我知道如何将字符串 1,2,3 转换为 Integer.but 我不知道如何将 +,* 从字符串类型转换为运算符。
My code logic is: For eg: Given string 2 + (3 * 5), So 3 * 5 will be operated first then +2 will be performed in result of 3 * 5.
我的代码逻辑是:例如:给定字符串2 + (3 * 5),所以3 * 5会先操作,然后+2会在3 * 5的结果中执行。
采纳答案by No Idea For Name
probably the best way to do it will be equals
, but it's best to ignore whitespaces:
可能最好的方法是equals
,但最好忽略空格:
i'm not quite sure how you split your string, but for example, if you have a char op
and two integer a
and b
:
我不太确定您如何拆分字符串,但是例如,如果您有一个字符op
和两个整数,a
并且b
:
String str = op.replace(" ", "");
if(str.equals("*")){
retVal = a*b;
} else if(str.equals("+")){
retVal = a+b;
}//etc
回答by Ogen
You will have to manually check and assign the operator. E.g.
您必须手动检查和分配操作员。例如
if (s.equals("+")) {
// addition
}
回答by committedandroider
Do what Ogen suggested and manually check the operator. A quick shortcut to doing the if, else if .... structure is switch, that is
执行 Ogen 的建议并手动检查操作员。执行 if, else if .... 结构的快捷方式是 switch,即
switch(operand) {
case "*":
break;
case "+":
break;
.....
default:
}
回答by Chan
Ok, assuming your assignment needs you to use the Stack class and you already have the logic to pick numbers ( including the negative numbers -- that would an operator followed by another operator in all but one cases) and operators and parens, what you could do is as follows.
好的,假设你的赋值需要你使用 Stack 类,并且你已经有了选择数字的逻辑(包括负数——在除一种情况下,在所有情况下都会有一个运算符后跟另一个运算符)以及运算符和括号,你可以做什么做如下。
If you encounter a number, pop the last two elements from your stack. The first item you pop will be an operator and the next will be a number. Evaluate the expression and push it into Stack and continue.
如果遇到数字,则从堆栈中弹出最后两个元素。您弹出的第一个项目将是一个运算符,下一个将是一个数字。评估表达式并将其推入堆栈并继续。
You can ignore the parenthesis. You will also have to handle the case of reading a number or parenthesis the first time.
您可以忽略括号。您还必须处理第一次读取数字或括号的情况。
回答by Jigar Modi
Quick solution: Use below code for executing correct javascript Arithmetic expression in java.
快速解决方案:使用以下代码在 java 中执行正确的 javascript 算术表达式。
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName("JavaScript");
try {
Object result = se.eval(val);
System.out.println(result.toString());
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}