TheAlgorithms-C-Plus-Plus/Others/Paranthesis Matching.cpp

55 lines
790 B
C++
Raw Normal View History

2017-12-24 01:30:49 +08:00
#include<iostream>
#include<stdlib.h>
2018-03-28 04:51:40 +08:00
#include<string>
2017-12-24 01:30:49 +08:00
#include<stdio.h>
using namespace std;
2018-03-28 04:51:40 +08:00
const int MAX = 100;
// -------------- stack --------------
char stack[MAX];
2017-12-24 01:30:49 +08:00
int top=0;
void push(char ch)
{
stack[top++]=ch;
}
char pop()
{
return stack[--top];
}
2018-03-28 04:51:40 +08:00
// -------------- end stack -----------
2017-12-24 01:30:49 +08:00
int main()
{
2018-03-28 04:51:40 +08:00
string exp;
2017-12-24 01:30:49 +08:00
cout<<"Enter The Expression : ";
2018-03-28 04:51:40 +08:00
cin >> exp;
for (int i = 0; i < exp.length(); i++)
2017-12-24 01:30:49 +08:00
{
if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<')
{
push(exp[i]);
}
else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>')
{
2018-03-28 04:51:40 +08:00
pop();
2017-12-24 01:30:49 +08:00
}
}
2018-03-28 04:51:40 +08:00
// makes sure the stack is empty after processsing (above)
2017-12-24 01:30:49 +08:00
if(top==0)
{
cout<<"Correct Expression";
}
else
{
cout<<"\nWrong Expression";
}
2018-03-28 04:51:40 +08:00
2017-12-24 01:30:49 +08:00
return 0;
}