【发布时间】:2019-03-21 15:18:42
【问题描述】:
在这个作业中,我需要阅读.txt 文件并确定表达式是否正确或“平衡”。我得到的第一个问题是正确的,但是对于第二个问题,我得到的输出比我想要的要多。这是#2的问题:
编写一个基于堆栈的算法来计算后置表达式。您的程序需要从名为“problem2.txt”的文件中读取其输入。该文件每行包含一个表达式。 对于每个表达式,将其值输出到标准输出。如果表达式格式不正确,则打印“Ill-formed”。
Problem2.txt如下:
3 2 + 5 6 8 2 / + + * 1 +
8 * 2 3 + + - 9 1 +
1 4 + 9 4 - * 2 *
// For my output I need to get:
76
Ill-formed
50
// With my code I am getting:
76
Ill-formatted
Ill-formatted
Ill-formatted
10
50
// and I’m not sure why I’m getting extra ill-formatted and a 10 in there
下面是我的代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;
import java.util.EmptyStackException;
public class Eval {
public static void main(String args[]) throws IOException {
//driver
try (BufferedReader filereader = new BufferedReader(new FileReader("Problem1.txt"))) {
while (true) {
String line = filereader.readLine();
if (line == null) {
break;
}
System.out.println(balancedP(line));
}
}
System.out.println("\n");
try (BufferedReader filereader2 = new BufferedReader(new FileReader("Problem2.txt"))) {
while (true) {
String line = filereader2.readLine();
if (line == null) {
break;
}
System.out.println(evaluatePostfix(line));
}
}
}
public static boolean balancedP (String s) {
Stack<Character> stackEval = new Stack<Character>();
for(int i = 0; i < s.length(); i++) {
char token = s.charAt(i);
if(token == '[' || token == '(' || token == '{' ) {
stackEval.push(token);
} else if(token == ']') {
if(stackEval.isEmpty() || stackEval.pop() != '[') {
return false;
}
} else if(token == ')') {
if(stackEval.isEmpty() || stackEval.pop() != '(') {
return false;
}
} else if(token == '}') {
if(stackEval.isEmpty() || stackEval.pop() != '{') {
return false;
}
}
}
return stackEval.isEmpty();
}
//problem 2 algo to evaluate a post-fixed expression
static int evaluatePostfix(String exp) throws EmptyStackException
{
Stack<Integer> stackEval2 = new Stack<>();
for(int i = 0; i < exp.length(); i++)
{
char c = exp.charAt(i);
if(c == ' ')
continue;
else if(Character.isDigit(c)) {
int n = 0;
while(Character.isDigit(c)) {
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i);
}
i--;
stackEval2.push(n);
}
else {
try {
//if operand pops two values to do the calculation through the switch statement
int val1 = stackEval2.pop();
int val2 = stackEval2.pop();
//operands in a switch to test and do the operator's function each value grabbed and tested
switch(c) {
case '+':
stackEval2.push(val2 + val1);
break;
case '-':
stackEval2.push(val2 - val1);
break;
case '/':
stackEval2.push(val2 / val1);
break;
case '*':
stackEval2.push(val2 * val1);
break;
}
} catch (EmptyStackException e) {
System.out.println("Ill-formatted");
}
}
}
return stackEval2.pop();
}
}
【问题讨论】:
-
我认为第一个表达式
3 2 + 5 6 8 2 / + + * 1 +不是有效的后缀。在第一次添加之后,这将变为5 5 6 8 2 / + + * 1 +,这没有任何意义。 -
这是我老师给我的表达方式,我无法改变。我的代码仍然为我提供了我正在寻找的正确响应。我认为出现多个格式错误的问题在于表达式 2。
-
第二个表达式的格式肯定是错误的,因为
*出现时只有一个可用的操作数在堆栈上。我对第二个表达式的输出没有问题。 -
在我这边,我得到了 3 种错误格式,而不是只有 1 种。你知道为什么会这样吗?
-
您收到多个“格式错误”的消息和意外结果,因为您在确定表达式格式错误后继续尝试评估该表达式。而不是在您的
catch子句中打印一条消息,您需要break;' out of thefor` 循环并然后显示错误。
标签: java stack equation evaluation