Advertisement
Eternoseeker

Parenthesis Checking

Nov 23rd, 2022 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | Source Code | 0 0
  1. #include<iostream>
  2. #include  <bits/stdc++.h>
  3. #include<string>
  4. #define MAX 100
  5. #include"stack.h"
  6. using namespace std;
  7.  
  8. class Paranthesis{
  9.     Stack s;
  10.     char str[MAX];
  11. public:
  12.     void check(){
  13.         cout<<"\nEnter # after expression(At the end)\n";
  14.         cout<<"\nEnter Expression: ";
  15.         cin.getline(str,MAX,'#');
  16.         char ch;
  17.         bool flag=0;
  18.         for(int i=0;str[i]!='\0';i++)
  19.         {
  20.             if(str[i]=='(' || str[i]=='[' || str[i]=='{')
  21.                 s.push(str[i]);
  22.             if(str[i]==')'||str[i]==']'||str[i]=='}')
  23.             {
  24.             if(s.isEmpty()){
  25.                 flag = 1; break;}                
  26.             ch=s.pop();
  27.                 if((str[i]==')'&& ch!='(') ||(str[i]==']'&& ch!='[')||(str[i]=='}'&& ch!='{'))
  28.                 {
  29.                     cout<<"\nNot parenthesized At "<<i<<" = "<<str[i];
  30.                     flag=1;
  31.                     break;
  32.                 }
  33.             }
  34.         }
  35.         if(s.isEmpty()==1 && flag==0)
  36.             cout<<"\nExpresseion is Well Parenthesized.";
  37.         else
  38.             cout<<"\nExpression is not Well Parenthesized.";
  39.     }
  40.  
  41. };
  42.  
  43. int main(){
  44.     Paranthesis p;
  45.     p.check();
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement