Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Modify the java code: The program below does one expression and then stops. So modify the program in such a way where it should ask
Modify the java code:
The program below does one expression and then stops. So modify the program in such a way where it should ask the user if there is another expression or not.
PROGRAM:
import java.util.Stack; import java.util.Scanner; public class Stringcalculator{ public static int eval(String expression){ char[] tokens = expression.toCharArray(); //Create Stack for integers StackA = new Stack (); // Create Stack for Operators (Characters) Stack B = new Stack (); //Tokenizing the string for (int i = 0; i < tokens.length; i++){ //If the current token is a whitespace, ignore it if (tokens[i] == ' ') continue; //If the current token is a number, then push it to the A stack if (tokens[i] >= '0' && tokens[i] <= '9'){ StringBuffer buffer = new StringBuffer(); //There can be more than one digits in a number so getting each number properly while (i < tokens.length && tokens[i] >= '0' && tokens[i] <= '9') buffer.append(tokens[i++]); A.push(Integer.parseInt(buffer.toString())); // Currently "i" points to the character next to the digit, // As the for loop increments the i, we will have to skip one token position; // Hence we need to decrease the value of i by 1 to get the correct offset. i--; } // If the present token is an opening brace, push it to B stack else if (tokens[i] == '(') B.push(tokens[i]); // If the present token is a closing brace, we have to solve the entire brace else if (tokens[i] == ')'){ while (B.peek() != '(') A.push(applyOperator(B.pop(),A.pop(),A.pop())); B.pop(); } else if (tokens[i] == '+' || tokens[i] == '-' || tokens[i] == '*' || tokens[i] == '/'){ // If the present token is an operator do the following:- while (!B.empty() && hasPrecedence(tokens[i], B.peek())) A.push(applyOperator(B.pop(),A.pop(),A.pop())); //Push present token to the B stack B.push(tokens[i]); } } //While we are at this point of the code, the entire expression has been parsed, //We have to apply remaining B to the remaining A while (!B.empty()) A.push(applyOperator(B.pop(),A.pop(),A.pop())); return A.pop(); } //This is a boolean function which returns true if 2nd operator has higher precendence than first operator, else return false public static boolean hasPrecedence(char operator1, char operator2){ if (operator2 == '(' || operator2 == ')') return false; if ((operator1 == '*' || operator1 == '/') && (operator2 == '+' || operator2 == '-')) return false; else return true; } //This is a method which applies an operator to an operand public static int applyOperator(char operator, int b, int a){ switch (operator) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': if (b == 0) throw new UnsupportedOperationException("Cannot divide by zero"); return a / b; } return 0; } // Main Method public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the string to evaluate : "); String str=sc.next(); System.out.println(eval(str)); sc.close(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started