Tuesday 6 September 2016

C++ program using stack to check whether given expression is well parenthesized or not.


Group C: Assignment No: 01 (SPPU Syllabus Assignment No:24)

Problem Statement:
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.


Program Code:

//============================================================================
// Name        : wellformness.cpp
// Author      : Nitin Shivale
// Version     : 1
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
#define size 10

class stackexp
{
    int top;
    char stk[size];
public:
    stackexp()
    {
     top=-1;
    }
    void push(char);
    char pop();
    int isfull();
    int isempty();
};

void stackexp::push(char x)
{
    top=top+1;
    stk[top]=x;
}

char stackexp::pop()
{
    char s;
    s=stk[top];
    top=top-1;
    return s;
}

int stackexp::isfull()
{
    if(top==size)
        return 1;
    else
        return 0;
}

int stackexp::isempty()
{
    if(top==-1)
        return 1;
    else
        return 0;
}

int main()
{
    stackexp s1;
    char exp[20],ch;
    int i=0;
    cout << "\n\t!!Well Formness of Parenthesis..!!!!" << endl; // prints !!!Hello World!!!
    cout<<"\nEnter the expression to check whether it is in well form or not :  ";
    cin>>exp;
    if((exp[0]==')')||(exp[0]==']')||(exp[0]=='}'))
    {
        cout<<"\n Invalid Expression.....\n";
        return 0;
    }
    else
    {
        while(exp[i]!='\0')
        {
            ch=exp[i];
            switch(ch)
            {
            case '(':s1.push(ch);break;
            case '[':s1.push(ch);break;
            case '{':s1.push(ch);break;
            case ')':s1.pop();break;
            case ']':s1.pop();break;
            case '}':s1.pop();break;
            }
            i=i+1;
        }
    }
    if(s1.isempty())
    {
        cout<<"\nExpression is well parenthesis...\n";
    }
    else
    {
        cout<<"\nSorry !!! Invalid Expression or not in well parenthesized....\n";
    }
    return 0;
}

/******************* output   *****************************************************

  !!Well Formness of Parenthesis..!!!!

Enter the expression to check whether it is in well form or not :  (m<(n[8]+0))

Expression is well parenthesis...


!!Well Formness of Parenthesis..!!!!

Enter the expression to check whether it is in well form or not :  )(m+n)*(a-b)

 Invalid Expression.....



     !!Well Formness of Parenthesis..!!!!

Enter the expression to check whether it is in well form or not :  (m+b(n[8)/2+3)

Sorry !!! Invalid Expression or not in well parenthesized....


*********************************************************************************/


data structures and algorithms Web Developer

5 comments:

  1. program should be modified it gives wrong output if exp is ::(m+b(n[8)/2+3))...output should be not well parenthesized but it is giving well parenthesized

    ReplyDelete
  2. Thank you so much for finding out the bug.

    ReplyDelete
  3. please explain all the program from starting

    ReplyDelete
  4. U have used inbuilt functions or not?

    ReplyDelete