This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hey everybody first-time post. The issue I am running into is currently I am creating a token for each character which is fine, but when I have an expression like (10 2) it should equal 12, not 2. I have tried adding in a delimiter as shown commented out, but I do not quite understand exactly how a StringTokenizer works. From my understanding, I see two different ways of solving this issue. First, use the StringTokenizer to split the expression for operators( -*/) and values. Second, ive considered changing the else statement. I'm posting here because I have been trying to wrap my mind around this and I could use a push in the right direction.
// shuntingYard algorithm to convert fromc infix to postfix
public String shuntingYard(String infix) {
StringBuilder output = new StringBuilder();
Stack<String> stack = new Stack<String>();
//String DELIMITERS = "\" -*/^\"";
//this.tokenizer = new StringTokenizer(RPN, DELIMITERS, true);
for (String token : infix.split("")) {
//operator
if (Operator.operators.containsKey(token)) {
while ( ! stack.isEmpty() && isHigherPrec(token, stack.peek())) {
output.append(stack.pop()).append(' ');
}
stack.push(token);
}
//left parenthesis
else if (token.equals("(")) {
stack.push(token);
}
//right parenthesis
else if (token.equals(")")) {
while ( ! stack.peek().equals("(")) {
output.append(stack.pop()).append(' ');
}
stack.pop();
}
//digit
else {
// Find a way to check for multidigits
output.append(token).append(' ');
}
}
while ( ! stack.isEmpty()) {
output.append(stack.pop()).append(' ');
}
return output.toString();
}
Subreddit
Post Details
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/javahelp/co...