Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package sa.edu.yuc; import java.util.Scanner; import java.util.Stack; public class Evaluate { public static void main(String[] args) { Stack operandStack = new Stack (); Stack operatorStack =

package sa.edu.yuc;

import java.util.Scanner; import java.util.Stack;

public class Evaluate {

public static void main(String[] args) { Stack operandStack = new Stack(); Stack operatorStack = new Stack(); operatorStack.push('#'); Scanner io = new Scanner(System.in); System.out.print("Enter an expression: "); String input = io.next(); int len = input.length(); for (int i = 0; i < len; i++) { //System.out.print(input.charAt(i)); char symbol = input.charAt(i); if (!isOperator(symbol)) { int n = Integer.parseInt(""+symbol); operandStack.push(n); } else { char poppedOperator = operatorStack.pop(); if (prec(symbol) > prec(poppedOperator)) { operatorStack.push(poppedOperator); operatorStack.push(symbol); } else { int b = operandStack.pop(); int a = operandStack.pop(); if (poppedOperator == '*') { operandStack.push(a * b); } else if (poppedOperator == '/') { operandStack.push(a / b); } else if (poppedOperator == '+') { operandStack.push(a + b); } else if (poppedOperator == '-') { operandStack.push(a - b); } else { //we will deal with it in future } operatorStack.push(symbol); } } } //end of for loop char poppedOperator = operatorStack.pop(); while (poppedOperator != '#') { int b = operandStack.pop(); int a = operandStack.pop(); if (poppedOperator == '*') { operandStack.push(a * b); } else if (poppedOperator == '/') { operandStack.push(a / b); } else if (poppedOperator == '+') { operandStack.push(a + b); } else if (poppedOperator == '-') { operandStack.push(a - b); } else { //we will deal with it in future } poppedOperator = operatorStack.pop(); } System.out.println("Result = " + operandStack.pop()); } private static int prec(char ch) { if (ch == '*' || ch == '/') return 10; else if (ch == '+' || ch == '-') return 5; else if (ch == '#') return 1; else return 0; } private static boolean isOperator(char ch) { if (ch == '+' || ch == '-' || ch == '*' || ch == '/') return true; return false; }

}

--------------------------------------------------------------------------------------------------

Questions1:

3*4-2

It can only handle single digit operands and no spaces are allowed within the expression. The operators are limited to +, - * and /.

In this Exercise, we want you to extend the given program in such a manner that it could handle decimal numbers, Or Reminder (%) e.g.

2.5 * 4.0 1.5 * 2.0

OR

5 - 2 * 4 % 16

*just apdate on this code*

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

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions

Question

How does a MDI help Dell forecast its sales of personal computers?

Answered: 1 week ago

Question

=+2. How well does the collaborator address the larger issues?

Answered: 1 week ago