public class ShuntingYard
extends java.lang.Object
2 -3 - 5
2 3 - 5 -
3 + 5 * 6
3 5 6 * +
(3 + 4) * ( 9 - 8)
3 4 + 9 8 - *
n = k = 4
n k 4 = =
net + pay
net pay +
3 + 4 * 5
3 4 5 * +
1 + 2 + 3 + 4 * 5
1 2 + 3 + 4 5 * +
3^5^6
3 5 6 ^ ^
4^(3+4)
4 3 4 + ^
123 + 456
123 456 +
12.34 + 65.66
12.34 65.66 +
There are two main data structures involved in the computation: A stack is used to hold operators and a queue is used to store the output as it is generated.
Constructor and Description |
---|
ShuntingYard() |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getQueueAsString() |
boolean |
isPrecedenceLessThan(Token o1,
Token o2) |
boolean |
isPrecedenceLessThanOrEqualTo(Token o1,
Token o2) |
boolean |
leftAssociative(Token o) |
static void |
main(java.lang.String[] args)
The main routine reads one line of infix and, when the return key is entered,
writes one line of the corresponding postfix.
|
boolean |
rightAssociative(Token o) |
void |
shuntingYard(Token[] toks,
int size)
Perform the Shunting-yard algorithm and add the postfix
expression to the queue.
|
public java.lang.String getQueueAsString()
public boolean leftAssociative(Token o)
o
- is a Token from the infix expressionpublic boolean rightAssociative(Token o)
o
- is a Token from the infix expressionpublic boolean isPrecedenceLessThanOrEqualTo(Token o1, Token o2)
o1
- is a Token from the infix expressiono2
- is a Token from the infix expressionpublic boolean isPrecedenceLessThan(Token o1, Token o2)
o1
- is a Token from the infix expressiono2
- is a Token from the infix expressionpublic void shuntingYard(Token[] toks, int size)
toks
- an array of Tokenssize
- of this sizepublic static void main(java.lang.String[] args) throws java.io.IOException
args
- not usedjava.io.IOException