Write a program that evaluates an arithmetic expression in infix notation, without full parentheses. Use the following
Question:
Write a program that evaluates an arithmetic expression in infix notation, without full parentheses. Use the following algorithm: There are two stacks: a numbers stack and an operators stack. When a number appears, push it onto the numbers stack. Any parenthesis or operator should be treated as described in Step 2 of Figure 6.10 on page 351—with one change. Whenever you pop an operation off the stack, you should immediately use that operation by popping two numbers, applying the operation, and pushing the answer back on the numbers stack.
Transcribed Image Text:
FIGURE 6.10 Converting an Infix Expression to a Postfix Expression (General Case) Pseudocode 1. Initialize a stack of characters to hold the operation symbols and parentheses. 2. do if (the next input is a left parenthesis) Read the left parenthesis and push it onto the stack. else if (the next input is a number or other operand) Read the operand and write it to the output. else if (the next input is one of the operation symbols) { Pop and print operations off the stack until one of three things occurs: (1) The stack becomes empty, (2) the next symbol on the stack is a left parenthesis, or (3) the next symbol on the stack is an operation with lower precedence than the next input symbol. When one of these situations occurs, stop popping, read the next input symbol, and push this symbol onto the stack. } else { Read and discard the next input symbol (which should be a right parenthesis). Pop and print operations off the stack until the next symbol on the stack is a left parenthesis. (If no left parenthesis is encountered, then print an error message indicating unbalanced parentheses and halt.) Finally, pop and discard the left parenthesis. } while (there is more of the expression to read); 3. Pop and print any remaining operations on the stack. (There should be no remaining left parentheses; if there are, the input expression did not have balanced parentheses.)
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 75% (8 reviews)
Here is a sample implementation in Python def evaluateexpressionexpression Define two stacks for num...View the full answer
Answered By
Akshay Shete
I have extensive experience as a tutor, both online and in-person. I have worked with students of all ages and abilities, and am skilled at adapting my teaching style to meet the needs of each individual student. I have a strong background in a variety of subjects, including math, science, and English, and am able to break down complex concepts in a way that is easy for students to understand. In addition to my subject matter expertise, I am also a patient and supportive teacher, and am committed to helping my students succeed. Whether I am working with a struggling student who needs extra help to catch up, or an advanced student looking to get ahead, I am able to provide the guidance and support they need to reach their goals. Overall, my hands-on experience as a tutor has prepared me to be a confident and effective teacher, and I am excited to use my skills to help students succeed.
0.00
0 Reviews
10+ Question Solved
Related Book For
Question Posted:
Students also viewed these Computer science questions
-
Write a program that evaluates an arithmetic expression in postfix notation. The basic algorithm is contained in Evaluating Postfix Expressions on page 345. Assume the input contains numbers (but no...
-
A labeled tree is one wherein the vertices are labeled. If the tree has n vertices, then {1, 2, 3,..., n} is used as the set of labels. We find that two trees that are isomorphic without labels may...
-
In this assignment you will write a program that will simulate a storage area with collections of items. Items can be added, removed, and the user can check the value of an item. This assignment does...
-
You are an information technology (IT) intern working for Health Network, Inc. (Health Network), a fictitious health services organization headquartered in Minneapolis, Minnesota. Health Network has...
-
If you must purchase a replacement battery for your car, would you prefer one that comes from a population with a - 1 month, for time to failure, or one that comes from a population with a = 1 year?...
-
A Rydberg hydrogen atom makes a downward transition to the n = 225 state, emitting a 9.32eV photon. What was the original state?
-
= 4848, = 24, n = 1200 A population has a mean and a standard deviation . Find the mean and standard deviation of the sampling distribution of sample means with sample size n.
-
Ace Maintenance, Inc., repairs heavy construction equipment and vehicles. Recently, the Shanti Construction Company had one of its giant earthmovers overhauled and its tires replaced. Repair work for...
-
Provide documentation of a step by step training plan for a beta fish. What will be the primary reinforcer? How will you cue the behavior? What schedule of reminforcement will you use. What is the...
-
Pin B has a mass m and slides along the slot in the rotating arm OC and along the slot DE which is cut in a fixed horizontal plate. Neglecting friction and knowing that rod OC rotates at the constant...
-
Redo the calculator program given in Figure 6.5 on page 330, but this time implement it in a different way. To evaluate the arithmetic expression, your program will first convert the expression to...
-
Suppose that you have n queens from a chess game, and that you also have an n-by-n chess board. Is it possible to place all n queen on the board so that no two queens are in the same row, no two...
-
Discuss the benefits of direct marketing to both buyers and sellers.
-
There are numerous pros and cons to using newspaper advertising. Define and explain each.
-
What are the implications of failing to consider the ecological model when shaping interventions health communicators consider and choose?
-
Describe the key constituents of the logic model. Give two examples for each.
-
How is marketing public relations different from traditional public relations?
-
How do cultural differences play a role in an individuals health literacy?
-
On May 9, 2016, Glenna purchases 500 shares of Ignaz Company stock for $7,500. On June 30, 2016, she writes a call option on the stock, giving the grantee the right to buy the stock for $9,000 during...
-
1. Use these cost, revenue, and probability estimates along with the decision tree to identify the best decision strategy for Trendy's Pies. 2. Suppose that Trendy is concerned about her probability...
-
A connected planar graph having 6 vertices, 7 edges contain _____________ regions. A. 15 B. 3 C. 1 D. 11
-
Describe non-recursive DFS algorithm in brief.
-
The given Graph is regular. A. True B. False A B F D E
-
Assignment 1 1. Write a program to implement the following formula for a -10 and b = 4; result = (a + 2b - 4ab)^4 // ^4 means to the power of 4 (1 mark) 2. Write a program to implement result = sin...
-
I have most of it done, I just need help fixing some things. import java.util.Scanner; public class Main { private String[] database; private Scanner input; public Main() { database = new...
-
Part 2 - WeatherMonitor Suppose you observe daily temperatures and want to know how hot each day is compared to recent days. That is, you want to: keep track of daily temperature values and know for...
Study smarter with the SolutionInn App