需要补全的代码如下
public int evaluate(String expr) {
int op1, op2, result = 0;
String token;
StringTokenizer tokenizer = new StringTokenizer(expr);
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
//如果是运算符,调用isOperator
if () {
//从栈中弹出操作数2
//从栈中弹出操作数1
//根据运算符和两个操作数调用evalSingleOp计算result;
//计算result入栈;
} else//如果是操作数
//操作数入栈;
}
return result;
}
思路
-
如果是运算符,调用
isOperator
这是isOperator
:
private boolean isOperator(String token) {
return (token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/"));
}
这个函数boolean
型,参数可以直接传字符串,所以在if()
中填入 isOperator(token)
即可。
-
从栈中弹出操作数2
弹栈使用stack.pop()
,所以填入op2 = stackpop()
。
-
从栈中弹出操作数1
方法同上。
-
根据运算符和两个操作数调用
evalSingleOp
计算result
首先看下evalSingleOp
以确定传入的参数
private int evalSingleOp(char operation, int op1, int op2)
那么,由上可以看出,需要传入char型运算符、int型操作数1、int型操作数2。operation
就是运算符,需要传token
,但是token
是字符串,所以使用string.charAt(0)
,把token
转换成token.charAt(0)
。至于op1、op2本身就是int型。
综上得出,需要填入
result=evalSingleOp(token.charAt(0), op1, op2);
-
计算
result
入栈
stack.push(result);
-
如果是操作数,操作数入栈
这里只能让int型入栈,但是输入的token
是字符串,所以必须要使用Integer.parseInt(string)
转化成int型。
故填入
stack.push(Integer.parseInt(token));
完整代码
-
[git链接](
需要补全的代码如下
public int evaluate(String expr) {
int op1, op2, result = 0;
String token;
StringTokenizer tokenizer = new StringTokenizer(expr);
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
//如果是运算符,调用isOperator
if () {
//从栈中弹出操作数2
//从栈中弹出操作数1
//根据运算符和两个操作数调用evalSingleOp计算result;
//计算result入栈;
} else//如果是操作数
//操作数入栈;
}
return result;
}
思路
-
如果是运算符,调用
isOperator
这是isOperator
:
private boolean isOperator(String token) {
return (token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/"));
}
这个函数boolean
型,参数可以直接传字符串,所以在if()
中填入 isOperator(token)
即可。
-
从栈中弹出操作数2
弹栈使用stack.pop()
,所以填入op2 = stackpop()
。
-
从栈中弹出操作数1
方法同上。
-
根据运算符和两个操作数调用
evalSingleOp
计算result
首先看下evalSingleOp
以确定传入的参数
private int evalSingleOp(char operation, int op1, int op2)
那么,由上可以看出,需要传入char型运算符、int型操作数1、int型操作数2。operation
就是运算符,需要传token
,但是token
是字符串,所以使用string.charAt(0)
,把token
转换成token.charAt(0)
。至于op1、op2本身就是int型。
综上得出,需要填入
result=evalSingleOp(token.charAt(0), op1, op2);
-
计算
result
入栈
stack.push(result);
-
如果是操作数,操作数入栈
这里只能让int型入栈,但是输入的token
是字符串,所以必须要使用Integer.parseInt(string)
转化成int型。
故填入
stack.push(Integer.parseInt(token));
完整代码
-
git链接
-
MyDC
/**
* Created by radish608 on 17-5-3.
*/
import java.util.StringTokenizer;
import java.util.Stack;
public class MyDC {
/**
* constant for addition symbol
*/
private final char ADD = '+';
/**
* constant for subtraction symbol
*/
private final char SUBTRACT = '-';
/**
* constant for multiplication symbol
*/
private final char MULTIPLY = '*';
/**
* constant for division symbol
*/
private final char DIVIDE = '/';
/**
* the stack
*/
private Stack<Integer> stack;
public MyDC() {
stack = new Stack<Integer>();
}
public int evaluate(String expr) {
int op1, op2, result = 0;
String token;
StringTokenizer tokenizer = new StringTokenizer(expr);
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
//如果是运算符,调用isOperator
if (isOperator(token)) {
//从栈中弹出操作数2
op2 = stack.pop();
//从栈中弹出操作数1
op1 = stack.pop();
//根据运算符和两个操作数调用evalSingleOp计算result;
result=evalSingleOp(token.charAt(0), op1, op2);
//计算result入栈;
stack.push(result);
} else//如果是操作数
//操作数入栈;
stack.push(Integer.parseInt(token));
}
return result;
}
private boolean isOperator(String token) {
return (token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/"));
}
private int evalSingleOp(char operation, int op1, int op2) {
int result = 0;
switch (operation) {
case ADD:
result = op1 + op2;
break;
case SUBTRACT:
result = op1 - op2;
break;
case MULTIPLY:
result = op1 * op2;
break;
case DIVIDE:
result = op1 / op2;
}
return result;
}
}
-
MyDCTester
/**
* Created by radish608 on 17-5-3.
*/
import java.util.Scanner;
public class MyDCTester {
public static void main(String[] args) {
String expression, again;
int result;
try {
Scanner in = new Scanner(System.in);
do {
MyDC evaluator = new MyDC();
System.out.println("Enter a valid postfix expression: ");
expression = in.nextLine();
result = evaluator.evaluate(expression);
System.out.println();
System.out.println("That expression equals " + result);
System.out.print("Evaluate another expression [Y/N]? ");
again = in.nextLine();
System.out.println();
}
while (again.equalsIgnoreCase("y"));
} catch (Exception IOException) {
System.out.println("Input exception reported");
}
}
}
运行结果
- 用例:
1 2 + 8 2 - 7 4 - / *
- 截图:
)
-
MyDC
/**
* Created by radish608 on 17-5-3.
*/
import java.util.StringTokenizer;
import java.util.Stack;
public class MyDC {
/**
* constant for addition symbol
*/
private final char ADD = '+';
/**
* constant for subtraction symbol
*/
private final char SUBTRACT = '-';
/**
* constant for multiplication symbol
*/
private final char MULTIPLY = '*';
/**
* constant for division symbol
*/
private final char DIVIDE = '/';
/**
* the stack
*/
private Stack<Integer> stack;
public MyDC() {
stack = new Stack<Integer>();
}
public int evaluate(String expr) {
int op1, op2, result = 0;
String token;
StringTokenizer tokenizer = new StringTokenizer(expr);
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
//如果是运算符,调用isOperator
if (isOperator(token)) {
//从栈中弹出操作数2
op2 = stack.pop();
//从栈中弹出操作数1
op1 = stack.pop();
//根据运算符和两个操作数调用evalSingleOp计算result;
result=evalSingleOp(token.charAt(0), op1, op2);
//计算result入栈;
stack.push(result);
} else//如果是操作数
//操作数入栈;
stack.push(Integer.parseInt(token));
}
return result;
}
private boolean isOperator(String token) {
return (token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/"));
}
private int evalSingleOp(char operation, int op1, int op2) {
int result = 0;
switch (operation) {
case ADD:
result = op1 + op2;
break;
case SUBTRACT:
result = op1 - op2;
break;
case MULTIPLY:
result = op1 * op2;
break;
case DIVIDE:
result = op1 / op2;
}
return result;
}
}
-
MyDCTester
/**
* Created by radish608 on 17-5-3.
*/
import java.util.Scanner;
public class MyDCTester {
public static void main(String[] args) {
String expression, again;
int result;
try {
Scanner in = new Scanner(System.in);
do {
MyDC evaluator = new MyDC();
System.out.println("Enter a valid postfix expression: ");
expression = in.nextLine();
result = evaluator.evaluate(expression);
System.out.println();
System.out.println("That expression equals " + result);
System.out.print("Evaluate another expression [Y/N]? ");
again = in.nextLine();
System.out.println();
}
while (again.equalsIgnoreCase("y"));
} catch (Exception IOException) {
System.out.println("Input exception reported");
}
}
}
运行结果
- 用例:
1 2 + 8 2 - 7 4 - / *
- 截图: