Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++11 Prefix Expression Evaluation Program Develop a program with functions that 1.) determine whether an expression is a valid prefix expression, and 2.) evaluate the

C++11 Prefix Expression Evaluation Program

Develop a program with functions that 1.) determine whether an expression is a valid prefix expression, and 2.) evaluate the expression if it is in fact a valid prefix expression. For reference, check this article on prefix expressions: https://en.wikipedia.org/wiki/Polish_notation Prefix Example: (5-6)x7 in prefix is x-567.

Psuedocode for a recursive valued function endPre, which examines the expression that begins at position first of the string strExp and to locate the end of the first prefix expression it finds. If successful, the function returns the index of the end of the prefix expression. If no such prefix expression exists, endPre returns 1. Use the following psuedocode to implement endPre: // Precondition: The substring of strExp from the index First through the end of the string contains no blank characters. // Postcondition: Returns the index of the last character in the prefix expression that begins at index First of strExp, or 1 if no such prefix expression exists. endPre(strExp: string, First: integer): integer last = strExp. 1ength() - 1 if (First < 0 or First > last) return -1 ch = character at position First of strExp if (ch is an identifier) return First // Index of last character in simple prefix expression else if (ch is an operator) { // Find the end of the first prefix expression firstEnd = endPre(strExp, fi rst + 1) // If the end of thefint prefix expression was found, find the end of the second prefix expression if (fi rstEnd > -1) return endPre(strExp, fi rstEnd + 1) // Point Y else return -1 } else return -1 You can use the function endPre to determine whether a string contains a prefix expression as follows: isPrefix(strExp:string):boolean lastChar=endPre(strExp,0) return (lastChar>=0) and (lastChar==strExp.length()-1)

Use the following psuedocode to develop a function evaluatePrefix which evaluates a valid prefix expression: //Precondition: strExp is a string containing a valid prefix expression with no blanks evaluatePrefix(strExp: string): float strLength = the length of strExp if (strLength == 1) return value of the identifier else { op = strExp[0] // Find the end of the first prefix expressionwill be the first operand endFirst = endPre(strExp, 1) // Recursively evaluate this first prefix expression operand1 = evaluatePrefix(strExp[1..endFirst]); // Recursively, evaluate the second prefix expression-will be the second operand endSecond = strLength - endFirst + 1 operand2 = evaluatePrefix(strExp[endFirst + 1, endSecond]) // Evaluate the prefix expression return operand1 op operand2 }

Use the functions to check if various strings are valid prefix expressions, and evaluate the expressions: +*ab-cd and *ab+-cd, where a = 2, b = 5, c = 3, and d = 7.

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

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions

Question

Discuss all branches of science

Answered: 1 week ago