VS调试的程序,scanf_s会报错。代码加上#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
#define _CRT_SECURE_NO_WARNINGS 1
typedef int ElemType;
typedef struct
{
ElemType* base;
ElemType* top;
int Length;
} SequenceStack;
//定义栈
int InitStack(SequenceStack* ps)
{
ps->base = (int*)malloc(MAXSIZE * sizeof(int));
if (!ps->base)
return ERROR;
ps->top = ps->base;
ps->Length = MAXSIZE;
return OK;
}
//初始化栈
int EmptyStack(SequenceStack* ps)
{
if (ps->base == ps->top)
{
return OK;
}
else
{
return ERROR;
}
}//判断栈是否为空
int GetHead(SequenceStack ps, ElemType* e)
{
if (ps.base == ps.top)
{
return ERROR;
}
else
{
e = --ps.top;
return *e;
}
}//获取栈顶元素
int StackLength(SequenceStack ps, ElemType* len)
{
if (ps.base == ps.top)
{
*len = 0;
return OK;
}
else
{
*len = 1;
while (--ps.top != ps.base)
{
*len = *len + 1;
printf("----");
printf("\n");
}
return OK;
}
}//获取栈的长度
int StackPop(SequenceStack* ps)
{
if (ps->top == ps->base)
{
return ERROR;
}
int e = 0;
while (--ps->top != ps->base || ps->top == ps->base)
{
e = *ps->top;
printf("%d\n", e);
if (ps->top == ps->base)
return OK;
}
}//将栈中元素依次出栈并输出
int Push(SequenceStack* ps, ElemType e)
{
if (ps->top - ps->base == MAXSIZE)
{
return ERROR;
}
*ps->top++ = e;
return OK;
}
int Pop(SequenceStack* ps)
{
if (*ps->base == *ps->top)
{
return ERROR;
}
else
{
--ps->top;
}
return OK;
}
int ten_eight(SequenceStack* ps, ElemType n)
{
int t, i = 1;
t = n % 8;
*ps->top++ = t;
n = n / 8;
while (n > 8)
{
if (n > 8)
{
t = n % 8;
*ps->top++ = t;
i++;
n = n / 8;
}
else
{
break;
}
}
*ps->top++ = n;
while (i >= 0)
{
printf("%d", *--ps->top);
i--;
}
printf("\n");
return OK;
}//进制转化
int main()
{
SequenceStack stack;
int status, head, temp, len = 0, num, e,ten;
InitStack(&stack);
while (1)
{
printf("%-18s\n", "************************");
printf("%-18s", "0、输入零退出程序。");
printf("%5s\n", "*");
printf("%-18s", "1、判断栈是否为空。");
printf("%5s\n", "*");
printf("%-18s", "2、获取栈的头元素。");
printf("%5s\n", "*");
printf("%-18s", "3、获取此栈的长度。");
printf("%5s\n", "*");
printf("%-18s", "4、栈元素依次弹出。");
printf("%5s\n", "*");
printf("%-18s", "5、实现十转八进制。");
printf("%5s\n", "*");
printf("%-18s", "6、将元素压入栈中。");
printf("%5s\n", "*");
printf("%-18s", "7、将元素弹出此栈。");
printf("%5s\n", "*");
printf("%-18s\n", "************************");
printf("你的选择:");
scanf("%d", &num);
printf("\n");
if (num == 0)
{
break;
}
else if (num == 1)
{
if (EmptyStack(&stack))
{
printf("当前栈为空!\n");
continue;
}
else
{
printf("当前栈不为空!\n");
continue;
}
}
else if (num == 2)
{
head = GetHead(stack, &temp);
if (head)
{
printf("当前头元素为:%d\n", head);
continue;
}
else
{
printf("当前栈为空!\n");
continue;
}
}
else if (num == 3)
{
StackLength(stack, &len);
if (len)
{
printf("当前栈的长度为%d\n", len);
continue;
}
else
{
printf("当前栈为空!\n");
continue;
}
}
else if (num == 4) {
StackPop(&stack);
continue;
}
else if (num == 5)
{
scanf("%d", &ten);
ten_eight(&stack,ten);
continue;
}
else if (num == 6)
{
printf("你要压入栈中的数:");
scanf("%d", &e);
if (Push(&stack, e))
{
printf("入栈成功!\n");
continue;
}
else
{
printf("当前栈满!\n");
continue;
}
}
else if (num == 7)
{
if (Pop(&stack))
{
printf("出栈成功!\n");
continue;
}
else
{
printf("当前栈为空!\n");
continue;
}
}
}
}
标签:ps,功能,顺序,return,实现,top,else,int,printf
From: https://www.cnblogs.com/qianyuzz/p/17059912.html