📄 main_sqstack.cpp
字号:
#include"c1.h"
typedef int SElemType;
// c3-1.h 栈的顺序存储表示
#define MAXSTACKSIZE 20 // 存储空间初始分配量
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
char pause;
void handle_choice(int choice);
SqStack S;
// bo3-1.cpp 顺序栈(存储结构由c3-1.h定义)的基本操作(9个)
Status InitStack(SqStack &S)
{ // 构造一个空栈S
S.base=new SElemType[MAXSTACKSIZE];
if(!S.base) exit(OVERFLOW); // 存储分配失败
S.top=S.base;
S.stacksize=MAXSTACKSIZE;
return OK;
}
Status ClearStack(SqStack &S)
{ // 把S置为空栈
S.top=S.base;
return OK;
}
Status StackEmpty(SqStack S)
{ // 若栈S为空栈,则返回TRUE,否则返回FALSE
if(S.top==S.base)
return TRUE;
else
return FALSE;
}
int StackLength(SqStack S)
{ // 返回S的元素个数,即栈的长度
return S.top-S.base;
}
Status GetTop(SqStack S,SElemType &e)
{ // 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
if(S.top>S.base)
{
e=*(S.top-1);
return OK;
}
else
return ERROR;
}
Status Push(SqStack &S,SElemType e)
{ // 插入元素e为新的栈顶元素
if(S.top-S.base>=S.stacksize) // 栈满,追加存储空间
return ERROR;
*(S.top)++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{ // 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
if(S.top==S.base) //栈空
return ERROR;
e=*--S.top;
return OK;
}
Status StackTraverse(SqStack S)
{ // 从栈底到栈顶依次对栈中每个元素调用函数visit()。
// 一旦visit()失败,则操作失败
while(S.top>S.base)
cout<<*S.base++<<'\t';
cout<<endl;
return OK;
}
Status DestroyStack(SqStack &S)
{ // 销毁栈S,S不再存在
delete[] S.base;
S.base=NULL;
S.top=NULL;
S.stacksize=0;
return OK;
}
//十进制到八进制转换
void DectoQ(int N)
{//
SElemType e;
SqStack s;
cout<<"十进制数"<<N<<"的八进制数为:";
InitStack(s);
while(N)
{
Push(s,N%8);
N=N/8;
}//while
while(!StackEmpty(s))
{
Pop(s,e);
cout<<e;
}//while
}//DettoQ
//主函数
int main()
{
system("cls");//执行系统命令cls,清屏
int choice;
do
{//显示主菜单
//system("cls");
cout<<"1-创建一个空的顺栈(长度最长为20)\n";
cout<<"2-元素入栈\n";
cout<<"3-元素出栈\n";
cout<<"4-取栈顶元素\n";
cout<<"5-置栈空\n";
cout<<"6-显示栈中元素\n";
cout<<"7-测试栈长\n";
cout<<"8-十进制到八进制转换\n";
cout<<"9-退出\n";
cout<<"Enter choice:";
cin>>choice;
handle_choice(choice);//Call function to direct flow based on choice
}while(choice!=9);//Repeat menu until user chooses to exit
return 0;
}//end of main function
void handle_choice(int choice) //根据用户选择调用对应处理函数
{ int i,e;
switch(choice)
{
case 1://创建一个空栈
InitStack(S);
break;
case 2://入栈
cout<<"请输入插入元素的值:";
cin>>e;//将创建的链表中数据元素的个数
cout<<endl;
Push(S,e);
cin.get(pause);
system("pause");
break;
case 3://出栈
Pop(S,e);
cout<<"出栈元素为:"<<e;
cout<<endl;
cin.get(pause);
system("pause");
break;
case 4://获取栈顶元素
GetTop(S,e);
cout<<"请输入要查询的元素位置:";
cout<<"栈顶元素为:"<<e;
cout<<endl;
cin.get(pause);
system("pause");
break;
case 5://清空栈
ClearStack(S);
cin.get(pause);
system("pause");
break;
case 6://通过遍历,输出栈中元素
StackTraverse(S);
cout<<endl;
cin.get(pause);
system("pause");
break;
case 7://测试栈长
i=StackLength(S);
cout<<"该栈长为:"<<i<<endl;
cin.get(pause);
system("pause");
break;
case 8://十进制数转换为八进制数
cout<<"请输入要转换的十进制数(>0的正整数):";
cin>>i;
cout<<endl;
if(i<0)
cout<<"输入非法,无法转换"<<endl;
else
DectoQ(i);
cout<<endl;
cin.get(pause);
system("pause");
break;
case 9://退出,销毁栈
DestroyStack(S);
break;
default://If any other(invalid) choice was entered,display error message
cout<<"Invalid choice\n";
break;
}
}//end of function handle_choice
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -