Skip to main content

DSL D26

Data Structure Lab :

Practical D26:

 

In any language program mostly syntax error occurs due to unbalancing delimiter such as

(),{},[]. Write C++ program using stack to check whether given expression is well

parenthesized or not.

 // Author : SE CSE GURU  
 
 #include<iostream>  
 #include<string>  
   
 using namespace std;  
   
 class Parenthesis{  
        
      public:  
           int Top,max;  
           char Stack[30];  
             
           Parenthesis(){  
             
           Top=-1;   
           max=29;   
        
      }  
           void push(char);  
           char pull();  
           int isFull();  
           int isEmpty();  
        
 };  
   
 void Parenthesis::push(char ch){  
        
      if(isFull()){  
           cout<<"Stack is Overflow "<<endl;  
      }  
      else{  
              
      Top++;   
      Stack[Top]=ch;   
       
      }  
             
 }  
   
 char Parenthesis::pull(){  
      if(isEmpty()){  
           cout<<"Stack is Underflow "<<endl;  
      }  
      else{  
           return(Stack[Top--]);  
      }  
 }  
   
 int Parenthesis::isFull(){  
      if(Top==max){  
           return 1;  
      }  
      else{  
           return 0;  
      }  
 }  
   
 int Parenthesis::isEmpty(){  
      if(Top==-1){  
           return 1;  
      }  
      else{  
           return 0;  
      }  
 }  
   
 int main(){  
      char ch,choice;  
      string str;  
      int length;  
      do  
      {  
      Parenthesis obj;  
       
      cout<<"Enter expression : "<<endl;   
      cin>>str;  
      length=str.size();   
     
      int flag=0;  
         
      for(int i=0;i<length;i++){  
          
      ch=str[i];  
          
      if (ch=='('||ch=='['||ch=='{'){  
               
          obj.push(ch);  
           }  
      else{  
                  
                if(!( (ch==')' && obj.pull()=='(') || (ch==']' && obj.pull()=='[') || (ch=='}' && obj.pull()=='{') ) ){  
                       
                     flag=1;  
                     break;  
                }  
           }  
        
      }  
        
      if(obj.isEmpty() && flag==0){  
           cout<<"The Expression is well parenthesized :) "<<endl;  
      }  
      else{  
           cout<<"The Expression is NOT well parenthesized :( "<<endl;  
      }  
      cout<<"Do you want to continue (y or n)"<<endl;  
      cin>>choice;  
      }while(choice=='y');  
        
      return 0;  
 }  
   

Comments

Popular posts from this blog

Circle Triangle Pattern

  Group A   Practical 3 a   Problem Statement :   a) Write C++ program to draw the following pattern. Use DDA line and Bresenham‘s circle drawing algorithm. Apply the concept of encapsulation. Check Out Code Here  ðŸ‘‡ Output :  Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊

DSL A2

  Write a Python program to store marks scored in subject “Fundamental of Data Structure” by N students in the class. Write functions to compute following:  a) The average score of class  b) Highest score and lowest score of class  c) Count of students who were absent for the test  d) Display mark with highest frequency The Code for above problem is as follows : """ @author: SPPU SE GURU """ total=int(input ("Enter total no. of students in your class ")) pre=int(input("No. of students appeared for FDS exam : ")) U=[] for i in range (total): i=i+1 U.append(i) R=[] for i in range(pre): roll=int(input("Enter Roll no. of students present for test :- ")) R.append(roll) M=[] for i in range(pre): print("Enter Roll no. ",R[i],end=" ") marks=int(input("Marks:- ")) M.append(marks) print("********** Solutions Are as fo...

OOP 4

  Object Oriented Programming Group B - Practical : 4 Problem Statement :  Write a C++ program that creates an output file, writes information to it, closes the file, open it again as an input file and read the information from the file. Check Out Code Here  ðŸ‘‡ Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊