第十章编程练习(5)
2016-02-03 15:18
344 查看
ff.h
#pragma once #ifndef ff_H_ #define ff_H_ struct customer { char fullname[35]; double payment; }; typedef customer 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);//满返回false,否则返回true.进栈 bool pop(Item & item);//出栈 }; #endif
function.cpp
#include "ff.h" double sum = 0; 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]; sum += items[top].payment; return true; } else return false; }
main.cpp
#include <iostream> #include "ff.h" #include <cstdlib> #include <cctype> extern double sum; using namespace std; int main() { Stack st; char ch; customer po; cout << "Please enter A to add a purchase order,\n" << "P to precess 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 fullname,PO payment to add: "; cin.getline(po.fullname, 35); cin >> po.payment; 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 << "Num of payment: " << sum << endl; } break; } cout << "Please enter A to add a purchase order,\n" << "P to precess a PO,or Q to quit.\n"; } cout << "Bye!\n"; system("pause"); return 0; }
相关文章推荐
- spring ioc原理 和 java反射 体会
- C# 给PDF添加图片背景
- python内置模块(二)
- C++开发工程师面试题
- php常用mysql操作函数
- 关于struts2.3.24.1所必须的jar包
- webdriver 关于元素的常用操作 python版本
- Spring Ioc就是对java反射及读取xml文件的使用的封装
- python 注意事项(—)
- c++多线程在异常环境下的等待
- 蓝桥杯结果填空题训练部分题目代码
- C++语法 面试题 带答案
- Spring 中注入bean的properties配置文件位置问题解决
- 序列化java
- C++ Primer 学习笔记——顺序容器(1)
- java-string与byte互换
- [Java基础]多线程之并发性以及解决方法
- 跟我学 Spring 3
- C# 给PDF添加图片背景
- Win8系统无法打开应用商店且提示“服务器错误”及错误代码0x80072EE7的解决方法