#include <stdio.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int bool; #define TRUE 1 #define FALSE 0 typedef struct { char *base; char *top; int stacksize; }sqStack; //=======================function protoType============================= bool InitStack(sqStack *stk); bool DestroyStack(sqStack *stk); bool ClearStack(sqStack *stk); bool StackEmpty(sqStack stk); int StackLength(sqStack stk); bool GetTop(sqStack stk, char *item); bool Push(sqStack *s, char item); bool Pop(sqStack *s, char *item); bool InitStack(sqStack *stk) { stk->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char)); if (!stk->base) return FALSE; stk->top = stk->base; stk->stacksize = STACK_INIT_SIZE; return TRUE; } bool GetTop(sqStack stk, char *item) { if (stk.top == stk.base) return FALSE; item = (stk.top-1); return TRUE; } bool Push(sqStack *stk, char item) { if (stk->top - stk->base >= stk->stacksize) { printf("allocate New Mem\n"); stk->base = (char *)realloc(stk->base, (stk->stacksize + STACKINCREMENT)* sizeof(char)); if (!stk->base) return FALSE; stk->top = stk->base + stk->stacksize; stk->stacksize += STACKINCREMENT; } *stk->top = item; stk->top++; return TRUE; } bool Pop(sqStack *stk, char* item) { if (stk->top == stk->base) return FALSE; stk->top--; *item = *(stk->top); return TRUE; } bool StackEmpty(sqStack stk) { if (stk.top == stk.base) return TRUE; else return FALSE; } bool ClearStack(sqStack *stk) { stk->top = stk->base; memset(stk->base, 0, sizeof(char)); if (stk->top) return TRUE; else return FALSE; } bool DestroyStack(sqStack *stk) { free(stk->base); printf("free memery\n"); return TRUE; } int main(int argc, char** argv) { sqStack stk; char ch; InitStack(&stk); ch = getchar(); while(ch != EOF) { while(ch != EOF && ch != '\n') { switch (ch) { case '#': Pop(&stk, &ch); break; case '@': ClearStack(&stk); break; case 'q': DestroyStack(&stk); exit(1); break; default: Push(&stk, ch); } ch = getchar(); } ClearStack(&stk); if (ch != EOF) ch = getchar(); } DestroyStack(&stk); return 1; }