Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

leverage ANTLR to implement a reverse polish calculator. Write code in Java ADD MORE FUNC TO THE CODE BELOW TO HANDLE: * * ( power

leverage ANTLR to implement a reverse polish calculator. Write code in Java
ADD MORE FUNC TO THE CODE BELOW TO HANDLE:
**(power, instead of ^), log, ln,sin, cos, tan, asin, acos, and atan, sinh, cosh, tanh,!, e, pi
CODE
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
String infix = scanner.nextLine();
String rpn = infixToRPN(infix);
double result = evaluateRPN(rpn);
System.out.println(infix);
System.out.println(rpn);
System.out.printf("%.5f%n", result);
}
scanner.close(); // Closing the scanner to avoid resource leak
}
// Method to convert infix expression to Reverse Polish Notation (RPN)
private static String infixToRPN(String infix){
StringBuilder result = new StringBuilder();
Stack stack = new Stack<>();
for (char c : infix.toCharArray()){
if (Character.isDigit(c)|| c =='.'){
result.append(c); // Append operand directly to result
} else if (c =='('){
stack.push(c); // Push '(' onto the stack
} else if (c ==')'){
while (!stack.isEmpty() && stack.peek()!='('){
result.append('').append(stack.pop()); // Append operators to result until '(' is encountered
}
stack.pop(); // Discard '('
} else {
while (!stack.isEmpty() && precedence(c)<= precedence(stack.peek())){
result.append('').append(stack.pop()); // Append operators with higher or equal precedence to result
}
stack.push(c); // Push current operator onto the stack
result.append(''); // Append space after operator
}
}
while (!stack.isEmpty()){
result.append('').append(stack.pop()); // Append remaining operators to result
}
return result.toString().trim(); // Return trimmed RPN expression
}
// Method to determine precedence of operators
private static int precedence(char operator){
switch (operator){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
// Method to evaluate RPN expression
private static double evaluateRPN(String rpn){
Stack stack = new Stack<>();
for (String token : rpn.split("\\s+")){
if (token.matches("[+-]?\\d+(\\.\\d+)?")){
stack.push(Double.parseDouble(token)); // Push operands onto stack
} else {
double b = stack.pop(); // Pop top two operands from stack
double a = stack.pop();
switch (token){
case "+":
stack.push(a + b); // Perform addition
break;
case "-":
stack.push(a - b); // Perform subtraction
break;
case "*":
stack.push(a * b); // Perform multiplication
break;
case "/":
if (b ==0){
throw new ArithmeticException("Division by zero"); // Handle division by zero
}
stack.push(a / b); // Perform division
break;
default:
throw new IllegalArgumentException("Invalid operator: "+ token); // Handle invalid operator
}
}
}
return stack.pop(); // Return final result
}
}
OUTPUInput expression Reverse Polish expression Computation result
(1+2)!-:(3-4**2)12+!342**--: -0.46154
sin (\pi -:6)\pi 6-:sin 0.5
ln(e**3) e 3**ln 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Autonomous Database In Enterprise Architecture

Authors: Bal Mukund Sharma, Krishnakumar KM, Rashmi Panda

1st Edition

1801072248, 978-1801072243

More Books

Students also viewed these Databases questions

Question

Customers must be external to the organization. True or false?

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago