Advertisement
apl-mhd

Bappy sir 3rd assignment

Mar 28th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. using namespace std;
  6. #define MAXSIZE 100
  7. char number[MAXSIZE];
  8. int top=-1;
  9.  
  10. void Push(char x){
  11.        
  12.     if(top == MAXSIZE -1){
  13.         cout<<"Error: Overflow !"<<endl;
  14.         return;
  15.     }
  16.    
  17.     number[++top] = x;
  18.    
  19. }
  20.  
  21. void Pop(){
  22.    
  23.     if(top == -1)
  24.     cout<<"Error:Underflow."<<endl;
  25.    
  26.     else
  27.         top--;
  28.    
  29. }
  30.  
  31. char Top(){
  32.        
  33.     //printf("%d\n", number[top]);
  34.    
  35.     return number[top];
  36. }
  37.  
  38.  
  39. void Print(){
  40.    
  41.     for(int i=0; i<=top; i++){
  42.        
  43.         cout<<number[i]<<" ";
  44.         }
  45.     cout<<endl;
  46.    
  47.     }
  48. bool isEmpty(){
  49.        
  50.     if(top == -1)
  51.         return false;
  52.     else
  53.     return true;
  54.    
  55. }
  56.  
  57.  
  58. int main(int argc, char **argv)
  59. {
  60.     string expression;
  61.     char ch, cTop;
  62.     cin>>expression;
  63.    
  64.     for(int i=0; i<expression.size(); i++){
  65.        
  66.         ch = expression[i];
  67.        
  68.     if(ch == '(' || ch == '{' || ch == '['){
  69.            
  70.             Push(ch);
  71.  
  72.         }
  73.     else if(ch == ')' || ch == '}' || ch == ']'){
  74.            
  75.             cTop = Top();
  76.            
  77.             if(ch == ')' && cTop == '(')
  78.                 Pop();
  79.             else if(ch == '}' && cTop == '{')
  80.                 Pop();
  81.             else if(ch == ']' && cTop == '[')
  82.                 Pop();
  83.             else
  84.                
  85.                 Push(ch);
  86.              
  87.         }
  88.     }
  89.    
  90.     bool check;
  91.    
  92.     check = isEmpty();
  93.    
  94.     ch = Top();
  95.    
  96.     if(check == false)
  97.         cout<<"Ballanced"<<endl;
  98.        
  99.     else if(top>1)
  100.         cout<<"Unmatched"<<endl;
  101.     else if(ch =='(' || ch =='{' || ch =='[')
  102.         cout<<"Opening bracket overflow"<<endl;
  103.     else
  104.         cout<<"Closing bracket overflow"<<endl;
  105.    
  106.    
  107.                
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement