鱼C论坛

 找回密码
 立即注册
查看: 5695|回复: 5

[技术交流] 中缀转后缀(Infix2Postfix.c)

[复制链接]
发表于 2015-12-18 22:46:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 spy 于 2015-12-19 10:18 编辑
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <conio.h>
  6. #include <math.h>

  7. /*
  8. 算法的基本思想是:
  9.         顺序扫描键盘缓冲区中输入的中缀表达式,读到操作数时,直接将其存放至操作数队列
  10.         读到运算符时,将操作符栈中所有优先级高于或等于该运算符的运算符弹出栈,
  11.         并将其存放至操作数队列,然后在将读入的运算符入栈
  12.         当读入左括号时直接入栈;
  13.         当读到右括号时,将与靠近栈顶的第一个左括号之间的运算符全部依次弹出栈,存放至输出队列
  14. */

  15. struct StackNode
  16. {
  17.         char data;
  18.         struct StackNode* next;
  19. };
  20. void Push(struct StackNode** top, char data);
  21. char Pop(struct StackNode** top);
  22. char GetTop(struct StackNode* top);

  23. struct QueueNode
  24. {
  25.         float data;
  26.         struct QueueNode* next;
  27. };
  28. void InitQueue(struct QueueNode** front, struct QueueNode** rear);
  29. void EnQueue(struct QueueNode** rear, float data);
  30. float DeQueue(struct QueueNode* front, struct QueueNode** rear);
  31. void QueueTraverse(struct QueueNode* front);
  32. void DestroyQueue(struct QueueNode** front, struct QueueNode** rear);

  33. int Priority(char op);

  34. void main()
  35. {
  36.         // 保存操作符的栈
  37.         struct StackNode* top = NULL;
  38.         // 队列中要存放的是转换后的逆波兰表达式就是后缀表达式
  39.         struct QueueNode* front = NULL;
  40.         struct QueueNode* rear = NULL;
  41.         float num;
  42.         char ch, e;
  43.         InitQueue(&front, &rear);
  44.         printf("请输入中缀表达式, 以回车键结束输入:\n");
  45.         do
  46.         {
  47.                 scanf("%c", &ch);
  48.                 switch ( ch )
  49.                 {
  50.                 case ' ': break;         // 过滤掉空格
  51.                 // 检测到键盘缓冲区中有数字开始的字符
  52.                 case '0':
  53.                 case '1':
  54.                 case '2':
  55.                 case '3':
  56.                 case '4':
  57.                 case '5':
  58.                 case '6':
  59.                 case '7':
  60.                 case '8':
  61.                 case '9':
  62.                         ungetc(ch, stdin);         // pay attention !
  63.                         scanf("%f", &num);         // 以浮点数的格式从键盘缓冲区获得一个操作数(整数同样也可以读取)
  64.                         EnQueue(&rear, num); break;         // 将读取到的操作数入队
  65.                 // 碰到左括号,先放入操作符栈
  66.                 case '(':
  67.                         Push(&top, ch); break;
  68.                 // 碰到有括号, 由于括号隔离了优先级, 则需要先计算左右括号之间的表达式
  69.                 case ')':
  70.                 case '\n':  // pay attention! 加'\n'是考虑到读取到最后的一个结束字符时栈中还有一个操作符并没有参与运算
  71.                         // top时刻指向操作符的栈顶,当栈中存在操作符且操作符位于左右括号之间时才将栈中的操作符弹出栈,然后放入操作数队列
  72.                         while ( top && ( e=Pop(&top), e!='(' ) )
  73.                         {
  74.                                 EnQueue(&rear, e);
  75.                         }
  76.                         break;
  77.                 // 四则运算
  78.                 case '+':
  79.                 case '-':
  80.                 case '*':
  81.                 case '/':
  82.                         // 判读优先级,如果当前读入的操作符优先级低于栈顶操作符优先级则说明栈顶操作符的优先级别高,将它弹出栈并入操作数队列
  83.                         while ( Priority(ch) <= Priority(GetTop(top)) )
  84.                         {
  85.                                 e = Pop(&top);
  86.                                 EnQueue(&rear, e);
  87.                         }
  88.                         Push(&top, ch);
  89.                         break;
  90.                 default:
  91.                         printf("表达式非法!\n");
  92.                         exit(0);
  93.                 }
  94.         } while( ch!='\n' );

  95.         // 此时队列中存放的是转换好的逆波兰表达式(后缀表达式),有了逆波兰表达式就可以进一步的求出表达式的值了(不难的求的)^-^
  96.         QueueTraverse(front);

  97.         DestroyQueue(&front, &rear);

  98.         _getch();

  99. }

  100. int Priority(char op)
  101. {
  102.         switch ( op )
  103.         {
  104.         case '(':
  105.         case '#':
  106.                 return 0;
  107.         case '-':
  108.         case '+':
  109.                 return 1;
  110.         case '*':
  111.         case '/':
  112.                 return 2;
  113.         default:
  114.                 return -1;
  115.         }
  116. }

  117. void Push(struct StackNode** top, char ch)
  118. {
  119.         struct StackNode* p = (struct StackNode*)malloc(sizeof(struct StackNode));
  120.         p->data = ch;
  121.         p->next = *top;
  122.         *top = p;
  123. }
  124. char Pop(struct StackNode** top)
  125. {
  126.         struct StackNode* p = *top;
  127.         char e;
  128.         if ( p == NULL )
  129.         {
  130.                 printf("下溢!\n");
  131.                 exit(0);
  132.         }
  133.         e = p->data;
  134.         *top = p->next;
  135.         free(p);
  136.         return e;
  137. }

  138. char GetTop(struct StackNode* top)
  139. {
  140.         if ( top == NULL )
  141.         {
  142.                 return -1;
  143.         }
  144.         return top->data;
  145. }

  146. void InitQueue(struct QueueNode** front, struct QueueNode** rear)
  147. {
  148.         struct QueueNode* p = (struct QueueNode*)malloc(sizeof(struct QueueNode));
  149.         p->next = NULL;
  150.         *front = *rear = p;
  151. }

  152. void EnQueue(struct QueueNode** rear, float e)
  153. {
  154.         if ( *rear == NULL )
  155.         {
  156.                 printf("队列已被销毁或者还没有被初始化, 请初始化队列!");
  157.         }
  158.         else
  159.         {
  160.                 struct QueueNode* p = (struct QueueNode*)malloc(sizeof(struct QueueNode));
  161.                 p->data = e;
  162.                 p->next = NULL;
  163.                 (*rear)->next =  p;
  164.                 *rear = p;
  165.         }
  166. }

  167. float DeQueue(struct QueueNode* front,struct QueueNode** rear)
  168. {
  169.         float e;
  170.         if ( front == NULL )
  171.         {
  172.                 printf("队列已被销毁或者还没有被初始化, 请初始化队列!\n\n");
  173.         }
  174.         else
  175.         {
  176.                 if ( front == *rear )
  177.                 {
  178.                         printf("队列为空, 无法出队!\n\n");
  179.                 }
  180.                 else
  181.                 {
  182.                         struct QueueNode* p = front->next;
  183.                         e = p->data;
  184.                         front->next = p->next;
  185.                         if ( p == *rear )
  186.                         {
  187.                                 *rear = front;
  188.                         }
  189.                         free(p);
  190.                 }
  191.         }
  192.         return e;
  193. }

  194. void QueueTraverse(struct QueueNode* front)
  195. {
  196.         struct QueueNode* p = front;
  197.         if ( p == NULL )
  198.         {
  199.                 printf("队列已被销毁或者还没有被初始化, 请初始化队列!");
  200.         }
  201.         else
  202.         {
  203.                 if ( p->next == NULL )
  204.                 {
  205.                         printf("队列为空!");
  206.                 }
  207.                 else
  208.                 {
  209.                         printf("队列中的元素为: ");
  210.                         while ( p->next != NULL )
  211.                         {
  212.                                 p = p->next;
  213.                                 switch ( (char)p->data )
  214.                                 {
  215.                                 case '+':
  216.                                 case '-':
  217.                                 case '*':
  218.                                 case '/':
  219.                                         printf("%c ", (char)p->data); break;
  220.                                 default:
  221.                                         printf("%f ", p->data);
  222.                                 }
  223.                         }
  224.                 }
  225.         }
  226.         printf("\n");
  227. }

  228. void DestroyQueue(struct QueueNode** front, struct QueueNode** rear)
  229. {
  230.         while( *front )
  231.         {
  232.                 *rear = (*front)->next;
  233.                 free(*front);
  234.                 *front = *rear;
  235.         }
  236. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-19 00:37:34 | 显示全部楼层
^-^
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-19 22:40:21 | 显示全部楼层
非常感谢楼主
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-12-21 23:30:48 | 显示全部楼层
非常感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-12-22 23:07:40 | 显示全部楼层
感谢分享
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-23 13:36:18 | 显示全部楼层

很高心能帮助到你^_^
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 12:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表