Postfix Expression Java
Postfix/.DS_Store
__MACOSX/Postfix/._.DS_Store
Postfix/a1.pdf
Pos?ix EvaluaCon
• Modify the pos?ix evaluaCon program we discussed in class in the following ways – Expressions can include the operators +, *, -‐, /, % (mod), ! (unary minus), ^ (exponenCaCon)
– If the input file does not exists, print an error message and exit the program
– If a token is not an integer or a legal operator print a message showing the illegal token and stop evaluaCon of the expression. Do not print a value for the expression. When this error occurs the program does not exit. AXer the error message is printed the program tries to evaluate the remaining expressions In the input file
Pos?ix EvaluaCon • AddiConal error checks
– If there are not enough operands on the stack for an operator (i.e. when you try to pop the operands for an operator) print a message indicaCng the operator that causes the problem and stop evaluaCon of the expression. Do not print an value for the expression. When this error occurs the program does not exit. AXer the error message is printed the program tries to evaluate the remaining expressions In the input file
– When all tokens for an expression have been used there should be exactly one value on the operand stack. If this is not the case, print an error message. As in the previous errors do not print a value for the expression and the program should try to evaluate the remaining expressions in the input file.
Pos?ix EvaluaCon
• As in the example we discussed in class there will be one expression per line in the input file and the tokens will be separated by whitespace.
• The following expressions matches the operator tokens for this problem – tokens[i].matches("\\+|\\-‐|\\!|\\*|\\%|\\/|\\^")
Assignment Submission
• Send me only 1 Java file. The file should be called Pos?ixExpr.java
• The first line in the file should be a comment with your name in the comment
• In the subject field of the email put the value CS340 A1
__MACOSX/Postfix/._a1.pdf
Postfix/Class Example.png
__MACOSX/Postfix/._Class Example.png
Postfix/src/Main.java
Postfix/src/Main.java
import
java
.
io
.
IOException
;
import
java
.
io
.
FileReader
;
import
java
.
io
.
BufferedReader
;
public
class
Main
{
public
Main
(){
}
public
static
void
main
(
String
args
[])
throws
IOException
{
PostfixExpr
p
;
try
{
BufferedReader
b
=
new
BufferedReader
(
new
FileReader
(
args
[
1
]));
String
line
=
b
.
readLine
();
while
(
line
!=
null
)
{
p
=
new
PostfixExpr
(
line
);
System
.
out
.
println
(
line
+
" = "
+
p
.
eval
());
line
=
b
.
readLine
();
}
b
.
close
();
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
"Input error!"
);
}
}
}
Postfix/src/PostfixExpr.java
Postfix/src/PostfixExpr.java
import
java
.
util
.
Stack
;
public
class
PostfixExpr
{
//A class that can be used to evaluate syntactically correct postfix expressions
String
tokens
[];
Stack
<
Integer
>
operands
;
public
PostfixExpr
(
String
exp
)
{
tokens
=
exp
.
split
(
"\\s+"
);
// \s+ matches 1 or more whitespace characters
}
public
int
eval
()
{
operands
=
new
Stack
<>
();
int
i
=
0
;
while
(
i
<
tokens
.
length
)
{
// +|* matches an operators
if
(
tokens
[
i
].
matches
(
"\\+|\\*"
))
doOperator
(
tokens
[
i
]);
// \d+ matches 1 or more digits
else
if
(
tokens
[
i
].
matches
(
"\\d+"
))
operands
.
push
(
new
Integer
(
tokens
[
i
]));
//else illegal token not handled in this example
i
++
;
}
return
operands
.
pop
();
}
private
void
doOperator
(
String
operator
)
{
int
operand1
=
operands
.
pop
();
int
operand2
=
operands
.
pop
();
switch
(
operator
)
{
case
"+"
:
operands
.
push
(
operand2
+
operand1
);
break
;
case
"*"
:
operands
.
push
(
operand2
*
operand1
);
break
;
}
}
}