problem Stack in c++

This is a discussion on problem Stack in c++ within the C++ Programming forums, part of the General Programming Boards category; write aprogram in c++ to read the tokens in one at a time ,if it is integer,push it on stack, ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    UAE
    Posts
    1

    Question problem Stack in c++

    write aprogram in c++ to read the tokens in one at a time ,if it is
    integer,push it on stack, if it is binary operator, pop two elements
    from the stack, apply the operator to the two elements and push the result back on the stack??
    i need your help to me to solve this question and thanks for every body help me in solutoin


    and this my tired

    Sorry but i new !!
    but was my tried simpleton ??
    Code:
    #include<iostream.h>
    const int size=5;
    void push(int[],int&,int);
    void pop(int[],int&,int&);
    void main()
    {int stack[size],item,top=0,i;
    for(i=0;i<size;i++)
    {cout<<"Enter item"<<endl;
    cin>>item;
    push(stack,top,item);
    }
    for(i=0;i<size;i++)
    {pop(stack,top,item);
    int x; // x from type integer
    if(item==x)
    push(stack,top,item);
    else
    pop(stack,top,item);
    }
    for(i=0;i<size;i++)
    {push(stack,top,item);
    pop(stack,top,item);
    }
    }
    
    void push(int stack[size],int&top,int item)
    {if(top<size)
    top++;
    stack[top]=item;
    else
    cout<<"stack is FULL"<<endl;
    }
    void pop(int stack[top],int&top,int&item)
    {if(top>0)
    {item=stack[top];
    top--;
    } 
    else
    cout<<"the stack is EMPTY"<<endl;
    }
    thank s
    Last edited by Salem; 08-04-2010 at 09:40 AM. Reason: Remove font size abuse, and added [code][/code] tags (for all the good that it does without identation)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    What have you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    << !! Posting Code? Read this First !! >>
    Get rid of void main: SourceForge.net: Void main - cpwiki
    Start putting the names of your arguments in your prorotypes: SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    You should only have one loop in main :
    Code:
    While there is still input
    {
       get next input
       if (input is operator)
       {
          pop two items
          perform operation on those items
          push result
       }
       else if input is number
          push input
       else
          error
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack using linked list problem
    By effa in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2009, 12:10 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  3. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 02:05 AM
  4. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21