Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Evaluate Postfix (10 points) Sketch of algorithm for evaluating a postfix string: (1) Create stack s. (2) For each token, r, in the postfix expression:

Evaluate Postfix (10 points) Sketch of algorithm for evaluating a postfix string: (1) Create stack s. (2) For each token, r, in the postfix expression: 1 If r is T or F push it into the stack s. 2 Else if x is a unary operator i If you do not havp at least one operand in s, you should return an error in the boolean (remember to free your data). ii pop an operand, opl, from s iii computer opl (see unary table) iv push the result into s v free opl and I 3 Else if x is a binary operator i If you do not have at least two operands in s, you should return an error in the boolean (remember to free your data). ii pop an operand, op2, from s iii pop an operand, opl, from s iv compute opl op2 r (see binary table) v push the result into s vi free opl, op2, and r. (3) If s contains more than one operand after all of the tokens are evaluated you should return an error in the boolean. (4) Otherwise pop and return the only value in s. (5) Be sure to free all remaining malloced data before leaving the function (including when returning an error). Do not free the returned string. Operator Type unary operator binary operator binary operator binary operator binary operator binary operator binary operator binary operator Usage op1 NOT opl op2 AND opl op2 NAND opl op2 OR opl op2 NOR opl op2 XOR opl op2 COND opl op2 BICOND Calculation lop1 opl && op2 !(op1 && op2) op1 || op2 !(opl || op2) opl != op2 lop1 || op2 op1 op2 Convert Postfix to Infix (10 points) Sketch of algorithm for converting a postfix strings to an infix strings: (1) Create stack s. (2) For each token, r, in the postfix expression: 1 If r is T or F push it into the stack s. 2 Else if x is a unary operator i pop an operand, opl, from s ii push the string "(r opl)" into s iii free opl and I 3 Else if r is a binary operator i pop an operand, op2, from s ii pop an operand, opl, from s iii push the string "opl r op2)" into s iv free opl, op2, and r (3) You assume that the postfix string is well formatted (feel free to imple- ment error checking if you would like). (4) pop and return the value in s. (5) Be sure to free all remaining malloced data before leaving the function (including when returning an error). Do not free the returned string. Hints for memory management: Every string that you push into your stack should be malloced. Using the provided function "boolean ToString" from the file "boolean WithError.c" will make this easier. It also a couple other handy functions that you should consider using. . I would recommend not using "strtok" to tokenize your string (it will require significantly more work on your part). Instead you should use the "tokenizeString" provided in the assignment. . You should free strings after popping them from your stack (be sure to use them before freeing them). Operator tokens aren't pushed in the stack and can be freed immediately after usage. Be sure to free all of your malloced data before returning. This included freeing your data in the case of an error. . Remember to also free any remaining tokens in your stack and in the token array before returning. This should only be necessary in the case of an error Deliverables: Your solution should be submitted as "booleanEvaluation.c", "booleanEvalu- ation.h", and "makefile". Also attach any additional files you create to solve this problem.

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

Mobile Usability

Authors: Jakob Nielsen, Raluca Budiu

1st Edition

0133122131, 9780133122138

More Books

Students also viewed these Programming questions