博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++primer plus第十章第5题
阅读量:4566 次
发布时间:2019-06-08

本文共 2023 字,大约阅读时间需要 6 分钟。

头文件:

//stack.h#ifndef _STACK_H#define _STACK_Hstruct customer{	char fullname[35];	double payment;};typedef customer Item ;class Stack{private:	enum {MAX=10};	Item items[MAX];	int top;public:	Stack();	virtual ~Stack() {};	bool isempty() const;	bool isfull() const;	bool push(const Item & item);	bool pop(Item & item);};#endif // !_STACK_H
.cpp文件:

//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;}
main:

//main.cpp#include 
#include "stack.h"void get_customer(customer & cu);using namespace std;int main(){ Stack st; char ch; customer temp; double payment = 0; cout << "Enter A or a to push a customer,\n" << "P or p to pop a customer,and Q or q to quit!" << endl; while ((cin >> ch) && (ch != 'q') && (ch != 'Q')) { while (cin.get() != '\n') continue; if ((toupper(ch)!= 'A') && (toupper(ch) != 'P' )) { cout << "Please enter A,por Q!" << endl; continue; } switch (ch) { case 'A': case 'a': if (st.isfull()) cout << "The stack is already full!" << endl; else get_customer(temp); st.push(temp); break; case 'P': case 'p': if (st.isempty()) cout << "The stack is empty!" << endl; else { st.pop(temp); payment += temp.payment; cout << temp.payment << "is poped!"; cout << "payment now total $" << payment << endl; } break; } cout << "Enter A or a to push a customer,\n" << "P or p to pop a customer,and Q or q to quit!" << endl; } cout << "Done!" << endl; return 0;}void get_customer(customer & cu){ cout << "Enter customer name :"; cin.getline(cu.fullname, 35); cout << "Enter customer payment :"; cin >> cu.payment; while (cin.get() != '\n') continue;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/yangquanhui/p/4937513.html

你可能感兴趣的文章
大数据等最核心的关键技术:32个算法
查看>>
Maven多模块项目搭建
查看>>
redis列表list
查看>>
雷林鹏分享: C# 简介
查看>>
ORA-12505: TNS: 监听程序当前无法识别连接描述符中所给出的SID等错误解决方法
查看>>
实用类-<Math类常用>
查看>>
构建之法阅读笔记之四
查看>>
10.15习题2
查看>>
Windows Server 2008 R2 备份与恢复详细实例
查看>>
Ubuntu上kubeadm安装Kubernetes集群
查看>>
关于java学习中的一些易错点(基础篇)
查看>>
MFC的多国语言界面的实现
查看>>
四则运算个人项目 最终版
查看>>
java线程系列---java5中的线程池
查看>>
SQL表连接
查看>>
新秀系列C/C++经典问题(四)
查看>>
memset函数具体说明
查看>>
经常使用的android弹出对话框
查看>>
确保新站自身站点设计的合理性的六大注意点
查看>>
promise
查看>>