Saturday, 14 April 2018

Department maintains a student information. The file contains roll number, name, division and address. Allow user to add, delete information of student. Display information of particular employee. If record of student does not exist an appropriate message is displayed. If it is, then the system displays the student details. Use sequential file to main the data.


#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
class tel
 {

 public:
int rollNo,roll1;
char name[10];
char div;
char address[20];
void accept()
{
cout<<"\n\tEnter Roll Number : ";
cin>>rollNo;
cout<<"\n\tEnter the Name : ";
cin>>name;
cout<<"\n\tEnter the Division:";
cin>>div;
cout<<"\n\tEnter the Address:";
cin>>address;
}
        void accept2()
        {
               cout<<"\n\tEnter the Roll No. to modify : ";
               cin>>rollNo;
        }
        void accept3()
        {
              cout<<"\n\tEnter the name to modify : ";
              cin>>name;
        }
        int getRollNo()
        {
        return rollNo;
        }
void show()
{

cout<<"\n\t"<<rollNo<<"\t\t"<<name<<"\t\t"<<div<<"\t\t"<<address;
}
};
int main()
{
int i,n,ch,ch1,rec,start,count,add,n1,add2,start2,n2,y,a,b,on,oname,add3,start3,n3,y1,add4,start4,n4;
char name[20],name2[20];
tel t1;
count=0;
fstream g,f;
do
{
cout<<"\n>>>>>>>>>>>>>>>>>>>>>>MENU<<<<<<<<<<<<<<<<<<<<";
cout<<"\n1.Insert and overwrite\n2.Show\n3.Search & Edit(number)\n4.Search & Edit(name)\n5.Search & Edit(onlynumber)\n6.Search & edit(only name)\n 7.Delete a Student Record\n 8.Exit\n\tEnter the Choice\t:";
cin>>ch;
switch(ch)
{
case 1:
f.open("StuRecord.txt",ios::out);
x:t1.accept();
f.write((char*) &t1,(sizeof(t1)));
cout<<"\nDo you want to enter more records?\n1.Yes\n2.No";
cin>>ch1;
if(ch1==1)
goto x;
else
{
f.close();
break;
}

case 2:
f.open("StuRecord.txt",ios::in);
f.read((char*) &t1,(sizeof(t1)));
//cout<<"\n\tRoll No.\t\tName \t\t Division \t\t Address";
while(f)
{
t1.show();
f.read((char*) &t1,(sizeof(t1)));
}
f.close();
break;
case 3:
cout<<"\nEnter the roll number you want to find";
cin>>rec;
f.open("StuRecord.txt",ios::in|ios::out);
f.read((char*)&t1,(sizeof(t1)));
while(f)
{
if(rec==t1.rollNo)
{
cout<<"\nRecord found";
add=f.tellg();
f.seekg(0,ios::beg);
       start=f.tellg();
n1=(add-start)/(sizeof(t1));
f.seekp((n1-1)*sizeof(t1),ios::beg);
t1.accept();
f.write((char*) &t1,(sizeof(t1)));
f.close();
count++;
break;
}
f.read((char*)&t1,(sizeof(t1)));
     }
if(count==0)
        cout<<"\nRecord not found";
f.close();
break;

case 4:
cout<<"\nEnter the name you want to find and edit";
cin>>name;
f.open("StuRecord.txt",ios::in|ios::out);
f.read((char*)&t1,(sizeof(t1)));
while(f)
{
y=(strcmp(name,t1.name));
if(y==0)
{
cout<<"\nName found";
add2=f.tellg();
f.seekg(0,ios::beg);
start2=f.tellg();
n2=(add2-start2)/(sizeof(t1));
f.seekp((n2-1)*sizeof(t1),ios::beg);
t1.accept();
f.write((char*) &t1,(sizeof(t1)));
f.close();
break;
}
        f.read((char*)&t1,(sizeof(t1)));
}
break;
     case 5:
           cout<<"\n\tEnter the roll number you want to modify";
           cin>>on;
           f.open("StuRecord.txt",ios::in|ios::out);
           f.read((char*) &t1,(sizeof(t1)));
           while(f)
           {
             if(on==t1.rollNo)
             {
               cout<<"\n\tNumber found";
               add3=f.tellg();
               f.seekg(0,ios::beg);
               start3=f.tellg();
               n3=(add3-start3)/(sizeof(t1));
               f.seekp((n3-1)*(sizeof(t1)),ios::beg);
               t1.accept2();
               f.write((char*)&t1,(sizeof(t1)));
               f.close();
               break;
             }
             f.read((char*)&t1,(sizeof(t1)));
          }
          break;
     case 6:
           cout<<"\nEnter the name you want to find and edit";
cin>>name2;
f.open("StuRecord.txt",ios::in|ios::out);
f.read((char*)&t1,(sizeof(t1)));
while(f)
{
y1=(strcmp(name2,t1.name));
if(y1==0)
{
cout<<"\nName found";
add4=f.tellg();
f.seekg(0,ios::beg);
start4=f.tellg();
n4=(add4-start4)/(sizeof(t1));
f.seekp((n4-1)*sizeof(t1),ios::beg);
t1.accept3();
f.write((char*) &t1,(sizeof(t1)));
f.close();
break;
}
        f.read((char*)&t1,(sizeof(t1)));
}
break;
   case 7:
    int roll;
    cout<<"Please Enter the Roll No. of Student Whose Info You Want to Delete: ";
  cin>>roll;
  f.open("StuRecord.txt",ios::in);
  g.open("temp.txt",ios::out);
  f.read((char *)&t1,sizeof(t1));
  while(!f.eof())
  {
    if (t1.getRollNo() != roll)
      g.write((char *)&t1,sizeof(t1));
      f.read((char *)&t1,sizeof(t1));
  }
  cout << "The record with the roll no. " << roll << " has been deleted " << endl;
  f.close();
  g.close();
  remove("StuRecord.txt");
  rename("temp.txt","StuRecord.txt");
    break;
   case 8:
      cout<<"\n\tThank you";
      break;


        }
  }while(ch!=8);
}

Read the marks obtained by students of second year in an online examination of particular subject. Find out maximum and minimum marks obtained in that subject. Use heap data structure. Analyze the algorithm.

#include <iostream>
//////////////////////////////////////////////////////////////
// //
//   Name : Vivek S. Sharma //
// Title : Heap Sort //
// //
// //
//////////////////////////////////////////////////////////////
using namespace std;


void MaxHeapify(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;

  while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;

if (temp > a[j])
break;

else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void MinHeapify(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;

  while (j <= n)
{
if (j < n && a[j+1] < a[j])
j = j+1;

if (temp < a[j])
break;

else if (temp >= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void MaxHeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{

temp = a[i];
a[i] = a[1];
a[1] = temp;

MaxHeapify(a, 1, i - 1);
}
}
void MinHeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{

temp = a[i];
a[i] = a[1];
a[1] = temp;

MinHeapify(a, 1, i - 1);
}
}
void Build_MaxHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1; i--)
MaxHeapify(a, i, n);
}
void Build_MinHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1; i--)
MinHeapify(a, i, n);
}
int main()
{
int n, i;
cout<<"\nEnter the number of Students : ";
cin>>n;
n++;
int arr[n];
for(i = 1; i < n; i++)
{
cout<<"Enter the marks :  "<<i<<": ";
cin>>arr[i];
}

Build_MaxHeap(arr, n-1);
MaxHeapSort(arr, n-1);


int max,min;
cout<<"\nSorted Data : ASCENDING : ";

for (i = 1; i < n; i++)
cout<<"->"<<arr[i];
min=arr[1];
Build_MinHeap(arr, n-1);
MinHeapSort(arr, n-1);
cout<<"\nSorted Data : DESCENDING: ";
max=arr[1];
for (i = 1; i < n; i++)
cout<<"->"<<arr[i];
cout<<"\nMaximum Marks : "<<max<<"\nMinimum marks : "<<min;
return 0;
}

Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client‘s telephone number.

#include<iostream>
//////////////////////////////////////////////////////////////
// //
//   Name : Vivek S. Sharma //
// Title : Hashing with sll //
// //
// //
//////////////////////////////////////////////////////////////
using namespace std;
 struct node{
int value;
node* next;
}*HashTable[10];
class hashing{
public:

hashing(){

for(int i=0 ; i<10 ; i++){
HashTable[i]=NULL;
}
}


int HashFunction(int value){
return (value%10);
}

node* create_node(int x){
node* temp=new node;
temp->next=NULL;
temp->value=x;
return temp;
}

void display(){
for(int i=0 ; i< 10; i++){
node * temp=new node;
temp=HashTable[i];
cout<<"a["<<i<<"] : ";
while(temp !=NULL){
cout<<" ->"<<temp->value;
temp=temp->next;
}
cout<<"\n";
}
}


int searchElement(int value){
bool flag = false;
            int hash_val = HashFunction(value);
            node* entry = HashTable[hash_val];
            cout<<"\nElement found at : ";
            while (entry != NULL)
    {
                if (entry->value==value)
        {
                    cout<<hash_val<<" : "<<entry->value<<endl;
                    flag = true;
                }
                entry = entry->next;
            }
            if (!flag)
                return -1;
}

void deleteElement(int value){
int hash_val = HashFunction(value);
            node* entry = HashTable[hash_val];

            if (entry == NULL )
            {
            cout<<"No Element found ";
                return;
            }

            if(entry->value==value){
            HashTable[hash_val]=entry->next;
            return;
            }
            while ((entry->next)->value != value)
    {
                entry = entry->next;
            }
            entry->next=(entry->next)->next;


}

void insertElement(int value){
int hash_val = HashFunction(value);
           // node* prev = NULL;
            //node* entry = HashTable[hash_val];
            node* temp=new node;
            node* head=new node;
            head = create_node(value);
            temp=HashTable[hash_val];
            if (temp == NULL)
                        {

                           HashTable[hash_val] =head;
                            }
            else{
            while (temp->next != NULL)

            {
                temp = temp->next;
            }

                    temp->next =head;

            }


}
};

int main(){
int ch;
int data,search,del;
hashing h;
do{
cout<<"\nTelephone : \n1.Insert \n2.Display \n3.Search \n4.Delete \n5.Exit";
cin>>ch;
switch(ch){
case 1:cout<<"\nEnter phone no. to be inserted : ";
cin>>data;
h.insertElement(data);
break;
case 2:h.display();
break;
case 3:cout<<"\nEnter the no to be searched : ";
cin>>search;

if (h.searchElement(search) == -1)
            {
        cout<<"No element found at key ";
        continue;
            }
break;
case 4:cout<<"\nEnter the phno. to be deleted : ";
cin>>del;
h.deleteElement(del);
cout<<"Phno. Deleted"<<endl;
break;
}
}while(ch!=5);
return 0;
}

Implement all the functions of a dictionary (ADT) using hashing. Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must be unique Standard Operations: Insert(key, value), Find(key), Delete(key)


#include<iostream>
#include<string.h>
//////////////////////////////////////////////////////////////
// //
//   Name : Vivek S. Sharma //
// Title : Hashing with probing //
// //
// //
//////////////////////////////////////////////////////////////
using namespace std;

class HashFunction
  {
     typedef struct hash
{
long key;
char name[10];
}hash;
hash h[10];
   public:
HashFunction();
void insert();
void display();
int find(long);
void Delete(long);


  };

HashFunction::HashFunction()
  {
int i;
for(i=0;i<10;i++)
  {
h[i].key=-1;
strcpy(h[i].name,"NULL");
  }
  }
void HashFunction::Delete(long k)
  {
int index=find(k);
if(index==-1)
  {
cout<<"\n\tKey Not Found";
  }
else
  {
h[index].key=-1;
strcpy(h[index].name,"NULL");
cout<<"\n\tKey is Deleted";
  }


  }
int HashFunction::find(long k)
  {
int i;
for(i=0;i<10;i++)
  {
if(h[i].key==k)
  {
cout<<"\n\t"<<h[i].key<<" is Found at "<<i<<" Location With Name "<<h[i].name;
return i;
  }
  }
if(i==10)
      {
return -1;
  }

  }


void HashFunction::display()
  {
int i;
cout<<"\n\t\tKey\t\tName";
for(i=0;i<10;i++)
    {
cout<<"\n\th["<<i<<"]\t"<<h[i].key<<"\t\t"<<h[i].name;
  }
  }

void HashFunction::insert()
  {
char ans,n[10],ntemp[10];
long k,temp;
int v,hi,cnt=0,flag=0,i;

do
  {
if(cnt>=10)
  {
cout<<"\n\tHash Table is FULL";
break;
  }
cout<<"\n\tEnter a Telephone No: ";
cin>>k;
cout<<"\n\tEnter a Client Name: ";
cin>>n;
hi=k%10;// hash function
if(h[hi].key==-1)
  {
h[hi].key=k;
strcpy(h[hi].name,n);
  }
     else
    {

if(h[hi].key%10!=hi)
  {
temp=h[hi].key;
strcpy(ntemp,h[hi].name);
h[hi].key=k;
strcpy(h[hi].name,n);
for(i=hi+1;i<10;i++)
    {
if(h[i].key==-1)
    {
h[i].key=temp;
strcpy(h[i].name,ntemp);
flag=1;
break;
      }
    }
for(i=0;i<hi && flag==0;i++)
    {
if(h[i].key==-1)
    {
h[i].key=temp;
strcpy(h[i].name,ntemp);
break;
      }
         }
    }
else
  {
for(i=hi+1;i<10;i++)
    {
if(h[i].key==-1)
    {
h[i].key=k;
strcpy(h[i].name,n);
flag=1;
break;
      }
    }
for(i=0;i<hi && flag==0;i++)
    {
if(h[i].key==-1)
    {
h[i].key=k;
strcpy(h[i].name,n);
break;
      }
       }
  }

  }
    flag=0;
     cnt++;
     cout<<"\n\t..... Do You Want to Insert More Key: y/n";
     cin>>ans;
  }while(ans=='y'||ans=='Y');

  }



int main()
  {
long k;
int ch,index;
char ans;
HashFunction obj;
do
  {
cout<<"\n\t***** Telephone (ADT) *****";
cout<<"\n\t1. Insert\n\t2. Display\n\t3. Find\n\t4. Delete\n\t5. Exit";
cout<<"\n\t..... Enter Your Choice: ";
cin>>ch;
switch(ch)
  {
case 1: obj.insert();
break;
case 2: obj.display();
break;
case 3: cout<<"\n\tEnter a Key Which You Want to Search: ";
cin>>k;
index=obj.find(k);
if(index==-1)
  {
cout<<"\n\tKey Not Found";
  }
break;
case 4: cout<<"\n\tEnter a Key Which You Want to Delete: ";
cin>>k;
obj.Delete(k);
break;
case 5:
break;
  }
cout<<"\n\t..... Do You Want to Continue in Main Menu:y/n ";
cin>>ans;
  }while(ans=='y'||ans=='Y');
  }


A Dictionary stores keywords & its meanings. Provide facility for adding new keywords, deleting keywords, updating values of any entry. Provide facility to display whole data sorted in ascending/ Descending order. Also find how many maximum comparisons may require for finding any keyword. Use Binary Search Tree for implementation.

#include"iostream"
#include<string.h>
using namespace std;
//////////////////////////////////////////////////////////////
// //
//   Name : Vivek S. Sharma //
// Title : BST //
// //
// //
//////////////////////////////////////////////////////////////
typedef struct node
{

 char k[20];
 char m[20];
 class node  *left;
 class node * right;
}node;

class dict
{
public:
 node *root;
 void create();
 void disp(node *);
 void insert(node * root,node *temp);
 int search(node *,char []);
 int update(node *,char []);
 node* del(node *,char []);
 node * min(node *);
};

void dict :: create()
{
 class node *temp;
 int ch;

 do
 {
  temp = new node;
  cout<<"\nEnter Keyword:";
  cin>>temp->k;
  cout<<"\nEnter Meaning:";
  cin>>temp->m;

  temp->left = NULL;
  temp->right = NULL;

  if(root == NULL)
  {
   root = temp;
  }
  else
  {
   insert(root, temp);
  }
  cout<<"\nDo u want to add more (y=1/n=0):";
  cin>>ch;
 }
 while(ch == 1);

}

void dict ::  insert(node * root,node *temp)
{
 if(strcmp (temp->k, root->k) < 0 )
 {
  if(root->left == NULL)
   root->left = temp;
  else
   insert(root->left,temp);
 }
 else
 { if(root->right == NULL)
   root->right = temp;
  else
   insert(root->right,temp);
 }

}

void dict:: disp(node * root)
{
 if( root != NULL)
 {
  disp(root->left);
  cout<<"\n Key Word :"<<root->k;
  cout<<"\t Meaning :"<<root->m;
  disp(root->right);
 }
}

int dict :: search(node * root,char k[20])
{
 int c=0;
 while(root != NULL)
 {
  c++;
  if(strcmp (k,root->k) == 0)
  {
   cout<<"\nNo of Comparisons:"<<c;
   return 1;
  }
  if(strcmp (k, root->k) < 0)
   root = root->left;
  if(strcmp (k, root->k) > 0)
   root = root->right;
 }

 return -1;
}
int dict :: update(node * root,char k[20])
{
 while(root != NULL)
 {
  if(strcmp (k,root->k) == 0)
  {
   cout<<"\nEnter New Meaning ofKeyword"<<root->k;
   cin>>root->m;
   return 1;
  }
  if(strcmp (k, root->k) < 0)
   root = root->left;
  if(strcmp (k, root->k) > 0)
   root = root->right;
 }
 return -1;
}
node* dict :: del(node * root,char k[20])
{
 node *temp;

 if(root == NULL)
 {
  cout<<"\nElement No Found";
  return root;
 }

 if (strcmp(k,root->k) < 0)
 {
  root->left = del(root->left, k);
  return root;
 }
 if (strcmp(k,root->k) > 0)
 {
   root->right = del(root->right, k);
   return root;
 }

 if (root->right==NULL&&root->left==NULL)
 {
  temp = root;
  delete temp;
  return NULL;
  }
  if(root->right==NULL)
  {
  temp = root;
  root = root->left;
  delete temp;
  return root;
  }
  else if(root->left==NULL)
  {
  temp = root;
  root = root->right;
  delete temp;
  return root;
  }
  temp = min(root->right);
  strcpy(root->k,temp->k);
  root->right = del(root->right, temp->k);
  return root;

}

node * dict :: min(node *q)
{
 while(q->left != NULL)
 {
  q = q->left;
 }
 return q;
}



int main()
{
 int ch;
 dict d;
 d.root = NULL;


 do
 {
  cout<<"\nMenu\n1.Create\n2.Disp\n3.Search\n4.Update\n5.Delete\nEnter Ur CH:";
  cin>>ch;

  switch(ch)
  {
case 1: d.create();
  break;
case 2: if(d.root == NULL)
  {
  cout<<"\nNo any Keyword";
  }
  else
  {
  d.disp(d.root);
  }
  break;
case 3: if(d.root == NULL)
 {
  cout<<"\nDictionary is Empty. First add keywords then try again ";
 }
  else
 {

        cout<<"\nEnter Keyword which u want to search:";
  char k[20];
  cin>>k;

  if( d.search(d.root,k) == 1)
  cout<<"\nKeyword Found";
  else
  cout<<"\nKeyword Not Found";
 }
  break;
case 4:
  if(d.root == NULL)
  {
  cout<<"\nDictionary is Empty. First add keywords then try again ";
 }
  else
  {
  cout<<"\nEnter Keyword which meaning  want to update:";
  char k[20];
  cin>>k;
  if(d.update(d.root,k) == 1)
  cout<<"\nMeaning Updated";
  else
  cout<<"\nMeaning Not Found";
  }
  break;
case 5:
  if(d.root == NULL)
  {
  cout<<"\nDictionary is Empty. First add keywords then try again ";
  }
  else
  {
  cout<<"\nEnter Keyword which u want to delete:";
  char k[20];
  cin>>k;
  if(d.root == NULL)
  {
  cout<<"\nNo any Keyword";
  }
  else
  {
  d.root = d.del(d.root,k);
    }
   }
  }
 }
 while(ch<=5);
 return 0;

}

For given expression eg. a-b*c-d/e+f construct inorder sequence and traverse it using postorder traversal(non recursive).


#include<iostream>
#include<stack>
using namespace std;
//////////////////////////////////////////////////////////////
// //
//   Name : Vivek S. Sharma //
// Title : BST Expression Conversion //
// //
// //
//////////////////////////////////////////////////////////////
class Btree{
typedef struct node{
int data;
struct node * right ,*left;
}node;

public :
node * root,*temp;
Btree(){
root=new node;
root=NULL;
}
void create(){
temp=new node;

cout<<"\nEnter the data : ";
cin>>temp->data;
temp->left=temp->right=NULL;
if(root==NULL){
root=temp;
}
else{
insert(root,temp);
}
}

void insert(node * root, node * temp){
char ch;
cout<<"\nDo u want to enter "<<temp->data<<" as left or rright child of "<<root->data<<" : ";
cin>>ch;
if(ch=='l'){
if(root->left==NULL){
root->left=temp;
}
else
insert(root->left,temp);
}
else{
if(root->right==NULL)
root->right=temp;
else
insert(root->right,temp);
}

}
void postOrder_recursive(node * root){
if(root!=NULL){
postOrder_recursive(root->left);
postOrder_recursive(root->right);
cout<<"\t "<<root->data;
}
}
void postOrder_nonRecursive(node* root){
if(!root)
{
cout<<"\nEmpty";
return;
}
stack<node *> s;
stack<node*> p;
s.push(root);
while(!s.empty()){
node * cur=s.top();
p.push(cur);
s.pop();
if(cur->left)
s.push(cur->left);
if(cur->right)
s.push(cur->right);
}
while(!p.empty()){
cout<<"\t"<<p.top()->data;
p.pop(); }
}
//Extra Part : Inorder
void InOrder_nonRecursive(node* root){
if(!root)
{
cout<<"\nEmpty";
return;
}
stack<node *> s;
stack<node*> p;

while(true){
while(root!=NULL){
//cout<<"\t "<<root->data;
s.push(root);
root=root->left;
}
if(s.empty())
return;
root=s.top();
s.pop();

cout<<"\t "<<root->data;
root=root->right;
}
}
//Extra Part : PreOrder
void PreOrder_nonRecursive(node* root){
if(!root)
{
cout<<"\nEmpty";
return;
}
stack<node *> s;
stack<node*> p;

while(true){
while(root!=NULL){
cout<<"\t "<<root->data;
s.push(root);
root=root->left;
}
if(s.empty())
return;
root=s.top();
s.pop();

//cout<<"\t "<<root->data;
root=root->right;
}
}
void display(node* root, int space){

if(root==NULL)
return ;
space +=3;
display(root->right,space);
cout<<"\n";
for(int i=3; i<=space ; i++){
cout<<"  ";}
cout<<root->data<<"\n";
display(root->left,space);
}

};
int main(){
Btree b;
int ch;
do{
b.create();
cout<<"\nDo u wnat to insert more elements :1/0 ";
cin>>ch;

}while(ch!=0);
b.display(b.root,0);
cout<<"\nRecursive Post Order : ";
b.postOrder_recursive(b.root);
cout<<"\nNon Recursive Post Order : ";
b.postOrder_nonRecursive(b.root);
cout<<"\nNon Recursive In Order : ";
b.InOrder_nonRecursive(b.root);
cout<<"\nNon Recursive Pre Order : ";
b.PreOrder_nonRecursive(b.root);
return 0;
}

Wednesday, 22 November 2017

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.

#include<iostream>
using namespace std;
#define size 20
class stack{
char stk[size];
int top;
public:
stack(){
top=-1;
}
void push(char x){
stk[++top]=x;
}
char pop(){
char temp=stk[top];
top--;
return temp;
}
bool empty(){
if(top==-1)
return true;
else return false;
}
char tops(){
char ch;
ch=stk[top];
return ch;
}


};
int main(){
stack st;
string s;
char ch, temp;
int i=0;
cout<<"\nEnter the expression : ";
cin>>s;
if(s[0]==')' || s[0]==']' || s[0]=='}')
cout<<"\nInvalid";
else{
while(s[i]!='\0'){
ch=s[i];
switch(ch){
case '(' : st.push(ch);break;
case '{' : st.push(ch);break;
case '[' : st.push(ch);break;
case ')' : if(st.tops()=='(')temp=st.pop();break;
case '}' : if(st.tops()=='{')temp=st.pop();break;
case ']' : if(st.tops()=='[')temp=st.pop();break;
}
i++;
}
if(st.empty())
cout<<"\nValid ";
else
cout<<"\nInvalid";
}

return 0;
}