我正在使用Stack类来计算涉及整数的简单算术表达式,例如1+2*3。您的程序将按照给定的顺序执行操作,而不考虑运算符的优先级。
因此,表达式1+2*3应该被计算为(1+2)*3=9,而不是1+(2*3)=7。
如果我的输入是1+2*3,我知道如何将字符串1、2、3转换为整数,但我不知道如何将+和*从字符串类型转换为运算符。
我的代码逻辑是:
例如:给定字符串2 + (3 * 5),所以首先会执行3 * 5,然后在3 * 5的结果上执行+2。
可能最好的方法是使用 equals
,但最好忽略空格:
我不太确定您如何拆分字符串,但例如,如果您有一个字符 op
和两个整数 a
和 b
:
String str = op.replace(" ", "");
if(str.equals("*")){
retVal = a*b;
} else if(str.equals("+")){
retVal = a+b;
}//etc
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();
}
按照Ogen的建议,手动检查运算符。快速完成if、else if....结构的方法是使用switch语句,即
switch(operand) {
case "*":
break;
case "+":
break;
.....
default:
}
if (s.equals("+")) {
// addition
}
好的,假设您的任务需要使用Stack类,并且您已经有了选择数字(包括负数--在除一个情况外的所有情况下都是一个运算符后跟另一个运算符)和运算符以及括号的逻辑,您可以按照以下步骤进行。
如果遇到数字,请从堆栈中弹出最后两个元素。您弹出的第一个项目将是运算符,下一个将是数字。计算表达式并将其推入Stack中并继续。
您可以忽略括号。您还必须处理第一次读取数字或括号的情况。
class Operateur{
private static final DecimalFormat decfor = new DecimalFormat("0.00");
public static String Op(String s){
String[] equation = cutString(s);
double sol = 0;
switch (equation[1]){
case "/":
sol = Double.parseDouble(equation[0])/Double.parseDouble(equation[2]);
break;
case "*":
sol = Double.parseDouble(equation[0])*Double.parseDouble(equation[2]);
break;
case "+":
sol = Double.parseDouble(equation[0])+Double.parseDouble(equation[2]);
break;
case "-":
sol = Double.parseDouble(equation[0])-Double.parseDouble(equation[2]);
break;
}
return sol+"";
}
public static String[] cutString(String s){
String[] arr = new String[0];
if(s.contains("+")){
arr = s.split("((?=[//+])|(?<=[//+]))");
}
if(s.contains("-")){
arr = s.split("((?=-)|(?<=-))");
}
if(s.contains("*")){
arr = s.split("((?=[//*])|(?<=[//*]))");
}
if(s.contains("/")){
arr = s.split("((?=[///])|(?<=[///]))");
}
return arr;
}
public static void orderOp(String equation){
while(equation.contains("/")){
equation = equation.replace(getOp(equation),(decfor.format(Double.parseDouble(Op(getOp(equation))))).replace(',', '.'));
}
System.out.println("Division :" +equation);
while(equation.contains("*")){
equation = equation.replace(getOp(equation),decfor.format(Double.parseDouble(Op(getOp(equation)))).replace(',', '.'));
}
System.out.println("Multiplication:" +equation);
while(equation.contains("+")){
equation = equation.replace(getOp(equation),Op(getOp(equation)));
}
System.out.println("addition:" +equation);
while(equation.contains("-")&& (equation.replaceAll("[^.]", "").length()>1)){
equation = equation.replace(getOp(equation),Op(getOp(equation)).replace(',', '.'));
equation = RemoveNegative(equation);
System.out.println(equation);
}
System.out.println("soustraction:" +equation);
}
public static String getOp(String s){
String r ="";
if(s.contains("/")){
int slash = s.indexOf("/");
int first = slash;
int last = slash;
first -= 1;
while((first >= 0)&&(s.charAt(first) != '+')&(s.charAt(first) != '-')&(s.charAt(first) != '*')&(s.charAt(first) != '/')){
first -= 1;
}
first += 1;
last += 1;
while((last < s.length())&&(s.charAt(last) != '+')&(s.charAt(last) != '-')&(s.charAt(last) != '*')&(s.charAt(last) != '/')&&(s.charAt(last) != '/')){//&(last >= s.length())
if(last < s.length()) {
last += 1;
}
}
r = s.substring(first,last);
}
else if(s.contains("*")){
int slash = s.indexOf("*");
int first = slash;
int last = slash;
first -= 1;
while((first >= 0)&&(s.charAt(first) != '+')&(s.charAt(first) != '-')&(s.charAt(first) != '*')&(s.charAt(first) != '/')){
first -= 1;
}
first += 1;
last += 1;
while((last < s.length())&&(s.charAt(last) != '+')&&(s.charAt(last) != '-')&&(s.charAt(last) != '*')&&(s.charAt(last) != '/')){
if(last < s.length()) {
last += 1;
}
}
r = s.substring(first,last);
}
else if(s.contains("+")){
int slash = s.indexOf("+");
int first = slash;
int last = slash;
first -= 1;
while((first >= 0)&&(s.charAt(first) != '+')&(s.charAt(first) != '-')&(s.charAt(first) != '*')&(s.charAt(first) != '/')){
first -= 1;
}
first += 1;
last += 1;
while((last < s.length())&&(s.charAt(last) != '+')&&(s.charAt(last) != '-')&&(s.charAt(last) != '*')&&(s.charAt(last) != '/')){
if(last < s.length()) {
last += 1;
}
}
r = s.substring(first,last);
}
else if(s.contains("-")){
int slash = s.indexOf("-");
int first = slash;
int last = slash;
first -= 1;
while((first >= 0)&&(s.charAt(first) != '+')&(s.charAt(first) != '-')&(s.charAt(first) != '*')&(s.charAt(first) != '/')){
first -= 1;
}
first += 1;
last += 1;
while((last < s.length())&&(s.charAt(last) != '+')&&(s.charAt(last) != '-')&&(s.charAt(last) != '*')&&(s.charAt(last) != '/')){
if(last < s.length()) {
last += 1;
}
}
r = s.substring(first,last);
}
return r;
}
public static String RemoveNegative(String s){
s = s.replace("-+","-");
s = s.replace("+-","-");
if(s.charAt(0) == '-'){
s = s.replaceFirst("-","");
s = s.replace("-","+");
while(s.contains("+")){
s = s.replace(getOp(s),Op(getOp(s)));
}
s = "-"+s;
}
return s;
}
}
if(currChar.equals("*"))
? - No Idea For Name