Professional Documents
Culture Documents
DSAEXP5
DSAEXP5
ALGORITHM
Experiment no. 5
Name: Juee Jamsandekar ROLL NO:A090
Batch/semester: Btech IT/3rd Batch :2
Objective:
return 0;
}
string convert(string infix)
{
int i = 0;
string postfix = "";
stack <int>s;
while(infix[i]!='\0')
{
else if(infix[i]==')')
{
while(s.top()!='('){
postfix += s.top();
s.pop();
}
s.pop();
i++;
}
else
{
while (!s.empty() && priority(infix[i]) <= priority(s.top())){
postfix += s.top();
s.pop();
}
s.push(infix[i]);
i++;
}
}
while(!s.empty()){
postfix += s.top();
s.pop();
}
int main()
{
string infix = "((a+(b*c))-d)+(e-f*(g/h))";
string postfix;
postfix = convert(infix);
return 0;
}
Output:
Code:
#include<iostream>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
stack<int> s;
int main(){
if (isdigit(exp[i]))
s.push(exp[i] - '0');
else{
int op2=s.top();
s.pop();
int op1=s.top();
s.pop();
if(exp[i]=='+')
s.push(op1+op2);
else if(exp[i]=='-')
s.push(op1-op2);
else if(exp[i]=='*')
s.push(op1*op2);
else if(exp[i]=='/')
s.push(op1/op2);
}
}
cout<<"After evalution we get: "<<s.top();
return 0;
}
Output:
Code:
#include <iostream> //main header file
#include <stack>
using namespace std;
void balance_parentheses();
int main()
{
int t;
cout << "Enter number of test cases:";
cin >> t;
return 0;
}
void balance_parentheses()
{
stack<char> a;
string s;
cout << "Enter string may or may not containing parentheses:";
cin >> s;
{
if (s[i] == '{' || s[i] == '[' || s[i] == '(') {
a.push(s[i]);
flag = 1;
}
if (!a.empty()) {
if (s[i] == '}') {
if (a.top() == '{')
// top of the stack
{
a.pop();
continue;
}
else
break;
}
if (s[i] == ']') {
if (a.top() == '[') {
a.pop();
continue;
}
else
break;
}
if (s[i] == ')') {
if (a.top() == '(') {
a.pop();
continue;
}
else
break;
}
}
else {
break;
}
}
Output: