您的位置:首页 > 编程语言 > C语言/C++

cpp 10.10

2017-01-04 15:14 148 查看
stack.h

#pragma once
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);

};



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;
}


stacker.cpp
#include<iostream>
#include<cctype>
#include"stack.h"

int main()
{
using namespace std;
Stack st;
char ch;
unsigned long po;
cout << "Please enter A to add 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";
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: