代码改变世界

stack

2008-02-01 14:05  Virus-BeautyCode  阅读(408)  评论(0编辑  收藏  举报
typedef   int   stack_entry;    
  enum   Error_Code1{success1,overflow1,underflow1};    
  const   int   maxstack=10;  
  #include<iostream>  
  using   namespace   std;  
   
  class   stack  
  {  
        public:  
          stack();  
          bool   empty()const;  
          void   clear();  
          bool   full()const;  
          int   size()const;  
          Error_Code1   pop();  
          Error_Code1   top(stack_entry   &item)const;  
          Error_Code1   push(const   stack_entry   &item);  
          stack&   operator=(stack   &source);  
          friend   void   copy_stack(stack   &dest,stack   &source);  
  private:  
                  int   count;  
                  stack_entry     entry[maxstack];          
  };  
  stack   tstack;  
   
  stack&   stack::operator=(   stack   &source)  
  {  
           
        for   (int   i   =   0;   i<count;   i++)  
        {  
              tstack.entry[i]   =   entry[i];  
              tstack.count         =   count;  
        }  
        return   tstack;              
  }    
   
  void     copy_stack(stack   &dest,stack   &source)  
  {  
          dest.count   =   source.count;  
          for   (   int   i   =   0;   i<source.count;   i++)  
          {  
                  dest.entry[i]   =   source.entry[i];        
          }    
  }      
   
   
  Error_Code1   stack::push(const   stack_entry   &item)  
   
  {  
      Error_Code1   outcome   =   success1;  
      if(   count>maxstack)  
         
          outcome   =   overflow1;  
         
      else  
         
        entry[count++]   =   item;  
         
        return   outcome;          
   
  }  
   
  Error_Code1   stack::pop()  
  {  
          Error_Code1   outcome   =   success1;  
          if   (count   ==   0)  
              outcome   =   underflow1;  
          else  
              --count;  
              return   outcome;  
  }  
   
  Error_Code1   stack::top(stack_entry   &item)const  
  {  
          Error_Code1   outcome=success1;  
          if   (count   ==   0)  
             
                  outcome   =   underflow1;  
          else  
                 
            item   =   entry[count-1];  
           
          return   outcome;  
               
  }  
   
  void   stack::clear()  
  {  
          count   =   0;  
          for   (int   i   =   0;   i<maxstack;   i++)  
                {  
                        entry[i]   =   0;  
                }          
  }  
   
   
  bool   stack::full()const  
  {        
  bool   outcome   =   true;  
          if   (count   ==   maxstack)  
                outcome   =   false;  
          return   outcome;          
  }  
   
  int   stack::size()const  
  {  
          return   count-1;  
  }  
           
  bool   stack::empty()const  
  {  
          bool   outcome   =   true;  
          if   (count   ==   0)  
                outcome   =   false;  
          return   outcome;  
                   
  }  
   
  stack::stack()  
  {  
          count   =   0;  
  }  
   
  int   main()  
  {  
           
          int   n;  
          stack   istack,teststack;  
          int   i;  
          int   item[maxstack];  
   
          cout<<"請輸入入棧整數個數(<10):"<<endl;  
          cin>>n;  
          for   (   i   =   0;   i<n;   i++   )  
            {  
                        cin>>item[i];  
                        istack.push(item[i]);  
                       
            }  
            cout<<endl<<endl;  
             
            while   (istack.empty())  
              {    
                      for   (int   j=0;j<n;j++)  
                      {  
                          Error_Code1   Errcode;  
                            Errcode   =   istack.top(item[j]);  
                          cout<<item[j]<<endl;  
                          istack.pop();  
                      }          
              }  
              system("pause");  
              return   0;  
  }