Write a program in java for rpn equation(stacks and queue). In this program you
ID: 3818202 • Letter: W
Question
Write a program in java for rpn equation(stacks and queue). In this program you must create stacks without importing stacks. The program will prompt the user to type in math fomulas and display the aswer on the screen.
Answer 14
You are to write a program that allows the user to type in a mathematical formula at the keyboard and produces the answer on the screen. The formula will be typed in INFIX notation and will include only positive integers for the numbers. The operators that are acceptable are the following: (plus), minus (multiply (divide), an ower Parenthesis are also allowed. You should allow the user to type in the equation of his choice and then display the answer on the screen. Display a real answer. (for example: 3 /2 1.5, NOT 1) ALSO DISPLAY THE POST-FIXEQUATION ON THE SCREEN. The normal rules of mathematics apply parenthesis have the highest precedence followed by the A, followed by and followed by and +0 Do not allow the program to bomb and warn the user if the equation is wrong. For example: 2 +43 12 cannot be calculated because there are too many operators. When I test the program, I will only type positive integers as input and you can assume the input equation is valid Hints: You should treat the equation as a queue of tokens and read the tokens from the queue one at a time. Convert the INFIX equation to a POSTFIX equation. Then resolve the POSTFIX equation. Sample equations: Postfix 12 (2 4 /5 43 3 4 2 34 9 43 3 4 2 34 9 2 4 34 3 2 4 A 34 3 Here are 2 String methods that will come in handy: replaceAll and split Example of program running: Enter an equation 12 (6 4 (1+1) 2 RPN: 12 6 4 1 1 2Explanation / Answer
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class InfixToPostfix {
private CustomStack stack;
private String input;
private String output = "";
public InfixToPostfix(String in) {
input = in;
int stackSize = input.length();
stack = new CustomStack(stackSize);
}
public String doTrans() {
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
switch (ch) {
case '+':
case '-':
gotOper(ch, 1);
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
stack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
output = output + ch;
break;
}
}
while (!stack.isEmpty()) {
output = output + stack.pop();
}
System.out.println(output);
return output;
}
public void gotOper(char opThis, int prec1) {
while (!stack.isEmpty()) {
char opTop = stack.pop();
if (opTop == '(') {
stack.push(opTop);
break;
}
else {
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1)
{
stack.push(opTop);
break;
} else
output = output + opTop;
}
}
stack.push(opThis);
}
public void gotParen(char ch){
while (!stack.isEmpty()) {
char chx = stack.pop();
if (chx == '(')
break;
else
output = output + chx;
}
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println(" Enter infix expression: ");
String expr = sc.nextLine();
String postFix;
InfixToPostfix infixtopostfox = new InfixToPostfix(expr);
postFix = infixtopostfox.doTrans();
System.out.println("Postfix is " + postFix + ' ');
}
class CustomStack {
private int maxSize;
private char[] stackArray;
private int top;
public CustomStack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.