我试图让Exercise20_23.java工作,但它不起作用。
我试过这个;
import java.util.*;
public class Exercise20_23 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an expression: ");
String expression = scanner.nextLine();
try {
System.out.println(expression + " = " + evaluateExpression(expression));
}
catch (Exception ex) {
System.out.println("Wrong expression: " + expression);
}
}
/** Evaluate an expression */
public static int evaluateExpression(String expression) {
// Create operandStack to store operands
Stack<Integer> operandStack = new Stack<>();
// Create operatorStack to store operators
Stack<Character> operatorStack = new Stack<>();
// Insert blanks around (, ), +, -, /, *, ^, and %
expression = insertBlanks(expression);
// Extract operands and operators
String[] tokens = expression.split(" ");
// Phase 1: Scan tokens
for (String token: tokens) {
if (token.length() == 0) // Blank space
continue; // Back to the for loop to extract the next token
else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
// Process all +, -, *, /, ^. % in the top of the operator stack
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '+' ||
operatorStack.peek() == '-' ||
operatorStack.peek() == '*' ||
operatorStack.peek() == '/' ||
operatorStack.peek() == '^' ||
operatorStack.peek() == '%')) {
processAnOperator(operandStack, operatorStack);
}
// Push the + or - operator into the operator stack
operatorStack.push(token.charAt(0));
}
else if (token.charAt(0) == '*' || token.charAt(0) == '/' || token.charAt(0) == '%') {
// Process all *, /, % in the top of the operator stack
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '*' ||
operatorStack.peek() == '/' ||
operatorStack.peek() == '%' )) {
processAnOperator(operandStack, operatorStack);
}
// Push the *, /, or % operator into the operator stack
operatorStack.push(token.charAt(0));
}
else if (token.charAt(0) == '^') {
// Process all ^ in the top of the operator stack
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '^')) {
processAnOperator(operandStack, operatorStack);
}
// Push the ^ operator into the operator stack
operatorStack.push(token.charAt(0));
}
else if (token.trim().charAt(0) == '(') {
operatorStack.push('('); // Push '(' to stack
}
else if (token.trim().charAt(0) == ')') {
// Process all the operators in the stack until seeing '('
while (operatorStack.peek() != '(') {
processAnOperator(operandStack, operatorStack);
}
operatorStack.pop(); // Pop the '(' symbol from the stack
}
else { // An operand scanned
// Push an operand to the stack
operandStack.push(Integer.valueOf(token));
}
}
// Phase 2: Process all the remaining operators in the stack
while (!operatorStack.isEmpty()) {
processAnOperator(operandStack, operatorStack);
}
// Return the result
return operandStack.pop();
}
/** Process one operator: Take an operator from operatorStack and
* apply it on the operands in the operandStack */
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((int)Math.pow(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;
}
/*
OUTPUT:
----jGRASP exec: java --module-path C:\Users\rmeri\Downloads\openjfx-19_windows-x64_bin-sdk\javafx-sdk-19\lib --add-modules=javafx.controls Exercise20_23
Enter an expression: (5 * 2 ^ 3 + 2 * 3 % 2) * 4
(5 * 2 ^ 3 + 2 * 3 % 2) * 4 = 160
----jGRASP: operation complete.
----jGRASP exec: java --module-path C:\Users\rmeri\Downloads\openjfx-19_windows-x64_bin-sdk\javafx-sdk-19\lib --add-modules=javafx.controls Exercise20_23
Enter an expression: 349 % 23 ^ 2 + 4 ^ 4 % 5
349 % 23 ^ 2 + 4 ^ 4 % 5 = 605
----jGRASP: operation complete.
*/
}
我尝试过这个;
import java.util.*;
public class Exercise20_23 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an expression: ");
String expression = scanner.nextLine();
String temp = expression;
try {
System.out.println(expression + " = " + evaluateExpression(expression));
}
catch (Exception ex) {
System.out.println("Wrong expression: " + temp);
}
}
/** Evaluate an expression */
public static int evaluateExpression(String expression) {
// Create operandStack to store operands
Stack<Integer> operandStack = new Stack<>();
// Create operatorStack to store operators
Stack<Character> operatorStack = new Stack<>();
// Insert blanks around (, ), +, -, /, *, ^, and %
expression = insertBlanks(expression);
// Extract operands and operators
String[] tokens = expression.split(" ");
// Phase 1: Scan tokens
for (String token: tokens) {
if (token.length() == 0) // Blank space
continue; // Back to the for loop to extract the next token
else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
// Process all +, -, *, /, ^. % in the top of the operator stack
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '+' ||
operatorStack.peek() == '-' ||
operatorStack.peek() == '*' ||
operatorStack.peek() == '/' ||
operatorStack.peek() == '^' ||
operatorStack.peek() == '%')) {
processAnOperator(operandStack, operatorStack);
}
// Push the + or - operator into the operator stack
operatorStack.push(token.charAt(0));
}
else if (token.charAt(0) == '*' || token.charAt(0) == '/' || token.charAt(0) == '%') {
// Process all *, /, % in the top of the operator stack
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '*' ||
operatorStack.peek() == '/' ||
operatorStack.peek() == '%' )) {
processAnOperator(operandStack, operatorStack);
}
// Push the *, /, or % operator into the operator stack
operatorStack.push(token.charAt(0));
}
else if (token.charAt(0) == '^') {
// Process all ^ in the top of the operator stack
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '^')) {
processAnOperator(operandStack, operatorStack);
}
// Push the ^ operator into the operator stack
operatorStack.push(token.charAt(0));
}
else if (token.trim().charAt(0) == '(') {
operatorStack.push('('); // Push '(' to stack
}
else if (token.trim().charAt(0) == ')') {
// Process all the operators in the stack until seeing '('
while (operatorStack.peek() != '(') {
processAnOperator(operandStack, operatorStack);
}
operatorStack.pop(); // Pop the '(' symbol from the stack
}
else { // An operand scanned
// Push an operand to the stack
operandStack.push(Integer.valueOf(token));
}
}
// Phase 2: Process all the remaining operators in the stack
while (!operatorStack.isEmpty()) {
processAnOperator(operandStack, operatorStack);
}
// Return the result
return operandStack.pop();
}
/** Process one operator: Take an operator from operatorStack and
* apply it on the operands in the operandStack */
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((int)Math.pow(op2, op1));
else if (op == '%')
operandStack.push(op2 % op1);
else if (op != '(' || op != ')') {
if (op == '%')
operandStack.push(op2 % op1);
else if (op == '^')
operandStack.push((int)Math.pow(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;
}
/*
OUTPUT:
----jGRASP exec: java --module-path C:\Users\rmeri\Downloads\openjfx-19_windows-x64_bin-sdk\javafx-sdk-19\lib --add-modules=javafx.controls Exercise20_23
Enter an expression: (5 * 2 ^ 3 + 2 * 3 % 2) * 4
(5 * 2 ^ 3 + 2 * 3 % 2) * 4 = 160
----jGRASP: operation complete.
----jGRASP exec: java --module-path C:\Users\rmeri\Downloads\openjfx-19_windows-x64_bin-sdk\javafx-sdk-19\lib --add-modules=javafx.controls Exercise20_23
Enter an expression: 349 % 23 ^ 2 + 4 ^ 4 % 5
349 % 23 ^ 2 + 4 ^ 4 % 5 = 605
----jGRASP: operation complete.
*/
}
我正在尝试修改评估表达式,为运算符添加“^”、“%”字符,以便我可以完成狂欢项目,但我不知道我还需要做什么,所以如果有人可以告诉我什么上面的两段代码都是错误的,我将非常感激。
你已经很接近了。
我看到你正在使用jGRASP,调试这类问题就是它的用途。在主循环内的第一个语句(“if”语句)处设置断点并进行调试。从“变量”选项卡中,拖出运算符和操作数堆栈并将它们放在任意位置。这将弹出“查看器”,以便您可以观看堆栈的填满和清空。为了方便起见,您还可以打开画布并将两个变量拖到那里,如下图所示(您可以保存画布以供以后重用,这与个人查看器不同)。反复点击调试器上的“恢复”,并观察堆栈以及“令牌”变量(为了方便起见,您也可以将其拖到画布上),此时代码中的“令牌”将是要处理的“下一个”令牌。将堆栈上发生的情况与根据优先级应该发生的情况进行比较,例如 (349 % (23 ^ 2)) + ((4 ^ 4) % 5)。当您发现导致问题的操作符时,退出并重新启动调试器,恢复直到问题点,然后使用调试器“单步”和/或“单步执行”按钮逐行运行,直到看到原因。