question archive Design a program with the following specifications: Required Program Name: RPN
Subject:Computer SciencePrice:6.86 Bought12
Design a program with the following specifications:
Program Implementation
Code:
import java.io.File; import static java.lang.Character.isDigit; import java.util.*; import java.util.regex.*; public class RPN { /* This method is for taking the input from the user, Here we will take the two type of input from the user, based on the choice. One will be through CLI and another will be from the file. */ public static String inputexpr(String expr) { Scanner sc = new Scanner(System.in); System.out.println("Enter 1 for input the expression"); System.out.println("Enter 2 to enter expression from the file"); int choice = sc.nextInt();sc.nextLine(); if(choice == 1) // If the user selects 1 { System.out.println("Enter the expression: "); expr = sc.nextLine(); //expression is stored here. } else if(choice == 2) { try{ File file = new File("C:\\Users\\asus\\Desktop\\expr.txt"); //location of the file Scanner files = new Scanner(file); expr = files.nextLine(); //expression is stored here. System.out.println(expr); } catch(Exception e) { System.out.println(e); } } return expr; } /* This function will take the expression from the above and then will note the counter according to the syntax. For example: if the syntax contains any symbol like +,-,*,/,^ then decrease the counter, otherwise if there is a character then increase the counter. */ public static boolean verifyexpr(String expr) { boolean b = true; int count=0; int len=expr.length(); int i=0; while(i<len) { if(isDigit(expr.charAt(i))) { count++; } else if(expr.charAt(i)=='+' ||expr.charAt(i)=='-'||expr.charAt(i)=='*'||expr.charAt(i)=='/'||expr.charAt(i)=='^') { count--; } else if(expr.charAt(i)==' '){} else { b = false; break; } i++; } if(count!=1) b = false; return b; } public static int evaluateexpr(String expr) { Stack<Integer> stack = new Stack<Integer>(); //define the stack "stack" int i=0; int src, src1, src2; int len = expr.length(); while(i<len) { if(isDigit(expr.charAt(i))) { //if the character is a digit then convert into integer and push onto stack src = Integer.parseInt(expr.charAt(i)+""); stack.push(src); } else { //if token is an operator then pop two operands from stack and perform operations. src2 = stack.pop(); src1 = stack.pop(); char ch = expr.charAt(i); if(ch == '+') { stack.push(src1 + src2); } else if(ch == '-') { stack.push(src1 - src2); } else if(ch == '*') { stack.push(src1 * src2); } else if(ch == '/') { stack.push(src1/src2); } else if(ch=='^') { stack.push((int)Math.pow(src1*1.0, src2*1.0)); } } i++; } return stack.pop(); } //Main function public static void main(String[] args) { Scanner sc = new Scanner(System.in); String expr = null; //Go to the inputexpr() method. expr = inputexpr(expr); //verify input expression boolean check = verifyexpr(expr); if(check) { int eval = evaluateexpr(expr); System.out.println("eval = "+eval); } else { System.out.println("Invalid Expression!!"); } } }
This program is done in VSCode.
Output(1):
Output(2):
Output(3):
Please see the attached file for the complete solution