一个题目关于类的定义,抽象数据类型Stack,数字字符输入的纠错,指针的使用。

下面是一个结构:
编写一个程序,它从栈中添加和删除customer结构。每次customer结构被删除时,其payment的值都被加入到总数中,并报告总数。

struct customer
{
	char fullname[35];
	double payment;
};

使用类来表示抽象数据类型(ADT)。

栈的特征:
先进后出,后进先出(LIFO),top=0,添加数据是向上叠加。删除数据时,向下删除。

可执行的操作:
建立空栈
数据添加到栈顶(压入push)
删除数据(弹出pop)
查看栈是否已满
查看栈是否为空

//Stack.h
#ifndef STACK_H_
#define STACK_H_

static double sum = 0;  //静态全局变量,在同文件夹中可访问,只能定义一次,其他文件引用时使用extern
struct customer   		//声明结构
{
	char fullname[35];
	double payment;
};

typedef customer Item; 

class Stack          //Stack结构的声明
{
private:
	enum {MAX = 3}; //equivalently, static const int MAX = 10; 表示生命周期为类的常量
	Item m_items[MAX];
	int m_top;
public:
	Stack();  //默认构造函数
	bool isempty() const;  //const 指类函数不会改变类成员变量
	bool isfull() const;
	void push(const Item* item); //使用指针,使用引用&亦可
	Item pop(Item* item);
};

#endif
//Stack.cpp
#include "Stack.h"
Stack::Stack()	  //定义构造函数 此处没用将所有成员变量初始化
{
	m_top = 0;
}

bool Stack::isempty() const //栈是否为空
{
	return m_top == 0;
}

bool Stack::isfull() const //栈是否已满
{
	return m_top == MAX;
}

void Stack::push(const Item* item) //将数据压入栈顶
{
	if (m_top < MAX)
	{
		m_items[m_top++] = *item;
	}
	else        //可选
	{
	
	}
}

Item Stack::pop(Item* item)  //将栈顶数据弹出
{
	if (m_top > 0)
	{
		*item = m_items[--m_top];
		return *item;
	}
	else
	{

	}
}
//main.cpp
#include <iostream>
#include "Stack.h"
#include <cstring> //for strcpy()
using std::cout;
using std::cin;
using std::endl;

extern double sum; //引用声明sum
customer* creat_customer(); //声明一个函数,建立顾客,返回顾客结构的指针

int main()
{
	bool active = true;
	Stack stack; //利用默认构造函数建立一个Stack类的实例
	char choice; //存储用户的选择
	customer* p = NULL; //创建并初始化顾客类的指针
	
	cout << "enter 'a' or 'A' to add itme, and 'p' or 'P' to pop item, enter 'q' or 'Q' to quit" << endl;
	while ((cin >> choice) && (toupper(choice) != 'Q')) 
	{
		while (cin.get() != '\n')   //清除多余的输入(清除缓存区)
		{
			continue;
		}
		
		if (!isalpha(choice))  //当输入非字母时,进行提醒,并重新输入
		{
			cout << "please check your input is a litter!" << endl;
			continue;
		}

		switch (choice)  //选择项
		{
			case 'a':
			case 'A':
			{
				if (stack.isfull())   //判断Stack是否已满
				{
					cout << "the stack is already full!" << endl;
				}
				else    //还有空位时,创建一个顾客,报告顾客的姓名以及花费的金额,再将结构添加入栈中,随后这个顾客被delete
				{
					cout << "creat a customer..." << endl;
					p = creat_customer();

					cout << p->fullname << "\t" << p->payment << "\t" << sum << endl;
					stack.push(p);
					delete p;
				}
				break;
			}

			case 'p':
			case 'P':
			{
				if (stack.isempty())  //判断Stack是否为空
				{
					cout << "the stack is empty! can not pop a customer." << endl;
				}
				else //非空则new一个顾客结构的空间盛放Stack中的顾客结构的数据,将花费金额加入总数,进行报告,并删除结构。
				{ 
					p = new customer;
					*p = stack.pop(p);
					sum += p -> payment;
					
					cout << p->fullname << "\t" << p->payment << "\t" << sum << endl;
					
					delete p;
					cout << "ok!" << endl;
				}
				break;
			}
			
			default: //未按要求输入时发出提示
			{
				cout << "check your input!" << endl;
			}
		}
		cout << "enter 'a' or 'A' to add itme, and 'p' or 'P' to pop item, enter 'q' or 'Q' to quit" << endl;
	}
	
	cout << "the sum of payment is " << sum << endl; //报告最后的总数
	cout << "the end" << endl;
	
	system("pause");
	return 0;
}

customer* creat_customer() //定义创建顾客类并返回顾客类指针的函数
{
	customer* p = new customer;
	
	cout << "enter the name of the costomer: ";
	char name[35];
	cin.getline(name, 35);
	strcpy(p -> fullname, name);
	
	cout << "record the payment of " << p -> fullname << " : ";
	double pay;
	cin >> pay;
	while (!cin)      //输入数字的纠错机制
	{
		cin.clear();
		while (cin.get() != '\n')
		{
			continue;
		}
		cout << "please check your input is a number! " << endl;
		cout << "record the payment of " << p->fullname << " : ";
		cin >> pay;
	}
	p -> payment = pay;
	
	return p;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值