Write a Java program that will process several rational expr

Write a Java program that will process several \"rational expressions\": for each expression it will return the expression itself in postfix notation and the value of the expression (if the value can be calculated). Assume that the input expressions are always syntactically correct. Your program will read several expressions from live standard input, write the expressions in postfix notation to the standard output, and (if the expression contains numerical operands only) also write the value of the expression to the standard output. Entering empty line will end your program execution. For example, if the expression entered by a user is ((5*2-1/6+14/3)* (2*3-5) + 7/2 your program will first output this expression in postfix notation 5 2 * 1 - 6/14 3/+ 23 *5 - *72/+ and then the expression\'s rational value 29/3 If the expression entered by a user is (x + 12)*2 - 7 your program will output this expression in postfix notation only x 12 + 2 * 7- Input expression consists of the following kinds of tokens: operands (integers such as 5, 12, 101 or variable names such as x, a, b) operators (+, -, *, /) and brackets: (and) You can use classes Token and Tokenizer posted in CP213 code samples section to simplify your input processing. Define a precedence function P on operators such that: P(\"(\")

Solution

import java.util.*;

public category EvaluateExpressionUsingStacks
produce stacks for operators and operands */
Stack<Integer> op = new Stack<Integer>();
Stack<Double> val = new Stack<Double>();
/* produce temporary stacks for operators and operands */
Stack<Integer> optmp = new Stack<Integer>();
Stack<Double> valtmp = new Stack<Double>();
/* settle for expression */
System.out.println(\"Evaluation Of Arithmetic Expression exploitation Stacks Test\ \");
System.out.println(\"Enter expression\ \");
String input = scan.next();
input = \"0\" + input;
input = input.replaceAll(\"-\",\"+-\");
/* Store operands and operators in several stacks */
String worker = \"\";
for (int i = 0;i < input.length();i++)
{
char ch = input.charAt(i);
if (ch == \'-\')
worker = \"-\" + temp;
else if (ch != \'+\' && ch != \'*\' && ch != \'/\')
worker = worker + ch;
else
Boolean it = false;
while (!op.isEmpty())
valuate and store in temporary stack */
if (i == 0)
  
else if (i == 1)
  
else if (i == 2)
  
}
else
  
}
/* bar all components from temporary stacks to main stacks */
whereas (!valtmp.isEmpty())
val.push(valtmp.pop());
whereas (!optmp.isEmpty())
op.push(optmp.pop());
/* ingeminate once more for same operator */
if (it)
i--;
}
System.out.println(\"\ Result = \"+val.pop());
}
}

 Write a Java program that will process several \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site