C++学习笔记--模板
来源:互联网 发布:天龙八部男生发色数据 编辑:程序博客网 时间:2024/05/17 20:28
// stacktp.h -- a stack template
#ifndef STACKTP_H_#define STACKTP_H_template <class Type>class Stack{private:enum {MAX = 10};Type items[MAX];int top;// index for top stack itempublic:Stack();bool isempty();bool isfull();bool push(const Type & item);bool pop(Type & item);};template <class Type>Stack<Type>::Stack(){top = 0;}template <class Type>bool Stack<Type>::isempty(){return top == 0;}template <class Type>bool Stack<Type>::isfull(){return top == MAX;}template <class Type>bool Stack<Type>::push(const Type & item){if (top < MAX){items[top++] = item;return true;}elsereturn false;}template <class Type>bool Stack<Type>::pop(Type & item){if (top > 0){item = items[--top];return true;}elsereturn false;}#endif// stacktem.cpp -- testing teh template stack class
#include <iostream>#include <string>#include <cctype>#include "stacktp.h"using namespace std;int main(){Stack<string> st;char ch;string 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))continue;switch (ch){case 'A':case 'a':cout << "Enter a PO number to add: ";cin >> po;if (st.isfull())cout << "stack already full\n";elsest.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;}
输出:
wang@wang:~/c++$ ./a.out
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
A
Enter a PO number to add: red911porsche
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P
PO #red911porsche popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P
stack already empty
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
Q
Bye
0 0
- C++Template学习笔记之函数模板
- (C/C++学习笔记)函数模板加强
- C++学习笔记--模板
- C++学习笔记--模板
- c++/c学习笔记--补遗
- C++/C学习笔记--(8)
- (C/C++学习笔记)函数模板遇上函数重载
- (C/C++学习笔记)函数模板的深入理解
- 【C++】学习笔记四十三——函数模板
- HeadFirst设计模式学习笔记5-模板模式
- C++学习笔记6--高级强制类型转换 命名空间和模块化编程 C预处理器 链接和作用域 函数模板 类模板 内联模板 容器和算法
- C++template ;模板学习
- Xdoclet学习笔记(模板)
- xsl学习笔记-模板
- 模板学习笔记(一)
- 模板学习笔记
- mako模板学习笔记
- D3D学习笔记:模板
- SVN的分支、主干合并的使用说明
- 设计程序,用一个二维数组存放5个学生的4门功课的考试成绩,求每个学生的平均成绩。
- 网页设计中的“骨骼”运用技巧
- bs.find()和bs.findAll()的区别,print()函数的格式化
- Qt图形视图框架(一)
- C++学习笔记--模板
- 详细探究Spark的shuffle实现
- 文件批量重命名
- 如何去掉google map上导航和GPS按钮
- java中的equals理解
- maven install时报错
- dev模板配置
- 对比Windows Phone与iOS、Android开发的不同[转]
- 省市三级联动