Assignment5
Assignment5
Assignment5
PAGE 1
Problem Statement : Postfix Expression Evaluation :
#include <iostream>
#include <stack>
#include <string>
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();
return 0;
}
PAGE 2
PROBLEM STATEMENT : INFIX TO POSTFIX
#include <iostream>
#include <string>
#include <stack>
void infixToPostfix(string s)
{
stack<char> st;
string result;
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();
}
int main()
{
PAGE 3
cout<<"enter expression"<<" : ";
string exp;
getline(cin,exp);
infixToPostfix(exp);
return 0;
}
#include <iostream>
#include <stack>
class Solution {
public:
int longestValidParentheses(string s) {
stack<char> ct;
stack<int> index;
index.push(-1);
int max_length = 0;
PAGE 4
int main() {
Solution solution;
string input = "(()())())()";
cout << "Length of the longest valid parentheses substring: " << solution.longestValidParentheses(input) <<
endl;
return 0;
}
PAGE 5