Comp 182/182L

advertisement
Comp 182
Schwartz
Fall 2010
Lab Project #5
Postfix Expressions
Points: 25
Due: Oct 27 ( Beginning of lecture)
In this project you will
1. Create an IntegerStack class and a StringStack class based on our classroom
discussion. The pop() and peek() methods of these classes should throw a
StackEmptyException if the stack is empty. The push( item) method should
throw a StackFullException if the stack is full.
2. Create an ArithmeticExpression class ( See ArithmeticExpression class outline
attached) which contains
a. a static method public static boolean isIntegerToken(String s)
b. a static method public static boolean isOperatorToken( String s)
c. a static method public static int postfixEvaluation( String postfixExpr)
d. a static method public static String infixToPostfix( String infixExpr)
e. a main method to test these infix and postfix expression methods. Your
main method will ask user which postfix expression method they want
to test and then ask the user for test input. Your class may have
additional static methods.
3. Your public static int postfixEvaluation( String postfixExpr) should evaluate
post fix expressions containing integers and the operators +, -, *, / and % and
return the integer result. You must use the postfix evaluation algorithm
discussed in class and your IntegerStack class. If you enter a postfix expression
that is syntactically incorrect, your postfixEvaluation program should throw a
SyntaxErrorException.
4. Your public static String infixToPosfFix( String infixExpr) should convert an
infix expression containing integers, (, ), *, -, /, *, and % to a postfix
expression and return the resulting postfix expression. Use the pseudo code
infix to postfix expression algorithm handed out in class and use your
StringStack class. You may assume that all infix expressions are syntactically
correct.
5. Details on format of infix and postfix expressions
a. The postfix expression will contain only integers, +, - , *, /, % and
white space
b. The infix expressions will contain only integers , ( , ), +, - , *, /, % and
white space
c. The integers, operators and parentheses in infix and in postfix
expressions will be separated by one or more white spaces and every
expression will fit on one line.
d. Use a StringTokenizer to extract the operands, operators and
parentheses from the input String.
6. Your main method should
a. input postfix expressions from the keyboard and evaluate them using
your postfixEvaluation method and print the results like this:
Postfix Expression: 87 6 8 * Postfix Expression Value: 39
b. input infix expressions from the keyboard and convert them to postfix
expressions and then evaluate them using your postfixEvaluation
method and print the results like this:
Infix Expression: ( 8 + 50 ) * 4 / ( 2 * 3 )
Postfix Expression: 8 50 + 4 * 2 3 * /
Postfix Expression Value: 38
Continue to get input from the keyboard until the user types "quit".
Turn in
1. Program source code ( IntStack.java, CharStack.java, ArithmeticExpression.java)
2. Your own test case plan. Should be a comprehensive test.
3. Test results from your own test cases.
4. Instructor will test your code in lab ( JD 2214)
public class ArithmeticExpression
{
public static boolean isIntegerToken(String s)
{ }
public static boolean isOperatorToken(String s)
{ }
public static int postfixEvaluation( String postfixExpr) throws SyntaxException
{ }
public static String infixToPostfix( String infixExpr)
{ }
public static void main ( String[] args)
{
//See details of the assignment to see what you need to put in here.
}
}
public class Stack
{
private char[] data;
private int top;
public Stack(int n )
{
//n is the initial size of data
}
public boolean isEmpty()
{
}
public void push( char item)throws StackFullException
{
}
public char pop() throws StackEmptyException
{
}
public char peek() throws StackEmptyException
{
}
public void printStack(String s)
{
}
}
Download