模拟售货员的行为
使用堆栈的后进先出的出现。
要吃飯去了
先上代碼
呵呵
實現的思路再添加
stack.h
#ifndef STACK_H_
#define 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
{
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;
}
else
return false;
}
bool Stack::pop(Item &item)
{
if(top>0)
{
item=items[--top];
return true;
}
else
return false;
}
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;
}
else
return false;
}
bool Stack::pop(Item &item)
{
if(top>0)
{
item=items[--top];
return true;
}
else
return false;
}
//stacker.h
#include <iostream>
#include <cctype>
#include <cctype>
#include "stack.h"
int main()
{
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 a purchase order,\n"
<<"P to process a PO,or Q to Quit.\n";
}
cout<<"Bye\n";
return 0;
}
break;
}
cout<<"Please enter A to add a purchase order,\n"
<<"P to process a PO,or Q to Quit.\n";
}
cout<<"Bye\n";
return 0;
}
是完整的代碼 大家可以調試 吃飯去先
转载于:https://blog.51cto.com/suxiangjuner/152693