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;
}
#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;
}
相关文章推荐
- jsoncpp在VS2010下的简单使用方法 不需编译
- JZOJ5405. 【NOIP2017提高A组模拟10.10】Permutation
- c语言自动删除cpp文件中的注释 使用代码删除注释【Linux、mac下】
- configure: error: C++ preprocessor "/lib/cpp" fails sanity check
- Ubuntu 10.10安装SSH
- [第一波模拟\day1\T2]{分班}(divide.cpp)
- Android Camera CameraHal.cpp 分析
- C++ STL源码剖析——memory.cpp
- 微软2014实习生及秋令营技术类职位在线测试-1.StringReorder.cpp
- AI 第一步 : Mac 10.10 下 Java1.8 + Eclipse Oxygen + TensorFlow 配置及Demo
- stdafx.h、stdafx.cpp作用
- 基于开源库jsoncpp的json字符串解析
- CPP thread 2
- JNI的cpp中往Eclipse输出LOG
- C++ preprocessor "/lib/cpp" fails sanity check 解决方法
- CFaderWnd.cpp
- JsonCpp使用
- VS Qt编译错误,找不到qrc_*.cpp
- 编译实验2-在C++中读写文件(testfile.cpp)
- VS2010打开.h后.cpp自动关闭或打开.cpp后.h自动关闭问题