Evaluate integer expressions

Assignment Help Basic Computer Science
Reference no: EM13551406

Evaluate integer expressions. Your program need only implement the following binary operators:

+ ... addition
- ... subtraction
* ... multiplication
/ ... integer division
< ... minimum (e.g. 3 < 2 yields 2)
> ... maximum (e.g. 3 > 2 yields 3)

To keep the assignment somewhat more manageable you may assume that all input integers are unsigned single decimal digits (0-9). However, your evaluation may result in larger and/or negative integers.

The evaluation of expressions must recognize the standard precedence:
< and > have the highest precedence
* and / have the middle precedence
+ and - have lowest precedence

Expressions must also permit the usual use of parentheses (...) to override precedence. Below are several examples of proper expressions:

1+2*3>4

(24-7)/2+6<3*(31-299)

2*3*4*5*6

10>9*8+7+2

Your program must read input expressions from a text file and allow the user to select which text file to process using a JFileChooser. You may assume that each line of the text file is a separate integer expression and that there are no blanks or other invalid characters in the file.

For each input expression your program should output (using System.out.println) the input line followed by "==" then followed by the numeric value of the resulting expressions evaluation.

You must implement a generic Stack class like the one given in lecture.

The standard algorithm for evaluating expressions of this style is to use two stacks - one stack for operators (the operator stack) and the other for operands/subexpression values (the operand stack). An expression is processed left to right as follows:

• Each operand encountered is pushed upon the operand stack.

• Each left paren encountered is pushed upon the operator stack.

• Each right paren causes operators to be popped from the operator stack and the associated subexpressions evaluated until a left paren is found in the operator stack. The left paren is then popped.

• Each operator has its precedence compared to the precedence of the operator atop the operator stack. Operators are popped off the operator stack and their corresponding subexpressions evaluated until the top of the operator stack is either a left paren or has a precedence less than the new operator. After evaluating subexpressions, the new operator is pushed onto the operator stack.

• At the end of the expression all operators on the operator stack must be popped and their corresponding subexpressions evaluated. The value of the expression should be the single remaining value atop the operand stack.

When the prior explanations refer to "popping an operator and evaluating the corresponding subexpression" this means that the operand stack should be popped twice, and the two popped operands are evaluated using the popped operator with the resulting value pushed on the operand stack. For example, if the top of the operand stack is a 7 and the second operand in the operand stack is a 3 and the operator is a "-", then all three of these are popped and the result of the subexpression 3-7 (i.e., the value -4) is pushed on the operand stack.

Reference no: EM13551406

Questions Cloud

Campbell corporation uses baumol model to manage cash : Campbell Corporation uses Baumol model to manage cash.
What is the angle between the axes of the polarizers : Unpolarized light with an intensity of585W / m2 is incident on a polarizer with an unknown axis. The light then passes through a second polarizer with has an axis which makes an angle of67.0° with the vertical.
Explain the thermite reaction and the calculated : The thermite reaction combines aluminum powder and iron oxide and ignites the mixture to make aluminum oxide and iron. Use the heat capacities of the products and reactants of the thermite reaction and the calculated
The payment structure of a corporate bond : The payment structure of a corporate bond is best thought
Evaluate integer expressions : Evaluate integer expressions - evaluation of expressions must recognize the standard precedence
What are the wavelength of the second overtone : A 2.00 m long rope is stretched between two supports with a tension that makes the speed of transverse waves 48.0 m/s. What are the wavelength of the second overtone
Find out the entropy of mixing for 1 mole of such a mixture : Dentists might use a mixture of 40% N2O and 60% O2 as an initial anesthetic of nitrous oxide(although exact proprtions may vary). Determine the entropy of mixing for 1 mole of such a mixture. Assume ideal gas conditions.
What light intensity emerges from the third filter : Unpolarized light of intensityI0is incident on three polarizing filters. The axis of the first is vertical, that of the second is29° from vertical, and that of the third is horizontal. What light intensity emerges from the third filter?
What is the common-size percentage for inventory account : Tom's Hardware has inventory of $318,000, equity of $421,800, total assets of $647,700, and sales of $687,400. What is the common-size percentage for the inventory account?

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Design static method below to find and return complex number

Given a "Complex" class with all the standard methods for complex numbers, design the static method below to find and return the smallest complex number.

  Application letter and resume

In this project, you will work with a fax cover sheet, an application letter, and a resume. To complete the project you will create a table, add text to a table, and format tables.

  Jane''s farm stand sells a variety of vegetables

Jane's farm stand sells a variety of vegetables. Write a program that produces an output file of vegetables sold, sorted by the biggest selling vegetable in terms of money made to the poorest selling.

  Why ratings for high quality pages must all get same rating

Ratings for high quality pages should all get the same rating. For example, a high quality page for a common interpretation of the query should get the same rating as a high quality page for a minor interpretation of the query.

  Explain in detail what the switches know

Do switches know the entire data link path between the sending and receiving host, explain in detail what the switches know?

  Displays the amount of pay for the pay period

1. Non-numeric values should not be accepted. 2. Negative values should not be accepted. 3. The minimum number of days for the pay period is 19 days for the new employees and 16 days for the experienced employees. The maximum number of days in a p..

  Simplify the following boolean expressions

Simplify the following Boolean expressions. a) xy + (x + y)z + y, b) x + y + (x + y + z), c) yz + wx + z + [wz(xy + wz)]

  Change arp entry in computer for mac address

Print your ARP cache table. Find a server on your local network. Change its ARP entry in your computer to point to your computer's MAC address. Print new ARP cache table. Now use the service and see what happens.

  Capacity as a systems analyst

In your capacity as a systems analyst, you may face situations in which you encounter an ethical dilemma. For example, you may be asked to design a system that supports an activity with which you personally disagree

  Write a program calculates and displays the pay to workers

Create 3 workers and initialize their member variables with any data you choose. Store the workers in a Linked List. Write a function that will traverse the list of workers calculating and displaying the amount of pay each worker has earned.

  Write an app that uses a foreach statement to sum the values

Write an app that uses a foreach statement to sum the values passed by the command line argument. Hint: Use static method ToDouble of class Convert to covert a string to a double value.

  What power of h is the error proportional to

Find the values of A and B such that error is minimized. What power of h is the error proportional to? Use this formula to find d/dx sin(x) at x = pi/3. Tabulate errors for different values of h.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd