Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CSE 212- Data Structure Lab || Lab -06

Name: Jihad H0ssain Jisan;


ID:231016712

Faculty : Md. Jahidul Hasan Jahid


Table of Contents
Problem Statement : Postfix Expression Evaluation : ............................................................................................................................................2
Problem statement : Infix to Postfix ......................................................................................................................................................................... 3
Problem statement : Longest Valid Parentheses .................................................................................................................................................... 4

PAGE 1
Problem Statement : Postfix Expression Evaluation :

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int evaluatePostfix(const string& expression) {


stack<int> s;
string number;

for (int i = 0; i < expression.size(); i++) {


if (expression[i] == ' ') {
if (!number.empty()) {
s.push(stoi(number));
number.clear();
}
continue;
}

if (isdigit(expression[i]) || (expression[i] == '-' && isdigit(expression[i+1]))) {


number += expression[i];
} else {
int val2 = s.top();
s.pop();
int val1 = s.top();
s.pop();

switch (expression[i]) {
case '+': s.push(val1 + val2); break;
case '-': s.push(val1 - val2); break;
case '*': s.push(val1 * val2); break;
case '/': s.push(val1 / val2); break;
}
}
}

return s.top();
}

int main() {
int N;
cout<<"number of expression"<<" : ";
cin >> N;
cin.ignore();

for (int i = 0; i < N; ++i) {


string expression;
getline(cin, expression);
cout << "Result of test case " << i+1 << ": " << evaluatePostfix(expression) << endl;
}

return 0;
}

PAGE 2
PROBLEM STATEMENT : INFIX TO POSTFIX

#include <iostream>
#include <string>
#include <stack>

using namespace std;


int prec(char c) {
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}

void infixToPostfix(string s)
{
stack<char> st;
string result;

for (int i = 0; i < s.length(); i++) {


char c = s[i];

if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
result += c;
else if (c == '(')
st.push('(');
else if (c == ')') {
while (st.top() != '(') {
result += st.top();
st.pop();
}
st.pop();
}
else {
while (!st.empty() && prec(c) <= prec(st.top())) {
result += st.top();
st.pop();
}
st.push(c);
}
}

while (!st.empty()) {
result += st.top();
st.pop();
}

cout << result << endl;


}

int main()
{

PAGE 3
cout<<"enter expression"<<" : ";

string exp;
getline(cin,exp);

infixToPostfix(exp);

return 0;
}

Problem statement : Longest Valid Parentheses

#include <iostream>
#include <stack>

using namespace std;

class Solution {
public:
int longestValidParentheses(string s) {
stack<char> ct;
stack<int> index;

index.push(-1);
int max_length = 0;

for (int i = 0; i < s.length(); ++i) {


if (s[i] == '(') {
ct.push('(');
index.push(i);
} else {
if (!ct.empty() && ct.top() == '(') {
ct.pop();
index.pop();
int current_length = i - index.top();
if (current_length > max_length) {
max_length = current_length;
}
} else {
ct.push(')');
index.push(i);
}
}
}
return max_length;
}
};

PAGE 4
int main() {
Solution solution;
string input = "(()())())()";
cout << "Length of the longest valid parentheses substring: " << solution.longestValidParentheses(input) <<
endl;
return 0;
}

PAGE 5

You might also like