Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following question posted down below asked to modify an algorithm given in class for evaluating arithmetic expressions. I have attached slides 10 and 13

The following question posted down below asked to modify an algorithm given in class for evaluating arithmetic expressions. I have attached slides 10 and 13 below the question screenshot, both of which have a light green background. Additionally, I will also be copying and pasting the program given that randomly generated arithmetic expressions and uses them to verify the correctness of the program. Include as many comments on the code that is given and attach all outputs once the code runs! Give the code using C++ as the coding language, preferable on Visual Studio 2015.

image text in transcribedSlides 10 & 13 are as follows:

image text in transcribed

image text in transcribedCode given is pasted down below:

#include  #include  #include  #include  using namespace std; string rand_gen_z3(int); int main(){ int N = 50; string s = rand_gen_z3(N); cout 0 && s.find(')')!=string::npos){ int next_symbol_index = rand()%(s.length() + 1); if (next_symbol_index==s.length()) next_symbol = ')'; else next_symbol = s[next_symbol_index]; } else next_symbol = s[rand()%s.length()]; switch(next_symbol){ case ')': if(num_of_lp == 0) // All parentheses seen so far matched // ) is not allowed for next symbol again = true; else num_of_lp--; break; case '(': num_of_lp++; break; } }while(again); z3_exp += next_symbol; last_symbol = next_symbol; } //check if the last symbol is an operator or '(' and hence need an operand to complete the expression if(last_symbol == '(' || last_symbol=='*' || last_symbol=='/' || last_symbol=='+' ||last_symbol == '-') z3_exp += symbols[rand()%3]; //adding necessary closing )'s for(int i = 0; i   Part II Modify and implement the algorithm given in class (see pages 10 and 13 of the supplemental slides for Chapter 4) for evaluating arithmetic expressions with the following restrictions and modifications:  The operands are limited to 0, 1, and 2. The operations are limited to +, - *,/, (and). All those operations carry the same meanings as in regular arithmetic expressions except that O All operations are module 3, i.e., . 1+2=0 since 1+2=3=0 module 3, 1-2=2 since -1 = 2 module 3, : 2*2=1 since 2*2=4=1 module 3, and 1 1/2 = 2 since 2*2=4=1 module 3. O + and - can be used an unary operation and the first token in an expression; i.e., all the following expressions are considered valid: . +3+5, +(8-9), 1+(+2+5), and (+2)*7. -2+1, -(1+4), 1+(-2+5), and (-2)*7. You may use the attached program to randomly generate arithmetic expressions and use them to verify the correctness of your program. You may also use the following test cases: - - - -2-1+0*0+0+(+(0*0+2)) value: 1 ((1*0))*(0+(+1+2)*1) value: 0 +0*1+(1*0)-2*0+0*1-2-(0)*(2*0+(1)-1)-0+0*0-((2-1)) value: 2 (1)+1+2+(0-1+2-(+(2-0+(1-1)*1+2)/2+2*2-2+2-0))+0+2 value: 1 (-2-1+2/1)*(-1*(1)/1-1)+0/0/2-2-(2)/(2-(+1)* 2+0/0) value: invalid expression since O is used as an denominator. Evaluating Postfix Arithmetic Expressions: the Complete Algorithm Input: An array E[1..n] of n tokens that represents a postfix arithmetic expressions Output: the value of the postfix expression E. i  wi n c i 1. Initialize stack S to be empty 2. For i=1 to n do if E[i] is an number operand push E[i] to S; else /ow we know E[i]  {+, -, * /} s = Pop(S) and report error if the pop fails or s is not a number t = Pop(S) and report error if the pop fails or t is not a number r = the result of performing E[i] on s and t Push(s, r) 10. Return Pop(s) /eed any error checking here? Converting an Infix Expression to a Postfix Expression: The Algorithm Input: An array E[1..n] of n tokens that represents an infix arithmetic expression Output: the postfix form of expression E.    1. Initialize stack S and output string T to be empty 2. For i=1 to n do 3. Process E[i] according to the following cases: case 0: E[i] is a number. T+= E[i]. I/Operands always come before the operation case 1: E[i]  {+,-,*,7,(}. While (!empty(S) and peek(S) has a precedence over E[i]) T += pop(S) PUSH(S, E[i]); case 2: E[i] is ). While !empty(S) and (peek(S) != "("). T += pop(s) 10. Return T      Part II Modify and implement the algorithm given in class (see pages 10 and 13 of the supplemental slides for Chapter 4) for evaluating arithmetic expressions with the following restrictions and modifications:  The operands are limited to 0, 1, and 2. The operations are limited to +, - *,/, (and). All those operations carry the same meanings as in regular arithmetic expressions except that O All operations are module 3, i.e., . 1+2=0 since 1+2=3=0 module 3, 1-2=2 since -1 = 2 module 3, : 2*2=1 since 2*2=4=1 module 3, and 1 1/2 = 2 since 2*2=4=1 module 3. O + and - can be used an unary operation and the first token in an expression; i.e., all the following expressions are considered valid: . +3+5, +(8-9), 1+(+2+5), and (+2)*7. -2+1, -(1+4), 1+(-2+5), and (-2)*7. You may use the attached program to randomly generate arithmetic expressions and use them to verify the correctness of your program. You may also use the following test cases: - - - -2-1+0*0+0+(+(0*0+2)) value: 1 ((1*0))*(0+(+1+2)*1) value: 0 +0*1+(1*0)-2*0+0*1-2-(0)*(2*0+(1)-1)-0+0*0-((2-1)) value: 2 (1)+1+2+(0-1+2-(+(2-0+(1-1)*1+2)/2+2*2-2+2-0))+0+2 value: 1 (-2-1+2/1)*(-1*(1)/1-1)+0/0/2-2-(2)/(2-(+1)* 2+0/0) value: invalid expression since O is used as an denominator. Evaluating Postfix Arithmetic Expressions: the Complete Algorithm Input: An array E[1..n] of n tokens that represents a postfix arithmetic expressions Output: the value of the postfix expression E. i  wi n c i 1. Initialize stack S to be empty 2. For i=1 to n do if E[i] is an number operand push E[i] to S; else /ow we know E[i]  {+, -, * /} s = Pop(S) and report error if the pop fails or s is not a number t = Pop(S) and report error if the pop fails or t is not a number r = the result of performing E[i] on s and t Push(s, r) 10. Return Pop(s) /eed any error checking here? Converting an Infix Expression to a Postfix Expression: The Algorithm Input: An array E[1..n] of n tokens that represents an infix arithmetic expression Output: the postfix form of expression E.    1. Initialize stack S and output string T to be empty 2. For i=1 to n do 3. Process E[i] according to the following cases: case 0: E[i] is a number. T+= E[i]. I/Operands always come before the operation case 1: E[i]  {+,-,*,7,(}. While (!empty(S) and peek(S) has a precedence over E[i]) T += pop(S) PUSH(S, E[i]); case 2: E[i] is ). While !empty(S) and (peek(S) != "("). T += pop(s) 10. Return T

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

Students also viewed these Databases questions