您的位置:首页 > 编程语言

第十章编程练习(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: