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?...
-
Find both first partial derivatives. z = x - 4xy + 3y
-
Explain why it is important to report communicable diseases, adverse drug reactions, and infectious diseases.
-
Your firm is contemplating the purchase of a new $410,000 computer-based order entry system. The system will be depreciated straight-line to zero over its five-year life. It will be worth $30,000 at...
-
a. Discuss the role of Bank Supervision. b. Outline the selected Core Principles for Effective Banking Supervision. c. Discuss how to assess the effectiveness of supervision d. Evaluate the...
-
Accounting records for The Ralston Company show the following for the most recent fiscal year: Required Prepare: a. A gross margin income statement. b. A contribution margin income statement. Units...
-
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...
-
A uniform live load of 250 lb/ft and a single concentrated live force of 1.5 k are to be placed on the floor beams. Determine (a) The maximum positive shear in panel AB, and (b) The maximum moment at...
-
1. Calculate E[R]. Hint: Would it make things easier if you broke supp (R) into multiple parts? Remember that I never want to assign you very difficult integrals. 2. Calculate Var[R]. Hint: Use the...
-
A Drug Identification Number (DIN) is: 1) An 8-digit number given by Service Canada that confirms the disinfectant or sanitizer product is approved and safe for use in Canada 2) A 10-digit number...
-
Retail business in smaller towns in India runs with two primary business models-hawkers and retail outlets. Discuss the two business models. Explain details with details explanation and conclusion.
-
(ii) Briefly describe the unconventional policy of quantitative tightening (tapering)? How is it different or similar to our conventional understanding of a contractionary monetary policy through the...
-
Suppose that P1 can be either a low type or a high type, and this type is hidden from P2. If P1 is a low type, payoffs from playing the game P2 X Y P1 (L) A 0,0 -1,1 B -2,2 0,0 while if she is a high...
-
Using the categories in the following legend, classify each transaction as a plus (+) or minus (-) on Schedule M-2 of Form 1120S. An answer might look like one of these: +AAA or -OAA. Legend AAA =...
-
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
-
Reflecting on the book Who Gets Promoted, Who Doesn't, and Why (Asher, 2008), answer the following questions in depth. Explain the statement, "all business is sales." What six things matter? If...
-
Reflecting on the book Who Gets Promoted, Who Doesn't, and Why (Asher, 2008), answer the following questions in depth. No matter what you have done in the past, the boss really doesn't care. Why?...
-
Do you think Richard Scrushy should have been found guilty or acquitted for his role in the case against HealthSouth? https://www.wsj.com/articles/SB111702610398942860
Literature For Composition An Introduction To Literature 10th Edition - ISBN: 0321829174 - Free Book
Study smarter with the SolutionInn App