This project requires the use of the following Java program files:
|
Download them to your PC before doing this project.
Abide to the Emory Honor code when doing assignments.
(1) brackets: ( ) (2) 2 operators: + - (3) operands (= numbers) |
Example:
"4" "+" "(" "5" "-" "6" "+" "7" ")" "-" "8" |
Examples: (I omitted the string quotes for brevity)
Input Output ----------------- ------------------------- 3 + 4 3 4 + 3 + 4 - 5 3 4 + 5 - ( 3 + 4 ) - 5 3 4 + 5 - 3 + ( 4 - 5 ) 3 4 5 - + |
The details on what to do is in the next section of the write up.
Requirement:
|
I will describe what infix to postfix conversion algorithm need to do for each type of inputs next.
I will first discuss the processing of an expression without brackets and then discuss how to processes brackets.
Consider the input: "3" "+" "4" "-" "5" : (I have omitted the string quotes in the input for brevity)
inputs: OperatorStack Output: 3 + 4 - 5 | | ^ | | | | | current symbol +-------+ |
When the current symbol is an operand, you output the symbol immediately:
inputs: OperatorStack Output: 3 + 4 - 5 | | "3" ^ | | | | | current symbol +-------+ |
When the current symbol is an operator, you (1) first pop the operator stack if the top contains an operator and output this operator and (2) then push the current symbol on the operator stack:
inputs: OperatorStack Output: 3 + 4 - 5 | | "3" ^ | | | | + | current symbol +-------+ |
When the current symbol is an operand, you output the symbol immediately:
inputs: OperatorStack Output: 3 + 4 - 5 | | "3" "4" ^ | | | | + | current symbol +-------+ |
When the current symbol is an operator, you (1) first pop the operator stack if the top contains an operator and output this operator and (2) then push the current symbol on the operator stack:
inputs: OperatorStack Output: 3 + 4 - 5 | | "3" "4" "+" ^ | | | | - | current symbol +-------+ |
When the current symbol is an operand, you output the symbol immediately:
inputs: OperatorStack Output: 3 + 4 - 5 | | "3" "4" "+" "5" ^ | | | | - | current symbol +-------+ |
When the input is exhausted, you pop the operator stack and output the symbol:
inputs: OperatorStack Output: 3 + 4 - 5 | | "3" "4" "+" "5" "-" ^ | | | | | current symbol +-------+ |
The output that you have generated is the postfix expression of the input infix expression !!
inputs: OperatorStack Output: 3 + ( 4 - 5 ) | | ^ | | | | | current +-------+ |
When the current symbol is an operand, you output the symbol immediately:
inputs: OperatorStack Output: 3 + ( 4 - 5 ) | | 3 ^ | | | | | current +-------+ |
When the current symbol is an operator, you (1) first pop the operator stack if the top contains an operator and output this operator and (2) then push the current symbol on the operator stack:
inputs: OperatorStack Output: 3 + ( 4 - 5 ) | | 3 ^ | | | | + | current +-------+ |
When the current symbol is a left bracket (, you push the current symbol on the operator stack:
inputs: OperatorStack Output: 3 + ( 4 - 5 ) | | 3 ^ | ( | | | + | current +-------+ |
When the current symbol is an operand, you output the symbol immediately:
inputs: OperatorStack Output: 3 + ( 4 - 5 ) | | 3 4 ^ | ( | | | + | current +-------+ |
When the current symbol is an operator, you (1) first pop the operator stack if the top contains an operator and output this operator and (2) then push the current symbol on the operator stack
In this case, the top of the stack contains ( it is not an operator. Result:
inputs: OperatorStack Output: 3 + ( 4 - 5 ) | - | 3 4 ^ | ( | | | + | current +-------+ |
When the current symbol is an operand, you output the symbol immediately:
inputs: OperatorStack Output: 3 + ( 4 - 5 ) | - | 3 4 5 ^ | ( | | | + | current +-------+ |
When the current symbol is a right bracket ), you pop all the operators off the operator stack until the matching ( and output the operators (do not output the brackets) that you have popped (in the popped order)
Result:
inputs: OperatorStack Output: 3 + ( 4 - 5 ) | | 3 4 5 - ^ | | | | + | current +-------+ |
When the input is exhausted, you pop the operator stack and output the symbol:
inputs: OperatorStack Output: 3 + ( 4 - 5 ) | | 3 4 5 - + ^ | | | | | current +-------+ |
Done.
Because the size of this array is highly variable, we will use an ArrayList<String> object to store the output.
The Test1.java tests some simple infix expression consisting of 2 operands and 1 operator
You should use this test program to test the processing of operands and operators by your algorithm
If you have bugs in your convertToPostfix( ) method, I suggest that you print the content of the ArrayList<String> object and the operator stack out after processing each symbol so you can compare what your code is doing with my examples above.
When you finish coding the processing steps for operators and operands, you can use Test1.java to test your code.
Passing this test will get you 30 pts.
The Test2.java test program tests the conversion of more complex infix expressions consisting of 3 or more operands and 2 or more operators
You should use Test2.java only after your code has passed Test 1 - because debugging will be more difficult (you see more outputs and it will be more confusing)
If you have code the processing of operands and operators correctly, your code will pass Test2 also.
If not, track down your error by printing the content of the ArrayList<String> object and the operator stack out after processing each symbol so you can compare what your code is doing with my examples above.
(Input 1 of Test1.java is the same as my first example)
The Test3.java tests the conversion of simple infix expressions with brackets
You will need to add processing steps for left bracket ( and right bracket ) to your code as described above.
When you're done, use Test3.jave to test and debug your code.
If you have bugs in your convertToPostfix( ) method, I suggest that you print the content of the ArrayList<String> object and the operator stack out after processing each symbol so you can compare what your code is doing with my examples above.
The Test4.java tests the conversion of complex infix expressions with brackets
If your processing steps for left bracket ( and right bracket ) were correct, your code will pass this test immediately.
But if your code fail to pass this test, I suggest that you print the content of the ArrayList<String> object and the operator stack out after processing each symbol so you can compare what your code is doing with my examples above.
You must be on Emory campus (e.g.: Emory unplugged) in order to turn in an assignment
Use your web browser and visit the cs171 turnin website: click here
|
Follow the instructions given in homework 1 to turn in.
You must be on Emory campus (e.g.: Emory unplugged) in order to make extension requests
Use your web browser and visit the cs171 turnin website: click here
Use assignment code hw6 to request extension for this homework.
Follow the instructions given in homework 1 to request extension.