为什么执行scanf_s输入数据时是错的,没有给输入直接就结束了程序
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define STACK_INT_SIZE 10 /*存储空间初始分配量*/
#define STACKINCREMENT 5 /*存储空间分配增量*/
typedef int ElemType; /*定义元素的类型*/
typedef struct
{
ElemType* base;
ElemType* top;
int stacksize; /*当前已分配的存储空间*/
}SqStack;
int InitStack(SqStack* S); /*构造空栈*/
int push(SqStack* S, ElemType e); /*入栈*/
int Pop(SqStack* S, ElemType* e); /*出栈*/
int CreateStack(SqStack* S); /*创建栈*/
void PrintStack(SqStack* S); /*出栈并输出栈中元素*/
int InitStack(SqStack* S)
{
S->base = (ElemType*)malloc(STACK_INT_SIZE * sizeof(ElemType));
if (!S->base) return ERROR;
S->top = S->base; //将top初始化为base
S->stacksize = STACK_INT_SIZE;
return OK;
}
int Push(SqStack* S, ElemType e) //插入元素e为新的栈顶元素
{
if (S->top - S->base == S->stacksize) return ERROR;
*S->top++ = e;
return OK;
}
int Pop(SqStack* S, ElemType* e) //弹出栈顶元素,用e返回
{
if (S->top ==S->base ) return ERROR;
*e = *--S->top;
return OK;
}
int CreateStack(SqStack* S)
{
int e;
if (InitStack(S))
printf("Init Success!\n");
else
{
printf("Init Fail!\n");
return ERROR;
}
printf("input data:(Terminated by inputing a character)\n");
while (scanf_s("%d", &e))
Push(S, e);
return OK;
}
void PrintStack(SqStack* S)
{
ElemType e;
while (Pop(S, &e))
printf("%3d", e);
}
//编写一个十进制转换为二进制的数制转换算法函数(要求利用栈来实现),并验证其正确性。
void conver(SqStack *S,int N)
{
ElemType e;
InitStack(S);
while (N)
{
Push(S,N%2);
N = N / 2;
}
while (S->top!=S->base)
{
Pop(S, &e);
printf("3%d",e);
}
}
int main()
{
SqStack ss;
int n;
printf("\n1-createStack\n");
CreateStack(&ss);
printf("\n2-Pop&Print\n");
PrintStack(&ss);
printf("\n100转换后的数:\n");
scanf_s("%d",&n);
conver(&ss, n);
return 0;
}