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

Infix to postfix conversion, also has to print the value of the expression. Must

ID: 3623866 • Letter: I

Question

Infix to postfix conversion, also has to print the value of the expression. Must implement the algorithm provided in the infixtopostfix and the evaluatepostfix and must follow framework in the class diagrams. Help is greatly appreciated!

Hints:

1. to push # into the stack:     theStack.push(new Operator('#'));

2. to pop a token into a variable of the type Token:

Token Current = (Token)theStack.pop();

3. to pop an operator into a variable of the type Operator:                                                             Operator Opr=(Operator)theStack.pop();

4. to cast a Token into an Operator: Opr = (Operator)Tkn;

5.to check if a Token variable Tkn contains an Operand: if(Tkn instanceof Operand)

6. to check if an operator is a '(': if(Opr.operator=='(')...

7 .to perform an operation:

switch(Opr.operator){

case '+': result = opnd1 + opnd2; break;

...}

?8. operator presedence: (operator)thestack.top();

 

import java.io.*;

import MyStackQueue.*;

class infix {

static Queue infixToPostfix(String s) 

{

*Initialize the stack with the dummy operator #

*Repeat until no more tokens:

*get the next token

*if the next token is an operand, enqueue the operand to the postfix

*if the next token is an operator, pop and enqueue operators to the postfix 

until the top of the stack has a lower precedence. Push the current operator into

the stack.

*if the next token is a (, push ( into the stack.

*if the next token is a ), pop and enqueue operators to the postfix until a ( is

on the top of the stack. Pop ( out of the stack.

*if no more tokens, pop and enqueue operators until the dummy opr. # is

on the top of the stack. Pop # out of the stack.

*note that # and ( has a precedence lower than other operators

 }

static int evaluePostfix(Queue Post)

 {

*Repeat until no more tokens

*If the next token is a number, push it into the stack

*if the next token is an operator pop two operands from the stack, do the operation,

and push the result back to the stack.

*The result is the only thing on the stack when done. (If there's more than one

thing in the stack, there is an error in the expression.) 

}

 

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

 Queue Post;

while(true){

System.out.print("Enter infix: ");

System.out.flush();

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

String s = br.readLine();

if ( s.equals("") ) break;

Post = infixToPostfix(s);

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

int result = evaluePostfix(Post);

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

}    

}

}

Infix to postfix conversion, also has to print the value of the expression. Must implement the algorithm provided in the infixtopostfix and the evaluatepostfix and must follow framework in the class diagrams. Help is greatly appreciated! uploaded image Hints: 1. to push # into the stack: theStack.push(new Operator('#')); 2. to pop a token into a variable of the type Token: Token Current = (Token)theStack.pop(); 3. to pop an operator into a variable of the type Operator: Operator Opr=(Operator)theStack.pop(); 4. to cast a Token into an Operator: Opr = (Operator)Tkn; 5.to check if a Token variable Tkn contains an Operand: if(Tkn instanceof Operand) 6. to check if an operator is a '(': if(Opr.operator=='(')... 7 .to perform an operation: switch(Opr.operator){ case '+': result = opnd1 + opnd2; break; ...} ?8. operator presedence: (operator)thestack.top(); import java.io.*; import MyStackQueue.*; class infix { static Queue infixToPostfix(String s) { *Initialize the stack with the dummy operator # *Repeat until no more tokens: *get the next token *if the next token is an operand, enqueue the operand to the postfix *if the next token is an operator, pop and enqueue operators to the postfix until the top of the stack has a lower precedence. Push the current operator into the stack. *if the next token is a (, push ( into the stack. *if the next token is a ), pop and enqueue operators to the postfix until a ( is on the top of the stack. Pop ( out of the stack. *if no more tokens, pop and enqueue operators until the dummy opr. # is on the top of the stack. Pop # out of the stack. *note that # and ( has a precedence lower than other operators } static int evaluePostfix(Queue Post) { *Repeat until no more tokens *If the next token is a number, push it into the stack *if the next token is an operator pop two operands from the stack, do the operation, and push the result back to the stack. *The result is the only thing on the stack when done. (If there's more than one thing in the stack, there is an error in the expression.) } public static void main(String[] args) throws IOException{ Queue Post; while(true){ System.out.print(??Enter infix: ??); System.out.flush(); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); if ( s.equals(????) ) break; Post = infixToPostfix(s); System.out.println(??Postfix is ?? + Post.toString() + 'n'); int result = evaluePostfix(Post); System.out.println(??Result is ?? + result + 'n'); } } }

Explanation / Answer

import java.awt.*; import javax.swing.*; public class infixtopostfix { public static void main( String args[] ) { StringBuffer infix = new StringBuffer(JOptionPane.showInputDialog( " Enter infix expression" ) ); System.out.println( " The infix expression is:"+ infix); StringBuffer postfix = convert( infix ); System.out.println( " The expression in postfix is:" + postfix ); System.exit( 0 ); } private static StringBuffer convert( StringBuffer infix ) { CharacterStack chStack = new CharacterStack(); StringBuffer temporary = new StringBuffer( "" ); chStack.pushChar( '(' ); chStack.print(); infix.append( ')' ); for ( int infixCount = 0; !chStack.isEmpty(); ++infixCount ) { if ( Character.isDigit( infix.charAt( infixCount ) ) ) temporary.append( infix.charAt( infixCount ) + " " ); else if ( infix.charAt( infixCount ) == '(' ) chStack.pushChar( '(' ); else if ( isOperator( infix.charAt( infixCount ) ) ) { while (isOperator( chStack.stackTop() ) && precedence( chStack.stackTop(), infix.charAt( infixCount ) ) ) temporary.append( chStack.popChar() + " " ); chStack.pushChar( infix.charAt( infixCount ) ); } else if ( infix.charAt( infixCount ) == ')' ) { while ( chStack.stackTop() != '(' ) temporary.append( chStack.popChar() + " " ); chStack.popChar(); } } return temporary; } private static boolean isOperator( char c ) { if ( c == '+' || c == '-' || c == '*' || c == '/' || c == '^' ) return true; else return false; } private static boolean precedence( char operator1, char operator2 ) { if ( operator1 == '^' ) return true; else if ( operator2 == '^' ) return false; else if ( operator1 == '*' || operator1 == '/' ) return true; else if ( operator1 == '+' || operator1 == '-' ) if ( operator2 == '*' || operator2 == '/' ) return false; else return true; return false; } } class CharacterStack extends StackComposition { public char stackTop() { char temp = popChar(); pushChar( temp ); return temp; } public char popChar() { return ( ( Character )super.pop() ).charValue(); } public void pushChar( char c ) { super.push( new Character( c ) ); } } I hope this will helps to You !