#include <stdio.h>
#define MAXSIZE 20
#define SIZE sizeof(SEQSTACK)
#define LINE_ITEM_COUNT (5) //每行的元素个数
#define PRINT_BOOL "%d " //对bool类型数据的输出的宏定义
#define SCANF_ITEM "%d" //对输入项进行宏定义
#define PRINT_ITEM "%d "
typedef short bool; //定义bool类型
const bool true = 1; //定义bool类型中的真
const bool false = 0; //定义bool类型中的假
typedef int ELEMTYPE; //定义栈中元素的类型
const ELEMTYPE STACK_NULL = -9999; //自定义栈为NULL的值为 -9999
//我们定义栈中的data[0]不存储任何信息,作为栈底的标志
//因此我们知道在我们定义的栈中存在的数据为从1到MAXSIZE-1。即MAXSIZE - 1 个元素。
typedef struct //定义顺序栈
{
ELEMTYPE data[MAXSIZE];
int top;
}SEQSTACK;
SEQSTACK * INITSTACK(); //初始化空栈
bool EMPTY(SEQSTACK *);//判断栈是否是空栈
bool PUSH (SEQSTACK *,ELEMTYPE);//将元素进行压栈,true压栈成功,false压栈失败
ELEMTYPE POP(SEQSTACK *);//将元素进行出栈,如果栈为空,则返回STACK_NULL
int LENGTH(SEQSTACK *); //获取栈的长度
ELEMTYPE GETTOP(SEQSTACK *);//获取栈顶元素,如果栈为空,则返回STACK_NULL
void CLEAR(SEQSTACK *);//将栈进行清空
void menu(); //菜单
void print_tab(); //打印tab键
void print_enter(); //打印回车键
void print_menu_item(char *,char *); //输出菜单项
void print_str(char *); //输出字符
void print_info(char *); //输出相关信息
void print_select_menu(); //输出选择菜单
SEQSTACK * init_stack(); //初始化栈
void is_empty(SEQSTACK *);//判断是否是空栈
bool is_init_stack(SEQSTACK *);//判断栈是否初始化
void stack_push(SEQSTACK *); //进栈操作
void stack_pop(SEQSTACK *);//出栈操作
void stack_gettop(SEQSTACK *); //获取栈顶元素
void stack_clear(SEQSTACK *);//将栈置空
void stack_method(SEQSTACK *,void (* fun)(SEQSTACK *)); //使用指向函数指针对系统进行重构
int main(int argc, char *argv[])
{
SEQSTACK *stack = NULL;
int select_item_value;
menu();
do
{
print_select_menu();
scanf("%d",&select_item_value);
switch(select_item_value)
{
case -1:
break;
case 0:
menu();
break;
case 1:
stack = init_stack();
break;
case 2:
stack_method(stack,is_empty);
break;
case 3:
stack_method(stack,stack_push);
break;
case 4:
stack_method(stack,stack_pop);
break;
case 5:
stack_method(stack,stack_gettop);
break;
case 6:
stack_method(stack,stack_clear);
break;
default:
menu();
break;
}
}while(select_item_value != -1);
return 0;
}
SEQSTACK * INITSTACK()
{
SEQSTACK * stack = (SEQSTACK *)malloc(SIZE);
stack->top = 0;
return stack;
}
bool EMPTY(SEQSTACK * stack)
{
bool result;
if (stack->top > 0)
{
result = true;
}
else
{
result = false;
}
return result;
}
bool PUSH (SEQSTACK * stack,ELEMTYPE data)
{
bool result;
if ( stack == NULL || stack->top >= MAXSIZE - 1)
{
result = false;
}
else
{
stack->top ++;
stack->data[stack->top] = data;
result = true;
}
return result;
}
ELEMTYPE POP(SEQSTACK * stack)
{
ELEMTYPE result;
if (LENGTH(stack) > 0)
{
result = stack->data[stack->top];
stack->top --;
}
else
{
result = STACK_NULL;
}
return result;
}
int LENGTH(SEQSTACK * stack)
{
return stack->top;
}
ELEMTYPE GETTOP(SEQSTACK * stack)
{
ELEMTYPE result;
if (LENGTH(stack) > 0)
{
result = stack->data[stack->top];
}
else
{
result = STACK_NULL;
}
return result;
}
void CLEAR(SEQSTACK * seq)
{
seq->top = 0;
}
void menu()
{
print_enter();
print_info("sequence stack manage system");
print_enter();
print_menu_item("1","init seq stack");
print_menu_item("2","judge seq stack is empty");
print_menu_item("3","push element");
print_menu_item("4","pop element");
print_menu_item("5","get top element");
print_menu_item("6","empty stack");
print_menu_item("0","return menu");
print_menu_item("-1","exit system");
}
void print_tab()
{
printf("\t");
}
void print_enter()
{
printf("\n");
}
void print_menu_item(char * num,char * desc)
{
print_tab();
print_str(num);
print_tab();
print_str(desc);
print_enter();
}
void print_str(char * str)
{
while(*str)
{
printf("%c",*str++);
}
}
void print_info(char * str)
{
print_tab();
print_str(str);
print_enter();
}
void print_select_menu()
{
print_tab();
print_str("select menu:");
}
SEQSTACK * init_stack()
{
print_info("init stack success.");
return INITSTACK();
}
void is_empty(SEQSTACK * stack)
{
bool result = EMPTY(stack);
if (result)
{
print_info("sequence stack is ok.");
}
else
{
print_info("sequence is empty.");
}
}
bool is_init_stack(SEQSTACK * stack)
{
if(NULL == stack)
{
print_info("sequence stack not init.");
return false;
}
return true;
}
ELEMTYPE input_method()
{
print_tab();
print_str("plsease input data:");
ELEMTYPE input;
scanf(SCANF_ITEM,&input);
return input;
}
void stack_push(SEQSTACK * stack)
{
ELEMTYPE element = input_method();
bool result = PUSH(stack,element);
if (result)
{
print_info("push data success.");
}
else
{
print_info("push data fall.");
}
}
void stack_pop(SEQSTACK * stack)
{
ELEMTYPE element = POP(stack);
if (element == STACK_NULL)
{
print_info("the stack is null");
}
else
{
print_tab();
printf("the pop element is ");
printf(PRINT_ITEM,element);
print_enter();
}
}
void stack_gettop(SEQSTACK * stack)
{
ELEMTYPE element = GETTOP(stack);
if (element == STACK_NULL)
{
print_info("the stack is null");
}
else
{
print_tab();
printf("the top element is ");
printf(PRINT_ITEM,element);
print_enter();
}
}
void stack_clear(SEQSTACK * stack)
{
CLEAR(stack);
print_info("stack is empty now.");
}
void stack_method(SEQSTACK * stack,void (* fun)(SEQSTACK * src_stack))
{
if (!is_init_stack(stack))
{
return;
}
else
{
(* fun)(stack);
}
}
|