#include "stdio.h"
#define OK 1
#define ERROR 0
#define Stacksize 100
typedef int status;
typedef int elemtype; //元素为整型
#define SIZE 100
typedef struct {
elemtype *base;
elemtype *top;
int stacksize;
}Sqstack;
//2、在第1题的基础上,填写完整下面的初始化函数。
int initStack(Sqstack &S)
{
S.base=new elemtype[SIZE];
if(!S.base) return ERROR;
S.top = S.base;
S.stacksize = SIZE;
return S.stacksize; //返回栈的大小
}
//3、请将下列的元素进栈函数补充完整。
status push(Sqstack &S, elemtype e)
{
if(S.top-S.base>=S.stacksize) return ERROR;
*S.top++=e;
return OK;
}
//4、请将下列的出栈函数补充完整。
elemtype pop(Sqstack &S, elemtype &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
//5、请将下列取栈顶元素的函数补充完整。
elemtype gettop(Sqstack &S, elemtype &e)
{
if(S.top==S.base) return ERROR;
e = *(S.top-1);
return OK;
}
//进制转换
void conversion(int N,int r){
Sqstack s;
int e=0;
initStack(s);
while(N){
e=N%r;
push(s,e);
N = N / r;
}
while(pop(s,e))
{
printf("%d",e);
}
}
//6、输入下列的main函数,完成对上述函数的调用。
main()
{
int e,x,L,i;
Sqstack Sa;
L=initStack(Sa);
printf("顺序栈长度是%d\n",L);
for(i=1;i<=5;i++)
{printf("input stack data to e=");
scanf("%d",&e);
push(Sa, e);
}
printf("三次取栈顶元素top=\n");
for(i=1;i<=3;i++)
{ gettop(Sa, e);
printf("e=%d\n",e);
}
printf("元素出栈:\n");
for(i=1;i<=2;i++)
{pop(Sa, e);
printf("e3=%d\n",e);}
gettop(Sa,e);
printf("此时栈顶元素为:%d\n",e);
printf("输入10进制数n和要转换的进制d,格式:?"); //输入10进制数n和要转换的进制d,格式:(n,d)
scanf("%d %d",&e,&x);
conversion(e,x);
}