Stack Implementation Code

Discussion in 'C++' started by AshifToday, May 22, 2005.

  1. AshifToday

    AshifToday Guest

    Expression Evaluation in Post-Fix Notation Using Stack
    =====================

    /*
    This program has been made by Ashif Zubair.
    Roll # 1468, B.Sc.(Hons.) in C.S.
    ALL RIGHTS ARE RESERVED TO ASHIF ZUBAIR.
    PROGRAM OBJECTIVE :-

    */
    #include <iostream.h>
    #include <conio.h>

    class stack
    {
    private:
    char *sspace;
    int tos;
    public:
    stack();
    int ssize;
    void push(char);
    char pop();
    int isempty();
    int isfull();
    }stk;

    /* Class Functions */
    stack::stack()
    {
    clrscr();
    tos = -1;
    cout<<"Enter Stack Size : ";
    cin>>ssize;
    clrscr();
    }

    void stack::push(char ch)
    {
    if(!isfull())
    {
    tos++;
    sspace[tos] = ch;
    }
    }

    char stack::pop()
    {
    if(!isempty())
    {
    char ch;
    ch = sspace[tos--];
    return ch;
    }
    }

    int stack::isfull()
    {
    if(tos > ssize)
    return 1;
    else
    return 0;
    }

    int stack::isempty()
    {
    if(tos == -1)
    return 1;
    else
    return 0;
    }
    /* Class Functions Finished */


    /* Main Function */

    void main (void)
    {
    clrscr();
    char exp[100];
    char get=NULL;
    char temp=NULL;
    int i=0;
    int open = 0;
    int close = 0;
    int high = 0;
    cout<<"Enter Your Expression :\nPress x Key When Finished\n\t\t\t";
    while(get != 'x'|| get != 'X')
    {
    get = getche();
    switch(get)
    {
    case '0' :
    exp[i++] = get;
    break;
    case '1' :
    exp[i++] = get;
    break;
    case '2' :
    exp[i++] = get;
    break;
    case '3' :
    exp[i++] = get;
    break;
    case '4' :
    exp[i++] = get;
    break;
    case '5' :
    exp[i++] = get;
    break;
    case '6' :
    exp[i++] = get;
    break;
    case '7' :
    exp[i++] = get;
    break;
    case '8' :
    exp[i++] = get;
    break;
    case '9' :
    exp[i++] = get;
    break;
    case '(' :
    open++;
    stk.push(get);
    break;
    case ')' :
    close++;
    temp = stk.pop();
    while(temp != '(')
    {
    exp[i++] = temp;
    temp = stk.pop();
    }

    case '^' :
    stk.push(get);
    high++;
    break;
    case '/' :
    stk.push(get);
    high++;
    break;
    case '*' :
    stk.push(get);
    high++;
    break;
    case '%' :
    stk.push(get);
    high++;
    break;

    case '+' :
    if(high == 0)
    stk.push(get);
    else
    while(!stk.isempty())
    {
    temp = stk.pop();
    if(temp != ')' || temp != '(')
    exp[i++] = temp;
    }
    high = 0;
    break;
    case '-' :
    if(high == 0)
    stk.push(get);
    else
    while(!stk.isempty())
    {
    temp = stk.pop();
    if(temp != ')' || temp != '(')
    exp[i++] = temp;
    }
    high = 0;
    break;
    default:
    cout<<"\n\nUnknow Operand/Operator\nOnly 0-9 & *,/,%,+,- Keys
    Allowed\n\nContinue To input : ";
    }
    cout<<"\n\n"<<exp[i-1]<<"\t"<<i<<endl;
    }
    clrscr();
    cout<<"Your Given Equaation in POST-FIX Representation.\n\n";
    for(int loop = 0;loop<i; loop++)
    {
    cout<<exp[loop];
    }
    getch();
    }

    /*=======================*/
    AshifToday, May 22, 2005
    #1
    1. Advertising

  2. AshifToday

    Guest

    I assume we can just guess as to what your question is... Have you
    even tried to compile this code yet?
    , May 22, 2005
    #2
    1. Advertising

  3. AshifToday

    AshifToday Guest

    its a functional code, i've posted the code jsut to share it, and any
    enhancement i could get from the viewrs comments!
    AshifToday, May 23, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surinder Singh
    Replies:
    1
    Views:
    1,195
    Richard Bos
    Dec 20, 2007
  2. Casey Hawthorne
    Replies:
    3
    Views:
    1,082
    Flash Gordon
    Nov 1, 2009
  3. Debajit Adhikary
    Replies:
    36
    Views:
    2,285
    Andre Kaufmann
    Feb 10, 2011
  4. Sam Roberts
    Replies:
    1
    Views:
    224
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    259
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page