java - 如何修改此代码上的指数和模数功能才能正常工作?
问题描述
这里的第一个问题,所以我不完全确定正确的方法,但这里是:
所以,我一直在寻找一些简单的代码来评估动态字符串表达式,并在 Study.com 上找到了这个很棒的代码,但由于某种原因,即使所有其他 4 个二进制运算符都是,指数 ^ 和模数 % 运算符也没有注册或工作工作得很好。
因此,如果你们能看一下并就我如何修改它以使其工作提供一些帮助,我将不胜感激。提前致谢!
一个简单的例子是:如果你要在代码的开头输入一个操作,例如 String[] go = {"1^2"}或String[] go = {"1%2"}并编译,它将分别自动推迟到错误的表达式:1^2或错误的表达式:1%2。
这是Study.com上的代码:
public static void main(String[] args) {
String[] go = {"1+2"};
if (go.length != 1) {
System.out.println("Usage: java EvaluateExpression \"expression\"");
System.exit(1);
}
try {
System.out.println(evaluateExpression(go[0]));
}
catch (Exception ex) {
System.out.println("Wrong expression: " + go[0]);
}
}
public static int evaluateExpression(String expression) {
Stack<Integer> operandStack = new Stack<>();
Stack<Character> operatorStack = new Stack<>();
expression = insertBlanks(expression);
String[] tokens = expression.split(" ");
for (String token: tokens) {
if (token.length() == 0)
continue;
else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '+' ||
operatorStack.peek() == '-' ||
operatorStack.peek() == '*' ||
operatorStack.peek() == '^' ||
operatorStack.peek() == '%' ||
operatorStack.peek() == '/')) {
processAnOperator(operandStack, operatorStack);
}
operatorStack.push(token.charAt(0));
}
else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '*' ||
operatorStack.peek() == '/')) {
processAnOperator(operandStack, operatorStack);
}
operatorStack.push(token.charAt(0));
}
else if (token.trim().charAt(0) == '(') {
operatorStack.push('(');
}
else if (token.trim().charAt(0) == ')') {
while (operatorStack.peek() != '(') {
processAnOperator(operandStack, operatorStack);
}
operatorStack.pop();
}
else {
operandStack.push(new Integer(token));
}
}
while (!operatorStack.isEmpty()) {
processAnOperator(operandStack, operatorStack);
}
return operandStack.pop();
}
public static void processAnOperator(Stack<Integer> operandStack, Stack<Character> operatorStack) {
char op = operatorStack.pop();
int op1 = operandStack.pop();
int op2 = operandStack.pop();
if (op == '+') operandStack.push(op2 + op1);
else if (op == '-') operandStack.push(op2 - op1);
else if (op == '*') operandStack.push(op2 * op1);
else if (op == '/') operandStack.push(op2 / op1);
else if (op == '^') operandStack.push(op2 ^ op1);
else if (op == '%') operandStack.push(op2 % op1);
}
public static String insertBlanks(String s) {
String result = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == ')' ||
s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '^' || s.charAt(i) == '%' ||
s.charAt(i) == '*' || s.charAt(i) == '/')
result += " " + s.charAt(i) + " ";
else
result += s.charAt(i);
}
return result;
}
解决方案
推荐阅读
- spring - 无法激活junit测试的事务
- xslt - XSLT 转换将命名空间从子级添加到父级
- python - Python视频游戏控制器脚本
- spring-boot - 如何在本机反应中从 Spring Boot 中获取数据?
- firebase - Firebase Cloud Firestore“请求/响应”文档
- java - 仅修改可搜索 Pdf 下的文本图层
- java - 在 grpc Spring boot 中关闭自定义线程池执行器
- visual-studio-2019 - Visual Studio 19 在 AWS 实例上运行时崩溃
- regex - 如何使用 xpath 选择特定的子节点?
- javascript - Recharts:如何使用 ComposedChart 从头到尾绘制阶梯线