The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 399 Weeks Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
Write a Java program to demonstrate using stacks. This project will use two stacks to evaluate infix arithmetic expressions. This will act similar to the way the Java compiler may evaluate simple expressions. The GenericStack object being created will be a generic object.
Given:
You will need for Project8.java:
public static int eval(String s)A method to completely evaluate a single expression in the form of a String. |
public static void apply(GenStack val, GenStack op)A method to apply a single binary operation. This methods takes an operator from op along with two values from val and performs the operation. The result is pushed back onto val. |
public static int prec(char op)A method to determine operator precedence. |
Take the GenStack code from Topic 8.
Some important details:
Based on the data file, your results should look something like:
45+ 312* 2-300 = 369 2 +3 * 4 = 14 (2 +3) * 4 = 20 (((3 + 2)*6) % 4 ) = 2 (( 40 + 55) / 2 ) + ( 2* ( 2 + 1)) = 53 3 - (4+5*6-77) / 4 = 13 (( ( ( ( ( ( ( ( ( 2-4)*3)/3) - 50)%3) *423)/20)-100) +564)% 4) = 3
Consider the following evaluation rules when building your program:
The rules for evaluation of expressions in d8 are as follows:
| Category | What to do. |
|---|---|
| ( | push onto operator stack |
| value | push value onto value stack |
| ) | while (top operator stack != '(') apply pop '(' from operator stack |
| operator | while (! empty operator stack && (prec(currop) |
| end of string | while (! empty operator stack) apply result remains on value stack |
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll