java堆栈代码,基于TXT文件的Java堆栈评估

在这个任务中,我需要阅读

.txt

归档并确定表达式是正确的还是“平衡的”。第一个问题我得到了纠正,但第二个问题我得到了比我想要的更多的输出。问题在于2:

编写一个基于堆栈的算法来计算后固定表达式。您的程序需要从名为problem2.txt的文件中读取其输入。此文件每行包含一个表达式。

对于每个表达式,将其值输出到标准输出。如果表达式格式错误,则打印格式错误。

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 stackEval = new Stack();

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 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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值