You are on page 1of 1

HCMUT/CO2003 Spring 2019

Lab 4 – Stack vs Queue


-----------
Instructions:
- You are expected to completed Problems 1 - 3 before attending the lab.
- Please submit the hardcopy of your program by the end of the lab.

1. Your tasks
The initial code for this lab provide you a skeleton to implement the following functions. Your tasks
are to complete these functions to make them work.

Problem 1.
Complete bool checkParentheses(char* arr) function that check a sequences of parentheses is
valid or not. Parentheses include regular parentheses (), square brackets [], and braces {}.

Example
char s1[] = "{(())}[]";
cout << checkParentheses(s1) << endl; // Output: 1 (Valid)
char s2[] = "({)}[]";
cout << checkParentheses(s2) << endl; // Output: 0 (Invalid)

Problem 2.
Complete int evaluatePostfixExpression(char* exp) function that return the result of the
postfix expression in exp. Note that the input expression only contains addition, subtraction,
multiplication, division on single digit numbers (operands).
Example:

char s3[] = "35+7*"; // equivalent to (3 + 5) * 7


cout << evaluatePostfixExpression(s3) << endl; // Output: 56

Problem 3.
Complete int evaluatePrefixExpression(char* exp) function that return the result of the
prefix expression in exp. Note that the input expression only contains addition, subtraction,
multiplication, division on single digit numbers (operands).
Example:

char s4[] = "-+98*72"; // equivalent to (9 + 8) - 7*2


cout << evaluatePrefixExpression(s4) << endl; // Output: 3

Instructor: Rang Nguyen TA: Thinh Vuong, Duy Tran

You might also like