1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef char datatype; 5 typedef struct stack 6 { 7 int top; /*栈顶指针*/ 8 datatype* data; /*数组*/ 9 int MaxSize; /*栈大小*/ 10 }stack; 11 12 /*初始化空栈*/ 13 void InitStack(stack* st, int sz) 14 { 15 st->top = -1; 16 st->MaxSize = sz; 17 st->data = (datatype*)malloc(sizeof(datatype) * st->MaxSize); //分配内存 18 } 19 /*释放站空间*/ 20 void FreeStack(stack* st) 21 { 22 free(st->data); 23 } 24 /*压栈*/ 25 int Push(stack* st, datatype d) 26 { 27 if (st->top == st->MaxSize-1) return -1; 28 st->data[++(st->top)] = d; 29 return 0; 30 } 31 /*弹栈*/ 32 datatype Pop(stack* st) 33 { 34 if ((st->top) == -1) 35 { 36 printf("llll%d",st->top); 37 exit(-1); 38 } 39 else { 40 return st->data[(st->top)--]; 41 } 42 43 } 44 /*取顶*/ 45 datatype getTop(stack* st) 46 { 47 if(st->top>-1) 48 return st->data[st->top]; 49 return NULL; 50 } 51 /*栈置空*/ 52 void MakeEmpty(stack* st) 53 { 54 st->top = -1; 55 } 56 57 /*后缀表达式计算*/ 58 int Midcal() 59 { 60 stack* sptr = (stack*)malloc(sizeof(stack)); 61 char buf[80]; 62 int i = 0, k; 63 InitStack(sptr, 80); 64 65 printf("input Postfix "); 66 scanf_s("%s", buf,30); //控制边界 67 68 while (buf[i] != '