@TOC@11/19 c++ primer plus10.7章 10.10, 10.11, 10.12
先来10.10的
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long 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);
bool pop(Item&item);
};
#endif
然后10.11
// stdafx.cpp : 只包括标准包含文件的源文件
//
s
a
f
e
p
r
o
j
e
c
t
n
a
m
e
safeprojectname
safeprojectname.pch 将作为预编译标头
// stdafx.obj 将包含预编译类型信息
#include “stdafx.h”
#include “stack.h”
Stack::Stack()
{
top = 0;
}
bool Stack::isempty() const
{ return top0;
}
bool Stack::isfull() const
{
return topMAX;
}
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;
}
;
// TODO: 在 STDAFX.H 中引用任何所需的附加头文件,
//而不是在此文件中引用
最后10.12
// 11月19.cpp: 定义控制台应用程序的入口点。
//
#include “stdafx.h”
#include “stack.h”
#include “ctype.h”
#include “iostream”
using namespace std;
int main()
{
using namespace std;
Stack st;
char ch;
unsigned long po;
cout << “please enter Ato add a purchase order,\n”
<< “P to process a po,or 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 number to add:”;
cin >> po;
if (st.isfull())
cout << “stack already full\n”;
else
st.push(po);
break;
case’p’:
case’P’:if (st.isempty())
cout << “stack aready empty\n”;
else {
st.pop(po);
cout << "PO " << po << “popped\n”;
}
break;
}
cout << "please enter A to add a purchase order ,\n "
<< “P to process a po ,or Q to quit.\n”;
}
cout << “Bye \n”;
return 0;
}
好啦,今天的节目就到这里了,相信大家都能看懂