Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are to write a program name calc.java that evaluates an infix expression ent

ID: 3856071 • Letter: Y

Question

You are to write a program name calc.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: Integer constants (a series of decimal digits). x (representing a value to be supplied later). Binary operators (+, -, *, /and %). Parentheses Spaces between tokens are allowed but not required. The program will convert the expression to postfix (RPN) form and display the converted expression. The following example illustrates the behavior of the program (user input is in bold and red): Porgram output is in bold and green. Enter infix expression: (7 + 1) * (8 - 2)/4 Converted expression: 71 + 82 - *4/ The result is: 12 Now, solve the postfix expression and return a result. You must now use an operand stack to process the postfix expression. Display this result on the screen as well as the postfix expression as shown above. If the infix expression contains an error of any kind, the program must display the message Error in expression (with an optional explanation) and then terminate. The following examples illustrate various types of errors: Enter infix expression: 12+ Error in expression!! No operator between operands. Also last token must be an operand. Enter infix expression: 10.4 Error in expression!! Cannot accept floating point numbers. Enter infix expression: 1(+ 2) Error in expression!! No operator between operand and left parentheses. Enter infix expression: 5 - (x - 2)) Error in expression!! No matching left parentheses for a right parentheses. Enter infix expression: 1** 2 Error in expression!! The * operator cannot be preceded by a * operator. The output of your program must match the format illustrated in this example. Here are some other additional requirements for this program: (1) You must use stack objects during the translation from infix to postfix. (2) Operators must have the correct precedence and associativity. Binary operators *, /and % takes precedence over binary + and -. And processing is always done from left to right.

Explanation / Answer

import java.io.IOException;

import java.util.*;

public class InPost {

private Stackk theStack;

private String input;

private String output = "";

public InPost(String in) {

input = in;

int stackSize = input.length();

theStack = new Stackk (stackSize);

}

//To convert infix to postfix

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 '(':

theStack.push(ch);

break;

case ')':

gotParen(ch);

break;

default:

output = output + ch;

break;

}

}

while (!theStack.isEmpty()) {

output = output + theStack.pop();

}

return output;

}

public void gotOper(char opThis, int prec1) {

while (!theStack.isEmpty()) {

char opTop = theStack.pop();

if (opTop == '(') {

theStack.push(opTop);

break;

} else {

int prec2;

if (opTop == '+' || opTop == '-')

prec2 = 1;

else

prec2 = 2;

if (prec2 < prec1) {

theStack.push(opTop);

break;

}

else output = output + opTop;

}

}

theStack.push(opThis);

}

public void gotParen(char ch) {

while (!theStack.isEmpty()) {

char chx = theStack.pop();

if (chx == '(')

break;

else output = output + chx;

}

}

public static void main(String[] args) throws EmptyStackException {

Scanner console= new Scanner(System.in);

System.out.println("Enter infix Statement: ");

String input = console.nextLine();

System.out.println("Infix is "+input);

String output;

InPost theTrans = new InPost(input);

output = theTrans.doTrans();

System.out.println("Postfix is " + output + ' ');

int result = PostfixEvaluate(output);

System.out.println("result is " +result);

}

class Stackk {

private int maxSize;

private char[] stackArray;

private int top;

  

public Stackk (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);

}

}

//To calculate the Result

static int PostfixEvaluate(String e) throws EmptyStackException {

  

int number1;

int number2;

int result = 0;

char c;

number1 = 0;

number2 = 0;

Stack<Character> s = new Stack<Character>();

for (int j = 0; j < e.length(); j++) {

c = e.charAt(j);

if (c == (Integer) (number1)) {

s.push(c);

} else {

number1 = s.pop();

number2 = s.pop();

switch (c) {

case '+':

result = number1 + number2;

break;

case '-':

result = number1 - number2;

break;

case '*':

result = number1 * number2;

break;

case '/':

result = number1 / number2;

break;

case '%':

result = number1 % number2;

break;

}

s.push((char) (result));

}

}

return s.pop();

}

}

Sample Output:

Enter infix Statement:
(7+1)*(8-2)/4

Infix is (7+1)*(8-2)/4

result is 12.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote