File: Objective: Write a program that will populate a queue with post-fix (rever
ID: 3888019 • Letter: F
Question
File:Objective: Write a program that will populate a queue with post-fix (reverse polish) integer expressions and then print out the solution to each expression. To understand this mostly every expression you've seen in math is called in-fix notation. In-fix Example 3 2 However in post-fix notation the operator is at the end of the expression. Post-fix Example 3 2 + A stack is used to calculate values in post-fix notation. Numbers are pushed onto the stack, and when an operator is reached it pops off the last two numbers and then pushes the resulting value back on the stack. See these EXAMPLE SLIDES for further information. This program must .Assume that each number and operator is separated by any type of space, and each expression is separated by a new line Calculate values using the algorithm mentioned above and in the slides for addition, subtraction, multiplication, and division (+,-,* Error message if the user tries to divide by zero . Read a given file Make sure to check if there's at least two items on the stack before when an operator is encountered or else it's not a properly formatted post-fix expression Finish when the program reaches the end of the string and it has only one value left on the stack, then that value is popped off and returned. Otherwise it was not properly formatted Use your own created stack and ueue, and not java's built in stack and q queue o You may use any implementation so either an array or a linked structure. HINTS! First when you're processing the inputted string you can use a Scanner and call the method nextO to get each token. From there you can evaluate if that token is an operator numeric value, or an error. If it's a numeric value then Integer.parseInt0 to get the integer value. You may also call Integer parselnt0 in a try-catch block. If the token is not an integer an exception will be raised and should be immediately halted. you may parse that string using
Explanation / Answer
**Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class Test
{
// Method to evaluate value of a postfix expression
static void calculatePostFix(String expression)
{
//creating a stack for calculation
Stack<Integer> myStack = new Stack<>();
// Scanning the characters one by one
for(int i=0;i<expression.length();i++)
{
char c=expression.charAt(i);
// If the character c is an number, then it is pushed into the stack.
if(c != ' ')
if(Character.isDigit(c))
myStack.push(c - '0');
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else
{
int number1 = myStack.pop();
int number2 = myStack.pop();
switch(c)
{
case '+':
myStack.push(number2+number1);
break;
case '-':
myStack.push(number2- number1);
break;
case '/':
myStack.push(number2/number1);
break;
case '*':
myStack.push(number2*number1);
break;
}
}
}
int result = myStack.pop();
if(myStack.empty())
System.out.println("Result is " + result);
else
System.out.println("Ill formatted expression");
}
// Driver program to test above functions
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the reverse Polish Calculator!");
System.out.println("Enter in the name of the file");
String fileName = in.nextLine();
System.out.println();
try {
Scanner scanner = new Scanner(new File(fileName));
while (scanner.hasNextLine()) {
String exp = scanner.nextLine();
System.out.println("Calculating " + exp);
calculatePostFix(exp);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
**Comment for any further queries
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.