1. #include<stdio.h> 
  2. #include<string.h> 
  3. #include<ctype.h> 
  4. #include<stdlib.h> 
  5. #define MAXSIZE 30 
  6.   
  7. typedef int ElemType; 
  8. typedef struct Stack{//栈结构  
  9.     ElemType data[MAXSIZE]; 
  10.     int top;//栈顶指针  
  11. }Stack,*pStack; 
  12.   
  13. void initStack(pStack S){//初始化栈  
  14.     S->top = -1; 
  15. bool push(pStack S,ElemType e){  
  16.     if(S->top == MAXSIZE-1){ 
  17.         puts("栈满"); 
  18.         return false
  19.     } 
  20.     S->data[++(S->top)] = e; 
  21.     return true
  22. bool pop(pStack S,ElemType *e){ 
  23.     if(S->top == -1){ 
  24.         puts("栈空"); 
  25.         return false
  26.     } 
  27.     *e = S->data[(S->top)--]; 
  28.     return true
  29. int main(void){ 
  30.     char string[MAXSIZE];//存储字符串  
  31.     char tempNumber[MAXSIZE];//存储数值字符  
  32.     char ch;//分解每个字符  
  33.     int index; 
  34.     int number;//从字符中获得的数值  
  35.     int result = 0;//最终结果  
  36.     int count = 0;//数字计数  
  37.     int a1,a2;//辅助数值计算  
  38.     bool tag;//标志位,数字是否已经结束  
  39.     Stack numberStack;//数字栈  
  40.     pStack S = &numberStack;//指向栈的指针  
  41.      
  42.     initStack(S);//初始化栈  
  43.     puts("Enter a string"); 
  44.     gets(string); 
  45.     for(index=0;index<strlen(string);index++){ 
  46.         ch = string[index]; 
  47.         if(isdigit(ch)){ 
  48.             tag = true;//现在输入的是数字  
  49.             tempNumber[count] = ch; 
  50.             count++; 
  51.         } 
  52.         else
  53.             if(tag){//判断上一个字符是不是数值  
  54.                 tempNumber[count] = '\0';//一个数值结束了  
  55.                 number = atoi(tempNumber); 
  56.                 push(S,number); 
  57.                 count = 0;//重新计算数值  
  58.                 tag = false
  59.             } 
  60.             switch(ch){ 
  61.                 case '+'
  62.                      pop(S,&a2); 
  63.                      pop(S,&a1); 
  64.                      push(S,a1+a2); 
  65.                      break
  66.                 case '-'
  67.                      pop(S,&a2); 
  68.                      pop(S,&a1); 
  69.                      push(S,a1-a2); 
  70.                      break
  71.                 case '*'
  72.                      pop(S,&a2); 
  73.                      pop(S,&a1); 
  74.                      push(S,a1*a2); 
  75.                      break
  76.                 case '/'
  77.                      pop(S,&a2); 
  78.                      pop(S,&a1); 
  79.                      push(S,a1/a2); 
  80.                      break
  81.                 default
  82.                      break
  83.             } 
  84.         } 
  85.     } 
  86.     pop(S,&result); 
  87.     printf("%d",result); 
  88.     getchar(); 
  89.     return 0;