Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help me 7'. getEvalSteps(expr) [30 pts] Write the function getEvalSteps(expr'), that takes a string containing a simple arithmetic expression, and returns a multi-line string

please help me

image text in transcribed
7'. getEvalSteps(expr) [30 pts] Write the function getEvalSteps(expr'), that takes a string containing a simple arithmetic expression, and returns a multi-line string containing the step bystep [one operator at a time) evaluation of that expression. For example, this call: getEvalSteps("2+3*48**3%3") produces this result (which is a single multiline string): 2+3*4-s**3x3 = 2+3\"4512%3 = 2+12512x3 = 2+122 = 142 = 12 Here are some considerations and hints: 0 000 000000 You are only responsible for legal input as described below. Numbers are limited to nonnegative integers. Operators are limited to +, e, *, If, 96, and \". All operators are binary, so they take two operands. So there are no unary operators, meaning "-"5 is not a legal input. For that, you'd need "05". In fact, the previous restriction is even stronger: no intermediate value of expr can be negative! So "1 -2+3" is not legal, since it would be converted first into "'I +3" which is not legal. So you can safely ignore all such cases. There are no parentheses. Operators must be evaluated by precedence [** first, then *,//,%, then +,). Equalprecedence operators are evaluated lefttorig ht. Evaluation proceeds until a single integer with no operators remains after the equals sign. The equal signs must all be stacked directly over each other. You may write this however you wish, though you may want to write a helper function that finds the next operator to be applied, and another helper function thatjust applies a single operator. Something like that. In any case, topdown design is crucial here. And don't forget to thoroughly test your helper functions before using them! In our sample solution, we used very few string methods,just "find" and "isdigit". You may use others, but you should not spin your wheels trying to find that awesome string method that will make this problem remarkably easier, as that method does not exist. For this function, as any other function, you may not use the eval function, so you will have to write your own equivalentjust for the kinds of simple expressions you will deal with here. Eval is dangerous and should be avoided, as it can lead to serious bugs or security holes

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions