#include "stdafx.h"
#include"stdlib.h"
#define STACK_INIT_SIZE 100
#define STACKINCREAMENT 10
typedef int elem;
typedef struct {
elem *base;
elem *top;
int stacksize;
}SqStack;
SqStack s;
void InitStack(SqStack * S) {
S->base = (elem *)malloc(STACK_INIT_SIZE * sizeof(elem));//创建一个连续的 长度为100个整形的栈
S->stacksize = STACK_INIT_SIZE;
S->top = S->base;//栈刚开始为空 栈顶指针和栈底指针指向一样
}
void Push(SqStack *S,elem e) {
if (S->top - S->base >= S->stacksize) {//如果长度超出100 重新开辟十个长度连在后面
S->base = (elem *)realloc(S->base, (S->stacksize + STACKINCREAMENT) * sizeof(elem));
if (!S->base) exit(-2);
printf("STACK---PUSH---SUCCESS\n\n");
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREAMENT;
}
*(S->top)=e;//将你想赋的值赋进去
S->top = S->top++;//栈顶指针往后推一个
}
void Pop(SqStack *S,elem *e) {
if (S->top == S->base) {
printf("栈目前为空\n");
exit(0);
}
S->top--;//栈顶指针回退一个
*e = *S->top;//将栈的最后一个值提出来
}
int StackEmpty(SqStack *S) {
if (S->top - S->base == 0) {
printf("栈为空");
return 0;
}
return 1;
}
elem StackLength(SqStack *S) {
return S->top - S->base;
}
void Numerical_conversion(int num,int Decimal) {//数值转换
if (num < 0) {
printf("-");
num = -num;
}//如果数值是负的 先输出一个-
while (num!=0) {
Push(&s,num%Decimal);
num /= Decimal;
}//将余数压栈
int stackLength = StackLength(&s);
for (int i = 0; i < stackLength; i++) {
int num;
Pop(&s, &num);
printf("%d", num);
}//将栈中的数依次弹出
printf("\n");
}
int main()
{
InitStack(&s);
Numerical_conversion(10, 2);
/*for (int i = 1; i < 101; i++) {
Push(&s, i);
}
Push(&s, 1);*/
/*printf("%d\n", StackLength(&s));
int num;
Pop(&s, &num);
printf("%d\n",num);*/
return 0;
}