Change this code to where the user can input in infix mode and with no spaces fo
ID: 3553073 • Letter: C
Question
Change this code to where the user can input in infix mode and with no spaces for instance
5+3 instead of + 5 3 which is how it is now
import java.util.Scanner;
public class test
{
private static class TreeNode
{
private final boolean leaf; // ?Is this a leaf? else internal
private final char op; // For an internal node, the operator
private double value; // For a leaf, the value
private TreeNode left, // Left subexpression for an internal node
right; // Right subexpression
// Bare-bones constructor
private TreeNode ( boolean leaf, char op, double value )
{
this.leaf = leaf;
this.op = op;
this.value = value;
this.left = null; // Empty to start
this.right = null;
}
// For leaf nodes, show the value; for internal, the operator.
public String toString() // To override Object.toString, must be public.
{ return leaf ? Double.toString(value) : Character.toString(op) ; }
}
TreeNode root = null;
public test ( Scanner input )
{ root = build(input); }
private TreeNode build ( Scanner input )
{
boolean leaf;
String token;
double value;
TreeNode node;
leaf = input.hasNextDouble();
if ( leaf )
{
value = input.nextDouble();
node = new TreeNode ( leaf, '', value );
}
else
{
token = input.next();
node = new TreeNode ( leaf, token.charAt(0), 0.0 );
node.left = build ( input );
node.right = build ( input );
}
return node;
}
/**
* Show the expression tree as a parenthesized infix expression.
* All the work is done in the private recursive method.
*/
public void showInFix ()
{
showInFix ( root );
System.out.println();
}
// Parenthesized infix requires parentheses in both the
// pre-order and post-order positions, plus the node
// itself in the in-order position.
private void showInFix ( TreeNode node )
{
if ( node != null )
{
// Note: do NOT parenthesize leaf nodes
if ( ! node.leaf )
System.out.print ("( "); // Pre-order position
showInFix ( node.left );
System.out.print ( node + " " ); // In-order position
showInFix ( node.right );
if ( ! node.leaf ) // Post-order position
System.out.print (") ");
}
}
/**
* Evaluate the expression and return its value.
* All the work is done in the private recursive method.
* @return the value of the expression tree.
*/
public double evaluate ()
{
//return root == null ? 0.0 : evaluate ( root ) ;
return evaluate(root);
}
// Evaluate the expression: for internal nodes, this amounts
// to a post-order traversal, in which the processing is doing
// the actual arithmetic. For leaf nodes, it is simply the
// value of the node.
private double evaluate ( TreeNode node )
{
double result; // Value to be returned
if ( node.leaf ) // Just get the value of the leaf
result = node.value;
else
{
// We've got work to do, evaluating the expression
double left, right;
char operator = node.op;
// Capture the values of the left and right subexpressions
left = evaluate ( node.left );
right = evaluate ( node.right );
// Do the arithmetic, based on the operator
switch ( operator )
{
case '-': result = left - right; break;
case '*': result = left * right; break;
case '/': result = left / right; break;
case '^': result = Math.pow (left, right ); break;
// NOTE: allow fall-through from default to case '+'
default: System.out.println ("Unrecognized operator " +
operator + " treated as +.");
case '+': result = left + right;
break;
}
}
// Return either the leaf's value or the one we just calculated.
return result;
}
}
import java.util.Scanner;
public class calc
{
public static void main ( String[] args )
{
test calc;
Scanner console = new Scanner(System.in);
// Allow for a command-line argument (which would be double-quoted).
if ( args.length > 0 )
{
System.out.println ("Processing string " + args[0]);
calc = new test(new Scanner(args[0]));
}
else
{
System.out.println
( "Prefix expression, with all elements separated by blanks");
calc = new test(new Scanner(console.nextLine()));
}
System.out.println (" Input as parenthesized infix expression:");
calc.showInFix();
System.out.println (" Value: " + calc.evaluate());
System.out.print ("Press <Enter> to exit: ");
console.nextLine();
}
}
Explanation / Answer
Find help at http://academicservices.us/ if stuck or confused
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.