Ass 2
Ass 2
Total Marks: 04
Obtained Marks:
CLO 2 – C4 – PLO A
Q1: (2 Marks)
Convert the following Infix expression into a Prefix expression using Stack:
A + B * (C + D) / F + D * E
(a + b – c) * (e / f) – (g – h/i)
Q2: (2 Marks)
Emma, a developer, is working on a compiler that checks for balanced expressions in code. Her
task requires analyzing each line to ensure that every opening bracket—whether parentheses,
curly braces, or square brackets—has a corresponding closing bracket in the correct order.
Emma needs to carefully go through each expression step-by-step, verifying that it is balanced to
avoid syntax errors during compilation. Your task is to help her by showing each step to confirm
the balance of brackets in a given expression when using a stack.
((()]))
Question 1:
1. Expression: A + B * (C + D) / F + D * E
Step 1: Reverse the Infix Expression
Reversed: E * D + F / )D + C ( * B + A
Step 2: Replace Parentheses
Modified: E * D + F / (D + C) * B + A
Step 3: Convert to Postfix
Output: ``
Stack: []
Processing:
E → Output: E
* → Stack: [*]
D → Output: E D
+ → Pop *, Output: E D * Stack: [+], Push +.
F → Output: E D * F
/ → Stack: [+, /]
( → Stack: [+, /, (]
D → Output: E D * F D
+ → Stack: [+, /, (, +]
C → Output: E D * F D C
) → Pop +, Output: E D * F D C +, Stack: [+, /]
Pop /, Output: E D * F D C + /, Stack: [+]
Pop +, Output: E D * F D C + / +, Stack: []
B → Output: E D * F D C + / + B
+ → Stack: [+]
A → Output: E D * F D C + / + B A
Pop +, Output: E D * F D C + / + B A +
Postfix: E D * F / C + B + A +
Step 4: Reverse Postfix
Prefix: + + A B / * D C F E
2. Expression: (a + b - c) * (e / f) - (g - h / i)
Step 1: Reverse the Infix Expression
Reversed: )i / h - g ( - )f / e ( * )c - b + a (
Step 2: Replace Parentheses
Modified: (i / h - g) - (f / e) * (c - (b + a))
DSA BS(AI)-3C SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University
Question 2:
To check if the expression ((())]) contains balanced brackets using a stack, we will follow these steps:
Create a Stack: This will help us track opening brackets as we encounter them.
Iterate Through Each Character: For every character in the expression:
If it is an opening bracket (i.e., (, {, or [), push it onto the stack.
If it is a closing bracket (i.e., ), }, or ]), check the stack. If it’s not empty and the top element
of the stack is the matching opening bracket, pop the stack. If they don’t match, the
expression is unbalanced.
Final Evaluation: After processing all characters, if the stack is empty, the brackets are balanced; if not,
they are unbalanced.
Step-by-Step Analysis of the Expression ((())]):
1. Start with an Empty Stack:
Stack: []
2. Examine Each Character:
First Character: (
It's an opening bracket.
Stack: ['(']
Second Character: (
It's also an opening bracket.
Stack: ['(', '(']
Third Character: (
This is yet another opening bracket.
Stack: ['(', '(', '(']
Fourth Character: )
This is a closing bracket.
Check the stack:
o The stack isn’t empty.
o The top item is (, which corresponds correctly to the closing bracket ).
Pop the stack.
Stack: ['(', '(']
Fifth Character: )
Another closing bracket.
Check the stack:
The stack still isn’t empty.
o The top item is (, which matches ).
DSA BS(AI)-3C SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology University
#include <iostream>
#include <stack>
using namespace std;
bool isBalanced(const string& expression) {
stack<char> stack;
for (char ch : expression) {
if (ch == '(' || ch == '{' || ch == '[') {
stack.push(ch);
} else if (ch == ')' || ch == '}' || ch == ']') {
if (stack.empty()) return false;
char top = stack.top();
if ((ch == ')' && top != '(') || (ch == '}' && top != '{') || (ch == ']' && top != '[')) {
return false;
}
stack.pop();
}
}
return stack.empty();
}
int main() {
string expression = "((())])";
if (isBalanced(expression)) {
cout << "The expression '" << expression << "' is balanced." << endl;
} else {
cout << "The expression '" << expression << "' is not balanced." << endl;
}
return 0;
}