第十章編程練習(5)

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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章