Question: please use these classes to write the code for above one and don't use any inbuilt functions. Thanks ############################### package stack; class Evaluation { public

 please use these classes to write the code for above one

please use these classes to write the code for above one and don't use any inbuilt functions. Thanks

###############################

package stack;

class Evaluation {

public int eval_postfix(String s) {

int res = 0;

Stack s1 = new Stack(s.length());

int n1; // result of 1st popping

int n2; // result of 2nd popping

for (int i = 0; i

char ch = s.charAt(i);

if (ch == ' ') {

continue;

} else {

if (ch >= '0' && ch

s1.push(ch - '0');

} else {

n2 = Integer.parseInt(Integer.toString(s1.pop()));

n1 = Integer.parseInt("" + s1.pop());

switch (ch) {

case '+':

s1.push(n1 + n2);

break;

case '-':

s1.push(n1 - n2);

break;

case '*':

s1.push(n1 * n2);

break;

default:

System.out.println("Expression is invalid!");

return -999;

}

}

}

}

res = Integer.parseInt(Integer.toString(s1.pop()));

if (!s1.isEmpty()) {

System.out.println("Expression is invalid!");

System.exit(1);

}

return res;

}

}

##################

package stack;

class Stack {

private int maxSize,top; // size of stack array and top of stack

private int[] stackArray;

//--------------------------------------------------------------

// This is the constructor that takes as an argument the maximum size

public Stack(int max) // constructor

{

maxSize = max; // set array size

stackArray = new int[max]; // create array

top = -1; // no items yet

}

//--------------------------------------------------------------

//put item on top of stack

public void push(int key)

{

if (top

stackArray[++top] = key; // increment top, insert item}

}else {

System.out.println("Stack is Full");

System.exit(1);

}

}

//--------------------------------------------------------------

public int pop() // take item from top of stack

{

if (top>=0) {

return stackArray[top--]; // access item, decrement top}

} else {

System.out.println("Expression is invalid!");

System.exit(1);

return -999;

}

}

//--------------------------------------------------------------

public boolean isEmpty() {

return (top == -1);

}

} // end class Stack

###################

package stack;

import java.util.Scanner;

public class PostfixEvaluation {

public static void main(String[] args) {

{Scanner input = new Scanner(System.in);

String exp;

Evaluation ex1 = new Evaluation();

System.out.println("Enter your expression:");

exp = input.nextLine();

input.close();

System.out.println(exp + " = " + ex1.eval_postfix(exp));

}

}

}

Exercise 2: Convert a fully parenthesized to postfix expression Implement an algorithm to convert a fully parenthesized to postfix expression 1. Demonstrate the output by including some tests in the main () function The algorithm 1. Initialize a stack of characters to hold the operation symbols and parentheses. 2. Do If (the next input is left parenthesis) Read the left parenthesis and push it onto the stack. Else if (the next input is a number or other operand) Read the operand and write it to the output Else if (the next input is one of the operation symbols) Read the operation symbol and push it onto the stack. Else { Read and discard the next input symbol (which should be a right parenthesis). There should be an operation symbol on top of the stack, so pop this symbol and write it to the output. (if there is no such symbol, then print an error message indicating that there were too few operations in the infix expression to half). After popping the operation symbol, there should be a left parenthesis on the top of the stack, so pop and discard this left parenthesis. (if there was no left parenthesis, then the input did not have balanced parentheses, so print an error message and half) } While (there is more of the expression to read); 3. At this point, the stack should be empty. If not, print an error message indicating that the expression was not fully parenthesized. Input: (((A+7)*(B/C))-(2*D)) Output: A 7+BC/*2D*. Exercise 2: Convert a fully parenthesized to postfix expression Implement an algorithm to convert a fully parenthesized to postfix expression 1. Demonstrate the output by including some tests in the main () function The algorithm 1. Initialize a stack of characters to hold the operation symbols and parentheses. 2. Do If (the next input is left parenthesis) Read the left parenthesis and push it onto the stack. Else if (the next input is a number or other operand) Read the operand and write it to the output Else if (the next input is one of the operation symbols) Read the operation symbol and push it onto the stack. Else { Read and discard the next input symbol (which should be a right parenthesis). There should be an operation symbol on top of the stack, so pop this symbol and write it to the output. (if there is no such symbol, then print an error message indicating that there were too few operations in the infix expression to half). After popping the operation symbol, there should be a left parenthesis on the top of the stack, so pop and discard this left parenthesis. (if there was no left parenthesis, then the input did not have balanced parentheses, so print an error message and half) } While (there is more of the expression to read); 3. At this point, the stack should be empty. If not, print an error message indicating that the expression was not fully parenthesized. Input: (((A+7)*(B/C))-(2*D)) Output: A 7+BC/*2D*

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!