Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class Stack
- {
- public:
- int maxn=10;
- char ara[10];
- int i=-1;
- char push(char c)
- {
- if(i<=maxn)
- {
- i++;
- ara[i]=c;
- }
- }
- void pop()
- {
- if(i>=0)
- {
- i--;
- }
- }
- int top()
- {
- return ara[i];
- }
- int IsEmpty()
- {
- if(i==-1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- };
- int main()
- {
- Stack s;
- char str[30];
- gets(str);
- for(int j=0;j<strlen(str);j++)
- {
- if(str[j]=='('||str[j]=='{'||str[j]=='[')
- {
- s.push(str[j]);
- }
- else if(str[j]==')')
- {
- if(s.IsEmpty())
- {
- cout<<"Invalid"<<endl;
- return 0;
- }
- else if(s.top()=='(')
- {
- s.pop();
- }
- }
- else if(str[j]=='}')
- {
- if(s.IsEmpty())
- {
- cout<<"Invalid"<<endl;
- return 0;
- }
- else if(s.top()=='{')
- {
- s.pop();
- }
- }
- else if(str[j]==']')
- {
- if(s.IsEmpty())
- {
- cout<<"Invalid"<<endl;
- return 0;
- }
- else if(s.top()=='[')
- {
- s.pop();
- }
- }
- }
- if(s.IsEmpty())
- {
- cout<<"Valid"<<endl;
- }
- else
- {
- cout<<"Invalid"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement