| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
这两段代码都是多文件的。 
第一段 
//头文件list.h 
#ifndef LIST_H_ 
#define LIST_H_ 
typedef int Item; 
class Plorg 
{ 
      private: 
              char m_name[20]; 
              int top; 
              public: 
                     void show()const; 
                     void visit(void(*pf)(Item&,Plorg&),Item lines); 
                     bool isfull()const; 
                     bool nofull()const; 
                     Plorg(const char* name="Plorga",Item PO = 50); 
                     const int Get()const; 
                     static const int MAX = 10; 
                     Item items[MAX]; 
                     void SetTop(Item command); 
      }; 
 
#endif 
//list.cpp实现函数功能 
#include "list.h" 
#include <cstring> 
#include <iostream> 
 
Plorg::Plorg(const char* name,Item PO) 
{ 
                   top = 0; 
                   strncpy(m_name,name,19); 
                   m_name[19] = '\0'; 
                   for(int i = 0;i<MAX;++i) 
                           if(top<MAX) 
                                      items[top++] = PO; 
                                      else 
                                      break; 
                   top = 0; 
                   }  
 
void Plorg::show()const 
{ 
     using std::cout; 
     using std::endl; 
     cout<<"Name:"<<m_name<<endl 
         <<"CI:\n"; 
         for(int i = 0;i<MAX;++i) 
         { 
                 if(i!=MAX-1)  
         cout<<items[i]<<","; 
         else 
         cout<<items[i]<<endl;; 
         } 
 } 
  
bool Plorg::isfull()const 
{ 
     return top==MAX; 
 }  
 
bool Plorg::nofull()const 
{ 
     return top<MAX; 
 } 
 void Plorg::visit(void(*pf)(Item&,Plorg&),Item lines) 
{ 
    (*pf)(lines,*this); 
 } 
const int Plorg::Get()const 
{ 
    return top; 
} 
void Plorg::SetTop(Item command) 
{ 
     if(command == 0) 
     ++top; 
     else if(command == 1) 
     --top; 
     else if(command == 2) 
     top=0; 
 } 
//main.cpp尚未完成 
#include <cstdlib> 
#include <iostream> 
#include "list.h" 
 
#define PUSH 0 
#define POP 1 
#define CLEAR 2 
 
void push(Item& item); 
void pop(Item& item); 
 
int main(int argc, char *argv[]) 
{ 
    using namespace std; 
    Plorg pg; 
    int PO; 
     
    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 
void push(Item& item,Plorg& pg) 
{ 
     int top; 
     top=pg.Get(); 
     if(pg.nofull()) 
     { 
     pg.items[top++] = item; 
     SetTop(PUSH); 
     } 
 } 
 void pop(Item& item,Plorg& pg) 
 { 
     int top; 
     top = pg.Get(); 
     if(pg.nofull()) 
     { 
     item = pg.items[--top];  
     SetTop(POP); 
     }  
  } 
第二段代码: 
//头文件stack.h 
#ifndef STACK_H_ 
#define STACK_H_ 
 
 
typedef unsigned long Item; 
 
class Stack 
{ 
 private: 
      enum{MAX=10}; 
      Item items[MAX]; 
      int top; 
 public: 
      Stack(); 
      bool isempty()const; 
      bool isfull()const; 
      bool push(const Item & item); 
      bool pop(Item & item); 
} 
#endif 
//类实现部分Stack.cpp 
#include "stack.h" 
Stack::Stack() 
{ 
              top=0; 
              } 
               
              bool Stack::isempty()const 
              { 
                   return top==0; 
               } 
                
               bool Stack::isfull()const 
               { 
                    return top==MAX; 
                } 
                bool Stack::push(const Item & item) 
                { 
                     if(top<MAX) 
                     { 
                                items[top++]=item; 
                                return true; 
                                } 
                                return false; 
                 } 
                 bool Stack::pop(Item & item) 
                 { 
                      if(top>0) 
                      { 
                               item = items[--top]; 
                               return true; 
                               } 
                               return false; 
                  }  
//最终实现部分main.cpp 
#include <iostream> 
#include "stack.h" 
#include <cctype> 
#include <cstdlib> 
 
int main(int argc, char *argv[]) 
{ 
    using namespace std; 
    Stack st; 
    char ch; 
    unsigned long po; 
    cout<<"Please enter A to add a purchase order,\n" 
    "P to process a PO,or Q to quit.\n"; 
     while(cin>>ch&&toupper(ch)!='Q') 
     { 
       while(cin.get()!='\n') 
            continue; 
       if(!isalpha(ch)) 
        { 
        cout<<"\a"; 
        continue; 
        } 
      switch(ch) 
     {  
      case 'A': 
      case 'a':cout<<"Enter a PO number to add:"; 
               cin>>po; 
                if(st.isfull()) 
                 cout<<"stack already full\n"; 
                 else 
                 st.push(po); 
                 break; 
      case 'p': 
      case 'P':if(st.isempty()) 
                cout<<"stack already empty\n"; 
                 else{ 
                 st.pop(po); 
                 cout<<"PO #"<<po<<"popped\n"; 
                  }  
                break;  
        } 
          cout<<"Please enter A to add purchase order, \n" 
                "P to process a PO,or Q to quit.\n"; 
     } 
    cout<<"Bye!\n"; 
    system("PAUSE"); 
    return EXIT_SUCCESS; 
} |   
 
 
 
 |