Thread: problem Stack in c++

  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
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    << !! 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, 03:55 AM
  3. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 03: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, 10:33 AM