一本很好的C++学习的丛书!初学者必看的
源代码在线查看: stkoptr1.cpp
软件大小: |
53266 K |
上传用户: |
car_bike2000000 |
|
|
关键词: |
初学者
|
下载地址: |
免注册下载 普通下载
|
|
// stkoptr1.cpp -- test stack of pointers
#include
#include // for rand(), srand()
#include // for time()
#include "stcktp1.h"
const int Num = 10;
int main()
{
std::srand(std::time(0)); // randomize rand()
std::cout int stacksize;
std::cin >> stacksize;
// create an empty stack with stacksize slots
Stack st(stacksize);
// in basket
const char * in[Num] = {
" 1: Hank Gilgamesh", " 2: Kiki Ishtar",
" 3: Betty Rocker", " 4: Ian Flagranti",
" 5: Wolfgang Kibble", " 6: Portia Koop",
" 7: Joy Almondo", " 8: Xaverie Paprika",
" 9: Juan Moore", "10: Misha Mache"
};
// out basket
const char * out[Num];
int processed = 0;
int nextin = 0;
while (processed < Num)
{
if (st.isempty())
st.push(in[nextin++]);
else if (st.isfull())
st.pop(out[processed++]);
else if (std::rand() % 2 && nextin < Num) // 50-50 chance
st.push(in[nextin++]);
else
st.pop(out[processed++]);
}
for (int i = 0; i < Num; i++)
std::cout
std::cout return 0;
}