#include<stdio.h>
#include <stdlib.h>
#define overflow -2
#define OK 1
#define ERROR 0
#define stack_inti_size 100
#define stackincrement 10
typedef sElemtype;
sElemtype e;
typedef int status;
typedef struct {
sElemtype *base;
sElemtype *top;
int stacksize;
}sqstack;
status clearstack(sqstack &s)
{
s.base = s.top = NULL;
return OK;
}
status empty(sqstack s)
{
if(s.base==s.top)
return ERROR;
else
return OK;
}
status sqstacklength(sqstack s)
{
int i;
i=s.top - s.base;
return i;
}
status destroystack(sqstack &s)
{
if (s.base==s.top) {
return ERROR;
}
for(;s.top>=s.base;s.top--)
{
s.top = --s.top;
free(s.top);
}
s.base = s.top =NULL;
return OK;
}
status Initstack(sqstack &s)//构造空栈S
{
s.base=(sElemtype*)malloc(stack_inti_size * sizeof(sElemtype));
if(!s.base) exit(overflow);
s.base=s.top;
s.stacksize=stack_inti_size;
return OK;
}
status get_top(sqstack s,sElemtype &e)//若栈不为空,返回栈顶元素
{
if (s.base == s.top) {
return ERROR;
}
e=*(s.top - 1);
return OK;
}
status Push(sqstack &s,sElemtype e)//插入元素e为新的栈顶元素
{
if (s.top - s.base >= s.stacksize) {
s.base = (sElemtype*)realloc(s.base,(s.stacksize+stackincrement)*sizeof(sElemtype));
if (!s.base) {
exit (overflow);
}
s.top = s.base + s.stacksize;
s.stacksize += stackincrement;
}
*s.top++ = e;
return OK;
}
status Pop(sqstack &s,sElemtype &e)//栈不为空,删除栈顶元素
{
if (s.base ==s.top) {
return ERROR;
}
e = --*s.top;
return OK;
}
void conversion(sqstack &s)
{
//sElemtype e;
int Ten;//输入的十进制数
int x;//要转换成的进制数
printf("输入十进制非负数:");
scanf("%d",&Ten);
printf("输入要转换成的进制数:");
scanf("%d",&x);
while(Ten) {
Push(s,Ten%x);
Ten=Ten/x;
}
while(empty(s)) {
Pop(s,e);
printf("%d",e);
}
}
void editzhan(sqstack &s)//利用字符栈s,从终端接受一行并传递给调用过程的数据区
{
char ch;
if(Initstack(s))
printf("初始化栈成功\n");
else
printf("初始化栈failed!");
ch=getchar();
while (ch!=EOF) {
while (ch!=EOF && ch!='\n') {
switch(ch) {
case '#':
Pop(s,e);
break;
case '@':
clearstack(s);
break;
default: Push(s,ch);
}
ch=getchar();
}
clearstack(s);
if (ch!=EOF) {
ch=getchar();
}
destroystack(s);
}
}
void main()
{
sqstack s;
//sElemtype e;
int chioce;
if(Initstack(s))
printf("初始化栈成功\n");
else
printf("初始化栈failed!");
printf("选择进入的程序!\n0.退出程序\n1.进制数之间的转换\n2..返回栈顶元素\n3.插入元素E为新的栈顶元素\n4.删除栈顶元素\n5.行编辑\n6.返回栈的长度\n7.销毁栈\n");
scanf("%d",&chioce);
while(chioce!=0)
{
switch(chioce)
{
case 0:
printf("退出成功\n");
break;
case 1:
//printf("input number\n");
//scanf("%d",&chioce);
conversion(s);
break;
case 2:
if(get_top(s,e)==ERROR)
printf("failed!\n");
else
printf("栈顶元素%d被成功返回\n",e);
break;
case 3:
printf("input the insertnumber\n");
scanf("%d",&e);
if(Push(s,e)==ERROR)
printf("false!\n");
else
printf("插入成功\n");
break;
case 4:
if(Pop(s,e)==ERROR)
printf("false!\n");
else
printf("栈顶元素%d被成功删除\n",e);
break;
case 5:
editzhan(s);
break;
case 6:
sqstacklength(s);
printf("长度为%d\n",e);
break;
case 7:
if(destroystack(s))
printf("销毁成功\n");
else
printf("失败!\n");
break;
default:printf("不可用命令\n");
}
printf("选择进入的程序!\n0.退出程序\n1.进制数之间的转换\n2.返回栈顶元素\n3.插入元素E为新的栈顶元素\n4.删除栈顶元素\n5.行编辑\n6.返回栈的长度\n7.销毁栈\n");
scanf("%d",&chioce);
}
}