Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

This program must use stacks in order to 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.

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M91008131

Have any Question?


Related Questions in Computer Engineering

Answer as thorough as possiblenbspinclude an explanation of

Answer as thorough as possible, Include an explanation of your recommendations or trading strategies. Should you early exercise the following American-style put option? If not always, under what situation would you early ...

The single-cycle design on p 17 of the notes is capable of

The single-cycle design on p. 17 of the notes is capable of performing more instructions than the subset that were discussed in §4.1 of the lecture notes. Open the MIPS Architecture Volume II-A: The MIPS32 Instruction Se ...

Imagine a small city chambana with a labor demand function

Imagine a small city Chambana with a labor demand function E=200-1/2w and a labor supply function E=2w-100. Assume that there is a sudden influx of 40 perfect substitutes immigrants. a. How does this affect the natives i ...

What are the best practices to follow for microsoft windows

What are the best practices to follow for Microsoft Windows network security. Which two would you start with and why?

Suppose mpc is 07 government spending increases by 10

Suppose MPC is 0.7. Government spending increases by $10 billion, and taxes decrease by $5 billion. How would the following items change, and by how much? Disposable income Private saving Public( Government) saving Equil ...

Reminder all files must be closed when you are done with

Reminder: All files must be closed when you are done with them, even if it stops early due to an IOError. If you're using with, this will happen automatically. If you're trying to close things manually using .close(), th ...

Question suppose you have a table employee with the

Question : Suppose you have a table, EMPLOYEE, with the following attributes: eid, work_title, fname, lname, ssn, salary, date_of_birth, and commission_rate. Ms. Smith is vice president of sales. She and three regional s ...

What are some topics that must be covered in a business

What are some topics that must be covered in a business case presented to management?

What is the purpose of exclusive gates such as the xor and

What is the purpose of exclusive gates such as the XOR and XNOR? What function do these gates perform?

Remember all of the following steps when showing that a

Remember all of the following steps when showing that a problem D is NPcomplete: 1. Show that D is in NP by briefly explaining how to quickly verify a solution to it. 2. Choose another problem Q that is known to be NP-ha ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As