//Vc++6.0实现,问题代码在choiceType类processstack(int number)里
//seqstack.h
//以下为栈的定义部分,如果用工程建立程序,可以另外启用文件seqstack.h
enum returninfo{success,fail,overflow,nooverflow,underflow,nounderflow};//定义返回信息清单
/*
模板顺序栈类seqstack的定义说明
*/
template <class Type>
class seqstack
{
public:
seqstack();//创建一个空栈
seqstack(int size);//创建一个可以容纳size个元素的栈
~seqstack();//回收一个栈
bool create(int size);//实际创建一个可以容纳size个元素的栈
returninfo destroy();//销毁一个栈
returninfo isempty() const;//确定栈是否已空
returninfo isfull() const;//确定栈是否已满
bool push(Type &item);//数据进栈
returninfo pushfull();//进栈时是否栈满
bool pop();//数据出栈
bool pop(Type &item);//数据出栈并返回出栈前的栈顶
returninfo popempty();//出栈时是否栈空
returninfo getpop(Type &item);//取出当前栈顶数据
returninfo information();//栈空间使用情况
returninfo display();//显示栈的所有元素
private:
Type *stackspace;//栈空间的具体实现,在下面通过动态申请空间建立数组
int stacksize;//栈的大小,当为0时,栈没有创建空间
int top;//栈顶位置,当为-1时,栈为空
int count;//已进栈数据个数
};
/*
定义一个实现顺序栈功能的菜单处理类interfacebase
*/
template <class Type>
class interfacebase
{
private:
seqstack<Type> seqstackonface;
int choice;
public:
void clearscreen(void);//清屏
void showmenu(void);//显示菜单函数
int userchoice(void);//用户的选项
int judgechoice();
returninfo processmenu(int menuchoice);//菜单函数
};
/*
定义一个选择数据类型的菜单处理类choiceType
*/
template <class Type>
class choiceType
{
private:
interfacebase<Type> interfacebaseonface;
public:
void clearscreen(void);//清屏
void showmenu(void);//显示菜单函数
int userchoice(void);//用户的选项
void processstack(int number);
returninfo processmenu(int menuchoice);//菜单函数
};
//seqstack.cpp
//功能:顺序栈的功能演示
#include "seqstack.h"
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>
//以下为栈的实现部分,如果用工程建立程序,可以另外启用文件seqstack.cpp
/*
seqstack类的实现部分
*/
template <class Type>
seqstack<Type>::seqstack()//创建一个空栈
{
stackspace=NULL;//初始化,空间起始地址为空
stacksize=0;//栈的大小,初始值为0,表示目前还没有建立
top=-1;//栈顶位置,当为-1时,栈为空
count=0;//已进栈数据个数为零
}
template <class Type>
seqstack<Type>::seqstack(int size)//创建一个可以容纳size个元素的栈
{
stackspace=NULL;//初始化,空间起始地址为空
stacksize=0;//栈的大小,初始值为0,表示目前还没有建立
top=-1;//栈顶位置,当为-1时,栈为空
count=0;//已进栈数据个数为零
create(size);//开始用size大小为参数建立栈的空间
}
template <class Type>
seqstack<Type>::~seqstack()//回收一个栈
{
destroy();//析构函数,释放相关空间
}
template <class Type>
bool seqstack<Type>::create(int size)//实际创建一个可以容纳size个元素的栈
{
if(stacksize)//栈已经存在,不能再创建
return false;
if(size<=0)//size的值必须大于零
return false;
stackspace=new Type[size];//申请动态数组空间
if(!stackspace)//没有申请到存储空间,创建栈不成功
return false;
stacksize=size;//申请到存储空间,创建栈成功,大小为size
top=-1;//栈刚建立成功时,栈顶指针为-1
count=0;//已进栈数据个数为零
return true;
}
template <class Type>
returninfo seqstack<Type>::destroy()//销毁一个栈
{
if(isempty()==fail)//确定栈是否被创建,没有创建视为空
return fail;
if(stackspace)
delete [] stackspace;//释放栈的空间,还给操作系统
stackspace=NULL;//空间起始地址恢复为空
stacksize=0;//栈的大小恢复为初始值0,表示目前还没有建立
top=-1;//栈顶指针恢复为-1
count=0;//已进栈数据个数为零
return success;
}
template <class Type>
returninfo seqstack<Type>::isempty() const//确定栈是否已空
{
if(!stacksize)//确定栈是否被创建,没有创建视为空
return fail;
return top==-1?underflow:nounderflow;//根据栈顶指针的位置返回是否为空
}
template <class Type>
returninfo seqstack<Type>::isfull() const//确定栈是否已满
{
if(!stacksize)//确定栈是否被创建,没有创建视为空
return fail;
return top==stacksize-1?overflow:nooverflow;//根据栈顶指针的位置返回是否为满
}
template <class Type>
bool seqstack<Type>::push(Type &item)//数据进栈
{
stackspace[++top]=item;//实际进栈的操作,具体分为两步:先产生地址,后插入数据
count++;//进栈数据个数加1
return true;//返回成功标志
}
template <class Type>
returninfo seqstack<Type>::pushfull()//进栈时是否栈满
{
if(isempty()==fail)
return fail;
if(isfull()==overflow)//如果栈空间已经为满,则不能执行进栈操作
return overflow;
else
return success;
}
template <class Type>
bool seqstack<Type>::pop()//数据出栈
{
top--;//执行出栈操作,栈顶指针减1
count--;//进栈数据个数减1
return true;//返回成功标志
}
template <class Type>
bool seqstack<Type>::pop(Type &item)//数据出栈并返回出栈前的栈顶
{
item=stackspace[top--];//执行出栈操作,把栈顶数据保存起来返回,栈顶指针减1
count--;//进栈数据个数减1
return true;//返回成功标志
}
template <class Type>
returninfo seqstack<Type>::popempty()//出栈时是否栈空
{
if(isempty()==fail)
return fail;
if(isempty()==underflow)//如果栈空间已经为空,则不能执行出栈操作
return underflow;
else
return success;
}
template <class Type>
returninfo seqstack<Type>::getpop(Type &item)//取出当前栈顶数据
{
if(isempty()==fail)//如果栈空间已经为空,则不能执行出栈操作
return fail;
if(isempty()==underflow)
return underflow;
item=stackspace[top];//把栈顶数据保存起来返回
return success;//返回成功标志
}
template <class Type>
returninfo seqstack<Type>::information()//栈空间使用情况
{
if(isempty()==fail)//当栈中没有数据时则放弃显示所有数据
return fail;
else
{
cout<<"栈总空间:"<<stacksize<<"个"<<endl<<"栈已用空间:"<<count<<"个"<<endl<<"栈剩余空间:"<<stacksize-count<<"个";
return success;
}
}
template <class Type>
returninfo seqstack<Type>::display()//显示栈的所有元素
{
if(isempty()==fail)//当栈中没有数据时则放弃显示所有数据
return fail;
else//当栈中有数据时则显示所有数据
{
cout<<"目前栈中的内容是:";
cout<<"栈底■";
for(int i=0;i<=top;i++)//栈的空间从0到top指向的位置
cout<<stackspace[i]<<" ";//输出每一个数据,同时间隔一个空格
cout<<"←top栈顶"<<endl;
return success;
}
}
/*
interfacebase类的实现部分
*/
template <class Type>
void interfacebase<Type>::clearscreen(void)
{
system("cls");
}
template <class Type>
void interfacebase<Type>::showmenu(void)
{
cout<<"顺序栈基本功能菜单"<<endl;
cout<<"=================="<<endl;
cout<<"1.创建一个栈"<<endl;
cout<<"2.销毁一个栈"<<endl;
cout<<"3.数据进栈(仅限整数)"<<endl;
cout<<"4.数据出栈"<<endl;
cout<<"5.显示栈中全部数据"<<endl;
cout<<"6.读取栈顶数据"<<endl;
cout<<"7.判断是否空栈"<<endl;
cout<<"8.判断是否满栈"<<endl;
cout<<"9.栈空间使用情况"<<endl;
cout<<"0.返回"<<endl;
cout<<"=================="<<endl;
}
template <class Type>
int interfacebase<Type>::userchoice(void)
{
int menuchoice;
cout<<"请输入您的选择:";
cin>>menuchoice;
choice=menuchoice;
return menuchoice;
}
template <class Type>
int interfacebase<Type>::judgechoice()
{
return choice;
}
template <class Type>
returninfo interfacebase<Type>::processmenu(int menuchoice)
{
int size,returnvalue;
Type item;
switch(menuchoice)//根据用户的选择进行相应的操作
{
case 1:
cout<<"请输入栈的大小:";
cin>>size;
seqstackonface.create(size);
break;
case 2:
returnvalue=seqstackonface.destroy();
if(returnvalue==fail)
cout<<"栈尚未建立!"<<endl;
else
cout<<"栈已销毁!"<<endl;
break;
case 3:
returnvalue=seqstackonface.pushfull();
if(returnvalue==fail)
cout<<"栈尚未建立!"<<endl;
else if(returnvalue==overflow)
cout<<"栈已满,数据无法进栈!"<<endl;
else
{
cout<<"请输入进栈数据:";
cin>>item;
seqstackonface.push(item);
cout<<"数据已进栈!"<<endl;
}
break;
case 4:
returnvalue=seqstackonface.popempty();
if(returnvalue==fail)
cout<<"栈尚未建立!"<<endl;
else if(returnvalue==underflow)
cout<<"栈已空,无数据出栈!"<<endl;
else
{
seqstackonface.pop(item);
cout<<"出栈数据:"<<item<<endl;
cout<<"数据已出栈!"<<endl;
}
break;
case 5:
returnvalue=seqstackonface.display();
if(returnvalue==fail)
cout<<"栈尚未建立!"<<endl;
else
cout<<"已遍历栈中全部数据!"<<endl;
break;
case 6:
returnvalue=seqstackonface.getpop(item);
if(returnvalue==fail)
cout<<"栈尚未建立!"<<endl;
else if(returnvalue==underflow)
cout<<"栈已空,无栈顶数据!"<<endl;
else
cout<<"当前栈顶数据为:"<<item<<endl;
break;
case 7:
returnvalue=seqstackonface.isempty();
if(returnvalue==underflow)
cout<<"栈已空!"<<endl;
else if(returnvalue==nounderflow)
cout<<"栈未空!"<<endl;
else
cout<<"栈尚未建立!"<<endl;
break;
case 8:
returnvalue=seqstackonface.isfull();
if(returnvalue==overflow)
cout<<"栈已满!"<<endl;
else if(returnvalue==nooverflow)
cout<<"栈未满!"<<endl;
else
cout<<"栈尚未建立!"<<endl;
break;
case 9:
returnvalue=seqstackonface.information();
if(returnvalue==fail)
cout<<"栈尚未建立!"<<endl;
else
cout<<endl;
break;
case 0:
break;
default:
cout<<"对不起,您输入的功能编号有错!请重新输入!!!"<<endl;
break;
}
return success;
}
/*
choiceType类的实现部分
*/
template <class Type>
void choiceType<Type>::clearscreen(void)
{
system("cls");
}
template <class Type>
void choiceType<Type>::showmenu(void)
{
cout<<"顺序栈数据类型菜单"<<endl;
cout<<"=================="<<endl;
cout<<"1.int"<<endl;
cout<<"2.float"<<endl;
cout<<"3.double"<<endl;
cout<<"0.退出"<<endl;
cout<<"=================="<<endl;
}
template <class Type>
int choiceType<Type>::userchoice(void)
{
int menuchoice;
cout<<"根据需要进栈的数据类型,"<<endl<<"请输入您的选择:";
cin>>menuchoice;
return menuchoice;
}
template <class Type>
void choiceType<Type>::processstack(int number)
{
int menuchoice;//定义变量,菜单选单项的选择
interfacebase<Type> interfacenow;
if(number==1)
interfacebase<int> interfacenow;
else if(number==2)
interfacebase<float> interfacenow;
else if(number==3)
interfacebase<double> interfacenow;
else
cout<<endl;
interfacenow.clearscreen();//清屏
while(interfacenow.judgechoice())
{
interfacenow.showmenu();//显示菜单
menuchoice=interfacenow.userchoice();//获取用户的选择
interfacenow.processmenu(menuchoice);//处理用户的选择
system("pause");//暂停
interfacenow.clearscreen();//清屏
}
}
template <class Type>
returninfo choiceType<Type>::processmenu(int menuchoice)
{
switch(menuchoice)//根据用户的选择进行相应的操作
{
case 1:
processstack(1);
break;
case 2:
processstack(2);
break;
case 3:
processstack(3);
break;
case 0:
exit(0);
default:
cout<<"对不起,您输入的功能编号有错!请重新输入!!!"<<endl;
system("pause");//暂停
break;
}
return success;
}
//main.cpp
#include "seqstack.cpp"
#include <windows.h>
/*
程序主入口
*/
void main(void)
{
int menuchoice;//定义变量,菜单选单项的选择
choiceType<int> choiceTypenow;
system("color f0");//修改屏幕的背景色和字的颜色
choiceTypenow.clearscreen();//清屏
while(1)//永真循环
{
choiceTypenow.showmenu();//显示菜单
menuchoice=choiceTypenow.userchoice();//获取用户的选择
choiceTypenow.processmenu(menuchoice);//处理用户的选择
choiceTypenow.clearscreen();//清屏
}
}//主函数结束