实验内容:
用顺序存储结构,实现教材定义的栈的基本操作,提供数制转换功能,将输入的十进制整数转换成二进制。
实验步骤:
(1)按照实验要求编写代码,构造顺序栈。
(2)输入验收用例,验证其输出结果。
#include <iostream>
#include <cmath>
using namespace std;
#define MAXSIZE 100
typedef int SElemType;//定义顺序栈的存储结构
typedef struct{SElemType *base;//栈底指针SElemType *top;//栈顶指针int stacksize;//栈的容量
}SqStack;void Help()
{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 << "10.创建并输入栈元素" << endl;cout << "11.十进制转二进制" << endl;cout << "12.退出"<< endl;
}//初始化为空栈
int InitStack(SqStack&S)
{S.base=new SElemType[MAXSIZE];if(!S.base){cout<<"存储空间分配失败"<<endl;exit(0);//异常退出}S.top=S.base;//top初始为base,空栈S.stacksize=MAXSIZE;//stacksize置为栈的最大容量MAXSIZEreturn 1;
}//销毁栈
int DestroyStack(SqStack &S)
{if(S.base){delete[]S.base;S.stacksize=0;S.base=S.top=NULL;}return 1;
}//将栈置空
int ClearStack(SqStack &S)
{if(S.base) S.top=S.base;cout<<"栈已置空"<<endl;return 1;
}//判断栈是否为空栈
bool StackEmpty(SqStack S)
{if(S.top==S.base) return true;else return false;
}//返回栈的长度
int StackLength(SqStack S)
{return S.top-S.base;
}//求栈顶元素
int GetTop(SqStack S,SElemType&e)
{if(S.top==S.base) return 0;e=*(S.top-1);return 1;
}//插入元素,并使其成为栈顶元素
int Push(SqStack &S,SElemType e)
{if(S.top-S.base==S.stacksize)return 0;*S.top++=e;return 1;
}//删除栈顶元素,并返回其值
int Pop(SqStack &S,SElemType&e)
{if(S.top==S.base)return 0;e=*--S.top;return 1;
}//输出栈内元素
void print(SqStack S)
{while(S.top!=S.base){cout<<*(S.top-1);S.top--;}cout<<endl;
}//创建并输入栈元素
void BulidStack(SqStack &S,int n)
{while(n--){cin>>*S.top;S.top++;}
}//数制转换
void DataChange(SqStack &S,int P) { InitStack(S);int x = 0;do {x = P % 2;P = P / 2;Push(S, x);} while (P != 0);print(S);DestroyStack(S);}bool IsLegal(SqStack S)
{if(abs(S.top-S.base)>MAXSIZE)return false;else if(!S.base)return false;elsereturn true;
}
int main()
{SqStack S;SElemType e;int n,m;Help();while(1){cout<<"请输入你的选择:";cin>>n;if(n==1){InitStack(S);cout<<"初始化成功"<<endl;}else if(n==2){if(!IsLegal(S)){cout<<"请先将栈初始化"<<endl;}else{DestroyStack(S);cout<<"销毁成功"<<endl;}}else if(n==3){if(!IsLegal(S)){cout<<"请先将栈初始化"<<endl;}else{ClearStack(S);}}else if(n==4){if(!IsLegal(S)){cout<<"请先将栈初始化"<<endl;}else{if(StackEmpty(S))cout<<"栈为空"<<endl;elsecout<<"栈非空"<<endl;}}else if(n==5){if(!IsLegal(S)){cout<<"请先将栈初始化"<<endl;}else{cout<<"栈的长度为:"<<StackLength(S)<<endl;}}else if(n==6){if(!IsLegal(S)){cout<<"请先将栈初始化"<<endl;}else{if(GetTop(S,e))cout<<"栈顶元素为:"<<e<<endl;elsecout<<"操作失败!"<<endl;}}else if(n==7){if(!IsLegal(S)){cout<<"请先将栈初始化"<<endl;}else{cout<<"请输入一个元素:";cin>>e;if(Push(S,e))cout<<"操作成功!"<<endl;elsecout<<"操作失败!"<<endl;}}else if(n==8){if(!IsLegal(S)){cout<<"请先将栈初始化"<<endl;}else{if(Pop(S,e))cout<<"栈顶元素为:"<<e<<endl;elsecout<<"操作失败!"<<endl;}}else if(n==9){if(!IsLegal(S)){cout<<"请先将栈初始化"<<endl;}print(S);cout<<endl;}else if(n==10){cout<<"请输入要初始化的元素个数:";cin>>m;cout<<"请输入"<<m<<"个元素:"<<endl;BulidStack(S,m);}else if(n==11){cout<<"请输入想要转换的十进制数:";int p;cin>>p;DataChange(S,p);}else if(n==12){return 0;}else{cout<<"操作有误"<<endl;}}return 0;
}