Friday, February 10, 2012

Program Stack2

#include <cstdlib>
#include <iostream>

using namespace std;

class stack1{
      public:
         stack1();
        void push(char);
        void top();
        void pop();
        void cetak();
     private:
        int top =-1;
        char stack[2];
        char x;

};

void stack1::push(char x){
     cout<<"masukkan satu karakter:";
     cin>>x;
    
     top++;
  stack[top]=x;
  }

void stack1::pop(){
     if (top<0){
      cout<<"stack kosong"<<endl;
      return;
  }
  x=stack[top];
  top++;
 
  cout<<"karakter yang pop adalah"<<x<<endl;
}

void stack1::cetak(){
     if (top<0)
     {
       cout<<"stack kosong"<<endl;
       return;
       }
    
  int i=0;
  for (i=top; i>=0; i--)
       cout<<stack[i]<<endl;
    }
   
int main(int argc, char *argv[])
{
    int input;
    cout<<"masukkan pilihan :"<<endl;
    cout<<"\tpush = 1"<<endl;
    cout<<"\tpop = 2"<<endl;
    cout<<"\tcetak = 3"<<endl;
    cout<<"\tquit = 4"<<endl;
    while(true)
    {
               cout<<"\nmasukkan pilihan :";
               cin>>input;
              
               if(input==1)
               {push();}
               else if(input==2)
               {pop();}
               else if(input==3)
               {cetak();}
               else if(input==4)
               {break;}
               else
               {cout<<"perintah '"<<input<<"'tidak dikenal"<<endl;}
               }
    system("PAUSE");
    return EXIT_SUCCESS;
}


download program antrian

download aplikasi perpustakaan

No comments:

Post a Comment