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

example 3 2 + 5* = (3+2) * 5 i need the paraenthesis his is what i have so far p

ID: 3542684 • Letter: E

Question

example


3 2 + 5* = (3+2) * 5


i need the paraenthesis


his is what i have so far

public String postfix(String output){

   newStack = new Stack();

   char a = ('(');

   char b = (')');

   for (int i=0; i<input.length(); i++){

     char ch = input.charAt(i);

     int pre1;

     int pre2;

     if ( ch == '+'|| ch == '-'|| ch == '*' || ch == '/'){

     newStack.push(ch);

// i don't know what to do here.

     }

     } else {

     output = a + output + ch + b;

     }

   }

System.out.println( output);

   return output;


   }   

Explanation / Answer

//here is the perfect one

//this is complete program

import java.util.*;

public class post_to_in

{

public static void main(String[] args)

{

String post = "32+5*";

String in;

in = postfix(post);

}


static String postfix(String input_post)

{

char[] input = input_post.toCharArray();

myStack newStack = new myStack();

String oprnd1,oprnd2,output;

char a = '(';

char b = ')';


for (int i=0; i<input_post.length(); i++)

{

char ch = input[i];

if ( ch == '+'|| ch == '-'|| ch == '*' || ch == '/')

{

oprnd2 = newStack.mypop();

oprnd1 = newStack.mypop();

output = a+oprnd1+ch+oprnd2+b;

newStack.mypush(output);

}

else

{

oprnd1 = ""+ch;

newStack.mypush(oprnd1);

}

}

oprnd1 = newStack.mypop();

int n = oprnd1.length();

output = oprnd1.substring(1,n-1);

System.out.println(output);

return output;

}


}


class myStack

{

int index;

public myStack()

{

index = 0;

}

String[] data = new String[100];


public void mypush(String str)

{

data[index++]=str;

}


public String mypop()

{

return data[--index];

}


}