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

Write a program that takes from standard input an expression without left parent

ID: 657962 • Letter: W

Question

Write a program that takes from standard input an expression without left parentheses and prints the equivalent infix expression with the parentheses inserted.

For example, given the input

1 + 2 ) * 3- 4 ) * 5 - 6 ) ) )

your program should print

( ( 1 + 2 ) * ( (3 - 4 ) * (5 - 6 ) ) )

So far I have this completed but I'm not sure what to add to account for the missing parenthese.

import java.util.Scanner;

public class Evaluate {
public static void main(String[] args) {

Stack<String> ops = new Stack<String>();

Stack<Double> vals = new Stack<Double>();

Scanner scan = new Scanner(System.in);

while (scan.hasNext ()) {

String s = scan.next();

String op = ops.pop();

double v = vals.pop();

if (op.equals("+")) v = vals.pop() + v;
else if (op.equals("-"))   v = vals.pop() - v;
else if (op.equals("*")) v = vals.pop() * v;
else if (op.equals("/")) v = vals.pop() / v;
else if (op.equals("sqrt")) v = Math.sqrt(v);

vals.push(v);

}

else vals.push(Double.parseDouble(s));

}

System.out.println(vals.pop());

}

}

Explanation / Answer

Note: Provided code information is not sufficent, so, i provided new code as per your requirements. Revert back, if you have any doubts.

Java code:

import java.awt.*;
import javax.swing.*;

public class Evaluate
{
public static void main( String args[] )
{
StringBuffer infix = new StringBuffer(JOptionPane.showInputDialog( "Enter the infix expression: " ) );
System.out.println( "The original infix expression is: " + infix + " " );
StringBuffer result = evaluteExp( infix );
System.out.println( "The evaluate expression infix notation is: " + result + " " );
System.exit( 0 );
}

private static StringBuffer evaluteExp( StringBuffer infix )
{
CharacterStack charStack = new CharacterStack();
StringBuffer temporary = new StringBuffer( "" );
charStack.pushChar( '(' );
charStack.print();
infix.append( ')' );
for ( int infixCount = 0; !charStack.isEmpty(); ++infixCount )
{
if ( Character.isDigit( infix.charAt( infixCount ) ) )
temporary.append( infix.charAt( infixCount ) + " " );
else if ( infix.charAt( infixCount ) == '(' )
charStack.pushChar( '(' );
else if ( isOperator( infix.charAt( infixCount ) ) )
{
while ( isOperator( charStack.stackTop() ) && precedence(
harStack.stackTop(), infix.charAt( infixCount ) ) )

temporary.append( charStack.popChar() + " " );
charStack.pushChar( infix.charAt( infixCount ) );

}
else if ( infix.charAt( infixCount ) == ')' )
{
while ( charStack.stackTop() != '(' )
temporary.append( charStack.popChar() + " " );
charStack.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 ) );
}
}

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