|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#ifndef STCK_T
#define STCK_T
template <class Type>
class Stack
{
private:
enum{SIZE=10};
int stacksize;
Type *items;
int top;
public:
explicit Stack(int ss=SIZE);
Stack(const Stack &st);
~Stack(){delete [] items;}
bool isempty(){return top==0;}
bool isfull(){return top==stacksize;}
bool push(const Type &item);
bool pop(Type&itme);
Stack&operator=(const Stack&st);
};
template <class Type>
Stack<Type>::Stack(int ss):stacksize(ss),top(0)
{
items=new Type[stacksize];
}
template <class Type>
Stack<Type>::Stack(const Stack&st)
{
stacksize=st.stacksize;
top=st.top;
items=new Type[stacksize];
for(int i=0;i<top;i++)
{
items[i]=st.items[i];
}
}
template <class Type>
bool Stack<Type>::push(const Type &item)
{
std::cout<<"执行push";
if(top<stacksize)
{
items[top++]=item;
return true;
}
else
return false;
}
template <class Type>
bool Stack<Type>::pop(Type &item)
{
if(top>0)
{std::cout<<"执行pop";
item=items[--top];
return true;
}
else
return false;
}
template <class Type>
Stack<Type>&Stack<Type>::operator=(const Stack<Type>&st)
{
if(this==&st)
return *this;
delete [] items;
stacksize=st.stacksize;
top=st.top;
items=new Type[stacksize];
for(int i=0;i<top;i++)
items[i]=st.items;
std::cout<<"赋值运算符。\n";
return *this;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"biaot.h"
const int Num=10;
int main()
{
std::srand(std::time(0));
std::cout<<"please enter stack size";
int stacksize;
std::cin>>stacksize;
Stack<const char *>st(stacksize);
const char* in[Num]={"1:hank gilgamesh","2:kiki ishtar","3:betty rocker","4:lan flagranti","5:wolfgang kibble","6:portia koop","7:joy almondo",
"8:xaverie paprika","9:juan moore","10:misha mache"};
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)
st.push(in[nextin++]);
else
st.pop(out[processed++]);
}
for(int i=0;i<Num;i++)
std::cout<<out[i]<<std::endl;
std::cout<<"bye\n";
std::cin.get();
std::cin.get();
std::cin.get();
return 0;
}
|
|