1 #include <stdio.h> 2 #include <stdlib.h> 3 #define Max 100 4 5 int *p; 6 int *tos; 7 int *bos; 8 9 /*添加一个数据放到堆栈对顶端*/ 10 void push(int i) 11 { 12 if (p > bos) 13 { 14 printf("堆栈以满 "); 15 return; 16 } 17 *p = i; 18 p++; 19 } 20 21 /*丛堆栈顶端取出一个数据*/ 22 int pop(void) 23 { 24 p--; 25 if (p < tos) 26 { 27 printf("堆栈下溢 "); 28 return 0; 29 } 30 return *p; 31 } 32 33 int main() 34 { 35 int a, b; 36 char s[80]; 37 p = (int *)malloc(Max*sizeof(int)); 38 if (!p) 39 { 40 printf("分配内存失败"); 41 exit(1); 42 } 43 tos = p; 44 bos = p + Max - 1; 45 printf("请输入第一个数据: "); 46 scanf("%d", &a); 47 push(a); 48 printf("请输入第二个数据: "); 49 scanf("%d", &b); 50 push(b); 51 printf("请输入操作符: "); 52 scanf("%s", s); 53 switch (*s) 54 { 55 case '+': 56 a = pop(); 57 b = pop(); 58 printf("结果是a+b = %d ", (a + b)); 59 push(a + b); 60 break; 61 case '-': 62 a = pop(); 63 b = pop(); 64 printf("结果是a-b = %d ", (a - b)); 65 push(a - b); 66 break; 67 case '*': 68 a = pop(); 69 b = pop(); 70 printf("结果是a*b = %d ", (a*b)); 71 push(a*b); 72 break; 73 case '/': 74 a = pop(); 75 b = pop(); 76 printf("结果是a/b = %d ", (a / b)); 77 push(a / b); 78 break; 79 default: 80 printf("请输入正确操作符 "); 81 } 82 83 getchar(); 84 return 0; 85 }