Homework 2: Dijkstra's Two-Stack Algorithm ASSIGNMENT: In 1960, Edsger W. Dijkstra came up with a very...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Homework 2: Dijkstra's Two-Stack Algorithm ASSIGNMENT: In 1960, Edsger W. Dijkstra came up with a very simple algorithm to evaluate arithmetic expressions with the help of two stacks. The first stack is called the "value (operand) stack" and it is used to store numbers (operands). The second stack is the "operator stack", used to store arithmetical operations (operators). In order to use the algorithm, all individual expressions must be enclosed in parentheses. The algorithm works in the following way: 1. take an arithmetic expression, with properly balanced parentheses a. example: (( 5+ (3*8)) - (2* 7)) 2. read the expression character by character 3. if number, push it onto the value stack 4. if operator, push it onto the operator stack 5. ignore left parentheses 6. when right parentheses are encountered: a. pop an operator and two values (from their respective stacks) b. perform an operation with those values (e.g. 3 * 8) c. push the result onto the value stack In this homework assignment, your task will be to implement Dijkstra's Two-Stack Algorithm for simple arithmetic operations (addition, subtraction, multiplication, division, modulo), using one of the stack implementations we studied (you can use either the linked-list stack or the resizing-array stack; do not use the built-in Java stack). The figure below describes the two-stack algorithm once again (left), and offers a simple example (right). Goal. Evaluate infix expressions. (1 + ((2+3) * (4.5))) operand operator Context. An interpreter! value stack operator stack Two-stack algorithm. [E. W. Dijkstra] Value: push onto the value stack. Operator: push onto the operator stack. Left parenthesis: ignore. Right parenthesis: pop operator and two values; push the result of applying that operator to those values onto the operand stack. mmmmm FEE 100 101 (1+((2+3)+(4+5))) +((2+3)*(4*5))) ((2+3)+(4+5))) +3)*(4+5))) 3)*(4.5))) >*(4-5))) (4.5))) (4-5))) *5))) 5))) ))) >> The assignment will be open on LMS until March 24th, 23:55. You are going to export your projects as archive files and submit them to LMS. Hint: In order to simplify your implementation of the algorithm, assume that each token (parenthesis, number or operator) is separated by a whitespace. Example: (2+3) invalid, but (2+3) valid Moreover, make sure that each expression is entered within parentheses and that the parentheses are perfectly balanced. example: (3+4)* 5 invalid, but ( ( 3+4)*5) valid It should be possible for the user to manually enter the expression they want into the program, via console input (using the Scanner input). You can use the two expressions given above (in the algorithm explanation and in the figure) to test whether your implementation is working correctly. Good luck. Homework 2: Dijkstra's Two-Stack Algorithm ASSIGNMENT: In 1960, Edsger W. Dijkstra came up with a very simple algorithm to evaluate arithmetic expressions with the help of two stacks. The first stack is called the "value (operand) stack" and it is used to store numbers (operands). The second stack is the "operator stack", used to store arithmetical operations (operators). In order to use the algorithm, all individual expressions must be enclosed in parentheses. The algorithm works in the following way: 1. take an arithmetic expression, with properly balanced parentheses a. example: (( 5+ (3*8)) - (2* 7)) 2. read the expression character by character 3. if number, push it onto the value stack 4. if operator, push it onto the operator stack 5. ignore left parentheses 6. when right parentheses are encountered: a. pop an operator and two values (from their respective stacks) b. perform an operation with those values (e.g. 3 * 8) c. push the result onto the value stack In this homework assignment, your task will be to implement Dijkstra's Two-Stack Algorithm for simple arithmetic operations (addition, subtraction, multiplication, division, modulo), using one of the stack implementations we studied (you can use either the linked-list stack or the resizing-array stack; do not use the built-in Java stack). The figure below describes the two-stack algorithm once again (left), and offers a simple example (right). Goal. Evaluate infix expressions. (1 + ((2+3) * (4.5))) operand operator Context. An interpreter! value stack operator stack Two-stack algorithm. [E. W. Dijkstra] Value: push onto the value stack. Operator: push onto the operator stack. Left parenthesis: ignore. Right parenthesis: pop operator and two values; push the result of applying that operator to those values onto the operand stack. mmmmm FEE 100 101 (1+((2+3)+(4+5))) +((2+3)*(4*5))) ((2+3)+(4+5))) +3)*(4+5))) 3)*(4.5))) >*(4-5))) (4.5))) (4-5))) *5))) 5))) ))) >> Homework 2: Dijkstra's Two-Stack Algorithm ASSIGNMENT: In 1960, Edsger W. Dijkstra came up with a very simple algorithm to evaluate arithmetic expressions with the help of two stacks. The first stack is called the "value (operand) stack" and it is used to store numbers (operands). The second stack is the "operator stack", used to store arithmetical operations (operators). In order to use the algorithm, all individual expressions must be enclosed in parentheses. The algorithm works in the following way: 1. take an arithmetic expression, with properly balanced parentheses a. example: (( 5+ (3*8)) - (2* 7)) 2. read the expression character by character 3. if number, push it onto the value stack 4. if operator, push it onto the operator stack 5. ignore left parentheses 6. when right parentheses are encountered: a. pop an operator and two values (from their respective stacks) b. perform an operation with those values (e.g. 3 * 8) c. push the result onto the value stack In this homework assignment, your task will be to implement Dijkstra's Two-Stack Algorithm for simple arithmetic operations (addition, subtraction, multiplication, division, modulo), using one of the stack implementations we studied (you can use either the linked-list stack or the resizing-array stack; do not use the built-in Java stack). The figure below describes the two-stack algorithm once again (left), and offers a simple example (right). Goal. Evaluate infix expressions. (1 + ((2+3) * (4.5))) operand operator Context. An interpreter! value stack operator stack Two-stack algorithm. [E. W. Dijkstra] Value: push onto the value stack. Operator: push onto the operator stack. Left parenthesis: ignore. Right parenthesis: pop operator and two values; push the result of applying that operator to those values onto the operand stack. mmmmm FEE 100 101 (1+((2+3)+(4+5))) +((2+3)*(4*5))) ((2+3)+(4+5))) +3)*(4+5))) 3)*(4.5))) >*(4-5))) (4.5))) (4-5))) *5))) 5))) ))) >> The assignment will be open on LMS until March 24th, 23:55. You are going to export your projects as archive files and submit them to LMS. Hint: In order to simplify your implementation of the algorithm, assume that each token (parenthesis, number or operator) is separated by a whitespace. Example: (2+3) invalid, but (2+3) valid Moreover, make sure that each expression is entered within parentheses and that the parentheses are perfectly balanced. example: (3+4)* 5 invalid, but ( ( 3+4)*5) valid It should be possible for the user to manually enter the expression they want into the program, via console input (using the Scanner input). You can use the two expressions given above (in the algorithm explanation and in the figure) to test whether your implementation is working correctly. Good luck. The assignment will be open on LMS until March 24th, 23:55. You are going to export your projects as archive files and submit them to LMS. Hint: In order to simplify your implementation of the algorithm, assume that each token (parenthesis, number or operator) is separated by a whitespace. Example: (2+3) invalid, but (2+3) valid Moreover, make sure that each expression is entered within parentheses and that the parentheses are perfectly balanced. example: (3+4)* 5 invalid, but ( ( 3+4)*5) valid It should be possible for the user to manually enter the expression they want into the program, via console input (using the Scanner input). You can use the two expressions given above (in the algorithm explanation and in the figure) to test whether your implementation is working correctly. Good luck.
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
Please help with the discusin questions ! I give thumbs up Case #1: Hailing a New Era: Haier in Japan As one of the most valuable brands in China, Haier designs,manufactures, and sells various home...
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
Joe rents his condo for $1,500 per month. Total rental and personal use days for the current year was 210 days and 20 days, respectively. What are the tax consequences for Joe?
-
According to News Wire "Inequality," what is the average per capita income in nations where the highest income decile gets (a) Over 45 percent of total income? (b) Less than 30 percent of total...
-
Consider a naive Bayes classifier with two features, shown below. We have prior information that the probability model can be parameterized by and p, as shown below: We have a training set that...
-
Christopher Boling was seriously injured in 2008 when vapors escaping from a gas can ignited. He filed a products liability claim against the manufacturer. To fund the litigation, Boling entered into...
-
Aunt Mollys Old Fashioned Cookies bakes cookies for retail stores. The companys best-selling cookie is chocolate nut supreme, which is marketed as a gourmet cookie and regularly sells for $8.00 per...
-
The December 3 1 , 2 0 2 4 , adjusted trial balance for the Blueboy Cheese Corporation is presented below. Account Title Debits Credits Cash $ 2 1 , 0 0 0 Accounts receivable 3 0 0 , 0 0 0 Prepaid...
-
1. A friend has offered to play a gambling game with you that involves flipping a coin that be has provided. Since a flip of heads will be to his advantage, you want to test the coin for fairness...
-
Entering into a collaborative ACO is challenging because of possible Anti-Kickback Statute and the Stark Law violations. What are the main conflicts? What has CMS done to minimize legal risks?...
-
Cylinder in magnetic fields (8pts) A uniform magnetic field of magnitude Bo points in the positive z- direction. An infinitely long solid cylinder runs parallel to and is centered on the z-axis. The...
-
Discuss how does ARP spoofing work? What is port mirroring and why is it used?
-
A project requires an initial investment of $2,400,000 depreciated straight-line to $0 in 10 years. The investment is expected to generate annual sales of $700,000 with annual costs of $450,000 for...
-
n preparation for Thanksgiving Day, the Save-You-More Store has stacked cans of cherry pie filling in a triangular pyramid. The top of the pyramid has a single can, the second row has three cans, and...
-
Discuss the application of the Divide-and-Conquer approach in computer science?
-
Find the curvature of the curve T(4) = 3Sing + 3Cosw + 3Cotwk
-
Why is it necessary to study the diffusion of molecules in biological systems?
-
Consider the two-period com economy of the text. Suppose that initially the equilibrium has a zero trade deficit. Now, increase the interest rate. Show how the Fisher diagram changes. Will trade...
-
Now, suppose that U.S. policymakers do not actually care about pollution, but merely about real purchasing power of U.S. incomes. What is the effect of an increase in on the terms of trade, and on...
-
From the 'trade flows data.xis' spreadsheet, identify a country with a trade deficit (other than the United States) and another with a trade surplus. Comment on what may be driving this deficit or...
-
a. The equation of the Phillips curve from 1970 to 1995 is: \[\pi_{t}-\pi_{t-1}=7.4 \%-1.2 u_{t}\] Calculate and define the natural rate of unemployment using this curve. b. The equation of the...
-
Suppose that the mark-up of goods prices over marginal cost is \(5 \%\), and that the wage-setting equation is: \[W=P(1-u)\] where \(u\) is the unemployment rate. a. What is the real wage, as...
-
How can a lockdown result in stagflation? A lockdown can have three different effects: a. The first happens when demand during the lockdown decreases so that actual output, \(Y_{t}\), falls exactly...
Study smarter with the SolutionInn App