Chinaunix首页 | 论坛 | 博客
  • 博客访问: 192675
  • 博文数量: 27
  • 博客积分: 725
  • 博客等级: 上士
  • 技术积分: 347
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-04 09:01
文章分类

全部博文(27)

文章存档

2012年(15)

2011年(12)

分类: C/C++

2012-03-27 18:42:36

用堆栈实现的计算器,可支持多重括号和多位整数的加减乘除运算。
  1. int main(void)
  2. {    
  3.     while (1) {
  4.         char *buffer = input();
  5.         int *sufstr = transform(buffer);
  6.         int result = calculate(sufstr);
  7.     }
  8.     return 0;
  9. }

  10. char *
  11. input(const char *ptr)
  12. {
  13.     static char buffer[MAX_LEN];
  14.     
  15.     memset(buffer, 0, sizeof buffer);
  16.     
  17.     printf("\n%s", ptr);
  18.     
  19.     if (!fgets(buffer, sizeof buffer, stdin))
  20.         return NULL;
  21.         
  22.     if (buffer[strlen(buffer) - 1] == '\n')
  23.         buffer[strlen(buffer) - 1] = '\0';
  24.         
  25.     if (!strcmp(buffer, "exit"))
  26.         return NULL;
  27.     
  28.     return buffer;
  29. }

  30. int *
  31. transform(char *str)
  32. {    
  33.     char              c, *data;
  34.     int              result, *sufstr;
  35.     struct    stack    *stack, *sufix;
  36.     
  37.     stack = stack_new(0);
  38.     sufix = stack_new(0);
  39.     
  40.     sufstr = stack_buffer(sufix);    
  41.     
  42.     for ( ; (c = *str) != '\0'; ) {
  43.     
  44.         if (ISNUM(c)) {
  45.         
  46.             str = read_int(str, &result);
  47.             
  48.             stack_push(sufix, (char *)result);
  49.             
  50.         } else {
  51.         
  52.             if (c == '(') {
  53.             
  54.                 stack_push(stack, str);
  55.                 
  56.             } else if (c == ')') {
  57.             
  58.                 while ((data = stack_pop(stack))) {
  59.                     if (*data == '(') break;
  60.                     stack_push(sufix, (char *)cti(*data));
  61.                 }
  62.                 
  63.             } else if (ISOPT(c)) {
  64.             
  65.                 data = stack_top(stack);
  66.                 
  67.                 if (!data || pri(c) > pri(*data)) {
  68.                 
  69.                     stack_push(stack, str);
  70.                     
  71.                 } else {
  72.                     data = stack_pop(stack);
  73.                     stack_push(sufix, (char *)cti(*data));
  74.                     stack_push(stack, str);
  75.                 }
  76.             }
  77.             str++;
  78.         }
  79.     }
  80.     
  81.     while ((data = stack_pop(stack))) {
  82.         stack_push(sufix, (char *)cti(*data));
  83.     }
  84.     
  85.     stack_push(sufix, (char *)-1);

  86.     stack_free(sufix);
  87.     stack_destroy(stack);    

  88.     return sufstr;
  89. }

  90. int
  91. calculate(int *restr)
  92. {
  93.     int              i, result, *ptr;
  94.     struct    stack    *stack;
  95.     
  96.     ptr = restr;
  97.     stack = stack_new(0);

  98.     for (i = 0; (i = *ptr) != -1; ptr++) {
  99.         if (i >= 0) {
  100.             stack_push(stack, (char *)i);
  101.         } else {
  102.             int a = (int)stack_pop(stack);
  103.             int b = (int)stack_pop(stack);
  104.             int c = cal(b, a, i);
  105.             stack_push(stack, (char *)c);
  106.         }
  107.     }
  108.     
  109.     result = (int)stack_pop(stack);
  110.     
  111.     stack_destroy(stack);
  112.     free(restr);
  113.     
  114.     return result;
  115. }


 V2.zip   
阅读(1500) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~