当前位置:首页 > 通信资讯 > 正文

本文实例为大家分享了C语言实现对后缀表达式(逆波兰表达式)的求解代码,供大家参考,具体内容如下

逆波兰表达式:

逆波兰表达式又叫后缀表达式。它是由相应的语法树的后序遍历的结果得到的。
例:5 - 8*(6 + 7) + 9 / 4:

其中缀表达式为:5 - 8 * 6 + 7 + 9 / 4

其语法树如下:

c语言用栈后缀表达式求值(利用栈求后缀表达式过程)

因此根据语法树可以得出他后序遍历(后缀表达式)为:
5 8 6 7 + * - 9 4 / +

这样就实现了中缀表达式到后缀表达式的转换。
同样的也可以得出他的前序遍历(前缀表达式也称波兰表达式):
 + - 5 * 8 + 6 7 / 9 4

逆波兰表达式计算实现原理:
1.首先当遇到运算操作数时将其进行push操作;

2.当遇到操作符是将此时的栈pop两次,先取出的栈顶为右操作数;

3.执行此方法到整个数组遍历完。

c语言用栈后缀表达式求值(利用栈求后缀表达式过程)

实现算法如下:

  1. void CalFunction(SqStack *S,char str[])
  2. {/*实现浮点型数据后缀表达式的加减乘除*/
  3. Elemtype number,e,d;
  4. char arr[MAXBUFFER];
  5. int i=0,j=0;
  6. InitStack(S);
  7. while(str[i]!='\0')
  8. {
  9. while(isdigit(str[i])||str[i]=='.') //过滤数字
  10. {
  11. arr[j++]=str[i++];
  12. arr[j]='\0';
  13. if( j >= MAXBUFFER )
  14. {
  15. printf("输入单个数据过大!\n");
  16. return ;
  17. }
  18. if(str[i]==' ')
  19. {
  20. number=atof(arr); //利用atof函数将数字字符串转化为double型数据
  21. PushStack(S,number); //将转换的数进行压栈
  22. j=0; //这里不要忘记将j重新初始化进行下个数据的转化
  23. break;
  24. }
  25. }
  26. /*如果遇到操作运算符则,弹出两个数据进行运算,然后将得出的结果重新入栈*/
  27. switch(str[i])
  28. {
  29. case '+':
  30. PopStack(S,&e);
  31. PopStack(S,&d);
  32. PushStack(S,d+e);
  33. break;
  34. case '-':
  35. PopStack(S,&e);
  36. PopStack(S,&d);
  37. PushStack(S,d-e);
  38. break;
  39. case '*':
  40. PopStack(S,&e);
  41. PopStack(S,&d);
  42. PushStack(S,d*e);
  43. break;
  44. case '/':
  45. PopStack(S,&e);
  46. PopStack(S,&d);
  47. if(e == 0)
  48. {
  49. printf("输入出错,分母为零!\n");
  50. return ;
  51. }
  52. PushStack(S,d/e);
  53. break;
  54. }
  55. i++; //继续遍历直到遍历字符串结束
  56. }
  57. PopStack(S,&e);
  58. printf("计算结果为:%lf",e);
  59. }

完整代码如下:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<assert.h>
  4. #include<ctype.h>
  5. #define INITSIZE 20
  6. #define INCREMENT 10
  7. #define MAXBUFFER 10
  8. #define LEN sizeof(Elemtype)
  9. /*栈的动态分配顺序存储结构*/
  10. typedef double Elemtype;
  11. typedef struct{
  12. Elemtype *base;
  13. Elemtype *top;
  14. int StackSize;
  15. }SqStack;
  16. void InitStack(SqStack *S)
  17. {
  18. S->base=(Elemtype*)malloc(LEN*INITSIZE);
  19. assert(S->base != NULL);
  20. S->top=S->base;
  21. S->StackSize=INITSIZE;
  22. }
  23. void PushStack(SqStack *S,Elemtype e)
  24. {
  25. if(S->top - S->base >= S->StackSize)
  26. {
  27. S->base=(Elemtype*)realloc(S->base,(S->StackSize+INCREMENT)*LEN);
  28. assert(S->base !=NULL);
  29. S->top=S->base+S->StackSize;
  30. S->StackSize+=INCREMENT;
  31. }
  32. *S->top =e;
  33. S->top++;
  34. }
  35. void PopStack(SqStack *S,Elemtype *e)
  36. {
  37. *e=*--S->top;
  38. }
  39. void CalFunction(SqStack *S,char str[])
  40. {
  41. Elemtype number,e,d;
  42. char arr[MAXBUFFER];
  43. int i=0,j=0;
  44. InitStack(S);
  45. while(str[i]!='\0')
  46. {
  47. while(isdigit(str[i])||str[i]=='.') //过滤数字
  48. {
  49. arr[j++]=str[i++];
  50. arr[j]='\0';
  51. if( j >= MAXBUFFER )
  52. {
  53. printf("输入单个数据过大!\n");
  54. return ;
  55. }
  56. if(str[i]==' ')
  57. {
  58. number=atof(arr); //利用atof函数将数字字符转化为double型数据
  59. PushStack(S,number); //将转换的数进行压栈
  60. j=0;
  61. break;
  62. }
  63. }
  64. switch(str[i])
  65. {
  66. case '+':
  67. PopStack(S,&e);
  68. PopStack(S,&d);
  69. PushStack(S,d+e);
  70. break;
  71. case '-':
  72. PopStack(S,&e);
  73. PopStack(S,&d);
  74. PushStack(S,d-e);
  75. break;
  76. case '*':
  77. PopStack(S,&e);
  78. PopStack(S,&d);
  79. PushStack(S,d*e);
  80. break;
  81. case '/':
  82. PopStack(S,&e);
  83. PopStack(S,&d);
  84. if(e == 0)
  85. {
  86. printf("输入出错,分母为零!\n");
  87. return ;
  88. }
  89. PushStack(S,d/e);
  90. break;
  91. }
  92. i++;
  93. }
  94. PopStack(S,&e);
  95. printf("计算结果为:%lf",e);
  96. }
  97. int main()
  98. {
  99. char str[100];
  100. SqStack S;
  101. printf("请按逆波兰表达式输入数据,每个数据之间用空格隔开:");
  102. gets(str);
  103. CalFunction(&S,str);
  104. return 0;
  105. }
  106. // 检测用例 5 - (6 + 7) * 8 + 9 / 4
  107. // 输入:5 8 6 7 + * - 9 4 / + #
  108. // 输出: - 96.750000

运行效果截图如下:

c语言用栈后缀表达式求值(利用栈求后缀表达式过程)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/qq_42552533/article/details/86562791

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。