在 Java 中替换嵌套 While 循环的替代方法
Alternative way to replace Nested While Loop in Java
我尝试使用以下代码计算 Java 中的数学表达式:
public double applyOp(char op,double b,double a)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return 0;
}
public boolean hasPrecedence(char op1,char op2)
{
return (op1 != '*' && op1 != '/') || (op2 != '+' && op2 != '-');
}
public double evaluate(String input) {
Stack<Double> values = new Stack<>();
Stack<Character> ops = new Stack<>();
int stringIndex = 0;
while (stringIndex < input.length())
{
StringBuilder multiDigitsNumber = new StringBuilder();
// If the input is number put to stack values
if (input.charAt(stringIndex) >= '0' && input.charAt(stringIndex) <= '9')
{
while (stringIndex < input.length() && input.charAt(stringIndex) >= '0' && input.charAt(stringIndex) <= '9')
{
multiDigitsNumber.append(input.charAt(stringIndex++));
}
values.push(Double.parseDouble(multiDigitsNumber.toString()));
}
// If the input is operator put to stack ops
else
{
while (!ops.empty() && hasPrecedence(input.charAt(stringIndex),ops.peek()))
{
values.push(applyOp(ops.pop(),values.pop(),values.pop()));
}
ops.push(input.charAt(stringIndex++));
}
}
// Execute remain operator in stack values
while (!ops.empty()) {
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
}
// The final number in stack value is result
return values.pop();
}
输入示例:
12+24*2-30/5.....
上面的代码工作正常,但我想知道有什么方法可以替换
while (stringIndex < input.length() && input.charAt(stringIndex) >= '0' && input.charAt(stringIndex) <= '9')
{
multiDigitsNumber.append(input.charAt(stringIndex++));
}
用别的东西,所以我不必在这种情况下使用嵌套的 while 循环。目标是捕获字符串中的数字,直到它到达运算符
提前致谢
你可以像这样使用正则表达式。
if (input.charAt(stringIndex) >= '0' && input.charAt(stringIndex) <= '9')
{
String number = input.substring(stringIndex).replaceAll("^(\d+).*", "");
values.push(Double.parseDouble(number));
stringIndex += number.length();
}
我尝试使用以下代码计算 Java 中的数学表达式:
public double applyOp(char op,double b,double a)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return 0;
}
public boolean hasPrecedence(char op1,char op2)
{
return (op1 != '*' && op1 != '/') || (op2 != '+' && op2 != '-');
}
public double evaluate(String input) {
Stack<Double> values = new Stack<>();
Stack<Character> ops = new Stack<>();
int stringIndex = 0;
while (stringIndex < input.length())
{
StringBuilder multiDigitsNumber = new StringBuilder();
// If the input is number put to stack values
if (input.charAt(stringIndex) >= '0' && input.charAt(stringIndex) <= '9')
{
while (stringIndex < input.length() && input.charAt(stringIndex) >= '0' && input.charAt(stringIndex) <= '9')
{
multiDigitsNumber.append(input.charAt(stringIndex++));
}
values.push(Double.parseDouble(multiDigitsNumber.toString()));
}
// If the input is operator put to stack ops
else
{
while (!ops.empty() && hasPrecedence(input.charAt(stringIndex),ops.peek()))
{
values.push(applyOp(ops.pop(),values.pop(),values.pop()));
}
ops.push(input.charAt(stringIndex++));
}
}
// Execute remain operator in stack values
while (!ops.empty()) {
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
}
// The final number in stack value is result
return values.pop();
}
输入示例:
12+24*2-30/5.....
上面的代码工作正常,但我想知道有什么方法可以替换
while (stringIndex < input.length() && input.charAt(stringIndex) >= '0' && input.charAt(stringIndex) <= '9')
{
multiDigitsNumber.append(input.charAt(stringIndex++));
}
用别的东西,所以我不必在这种情况下使用嵌套的 while 循环。目标是捕获字符串中的数字,直到它到达运算符
提前致谢
你可以像这样使用正则表达式。
if (input.charAt(stringIndex) >= '0' && input.charAt(stringIndex) <= '9')
{
String number = input.substring(stringIndex).replaceAll("^(\d+).*", "");
values.push(Double.parseDouble(number));
stringIndex += number.length();
}