当前位置:   article > 正文

将中缀表达式转化成后缀表达式_中缀表达式转后缀表达式

中缀表达式转后缀表达式

中缀变后缀主要的思想就是将需要的运算符先做一个映射

 对于任意表达式,式中从头开始扫,遇到非运算符,即任意数字或字母直接输出

遇到运算符考虑放入栈中

若栈空则放入

若栈不为空,判断栈顶的优先级是否 < 待放入的运算符,若 小于则将其压入栈中;

若不小于,则将栈一直 pop,知道运算符可以放入栈中

 

当然还有特殊的元素符, '('   ')'  这两个元素比较特殊,‘(’ 元素符优先级最大,自然放入栈中,但是要注意,只有遇到 ')' 时,‘(’

才会弹出,所以在栈中有 '(' 时,需要特判,当然 '('  ')' 这两个运算符是不需要输出的

下面给出应用 STL 中的程序和自己写的栈链的程序,主函数是相似的,只是在不重要的地方改了一下

 

stack 代码:

  1. #include <iostream>
  2. #include <map>
  3. #include <stack>
  4. using namespace std;
  5. int n,m,t;
  6. int i,j,k;
  7. //int a[N];
  8. map<char,int> mp;
  9. int main()
  10. {
  11. //IOS;
  12. mp['+']=mp['-']=1;
  13. mp['*']=mp['/']=2;
  14. mp['(']=3,mp[')']=1;
  15. stack<char> s;
  16. while(s.size()) s.pop();
  17. string str; cin>>str;
  18. for(int i=0;str[i];i++){
  19. //dbg(i);
  20. if(!mp.count(str[i])) cout<<str[i];
  21. else{ //处理运算符
  22. if(s.empty()) s.push(str[i]);
  23. else
  24. {
  25. if(mp[str[i]]<=mp[s.top()] && s.top()=='('){
  26. s.push(str[i]);
  27. }
  28. else if(str[i]==')'){
  29. while(true){
  30. if(s.size() && s.top()=='(') { s.pop(); break; }
  31. else if(s.size()) cout<<s.top(),s.pop();
  32. }
  33. }
  34. else if(mp[str[i]]>mp[s.top()]) s.push(str[i]);
  35. else{
  36. while(true){
  37. if(s.size() && s.top()=='(') { s.push(str[i]); break; }
  38. if(s.empty() || mp[str[i]]>mp[s.top()]){ s.push(str[i]); break; }
  39. else cout<<s.top(),s.pop();
  40. }
  41. }
  42. }
  43. }
  44. }
  45. while(s.size()) cout<<s.top(),s.pop();
  46. cout<<endl;
  47. //PAUSE;
  48. return 0;
  49. }

手写栈链代码 :

  1. #include <iostream>
  2. #include <map>
  3. #define null NULL
  4. using namespace std;
  5. int n,m,t;
  6. int i,j,k;
  7. //int a[N];
  8. map<char,int> mp;
  9. struct node
  10. {
  11. char data;
  12. node *next;
  13. };
  14. class Stack
  15. {
  16. private:
  17. node *fa;
  18. int sum;
  19. public:
  20. Stack ()
  21. {
  22. fa=new node;
  23. fa=null;
  24. sum=0;
  25. }
  26. void push(char x)
  27. {
  28. node *p=new node;
  29. p->data=x;
  30. p->next=fa;
  31. fa=p;
  32. sum++;
  33. }
  34. void pop()
  35. {
  36. node *p=fa;
  37. fa=fa->next;
  38. delete p;
  39. sum--;
  40. }
  41. char top()
  42. {
  43. return fa->data;
  44. }
  45. bool empty()
  46. {
  47. if(fa==null) return true;
  48. else return false;
  49. }
  50. int size()
  51. {
  52. return sum;
  53. }
  54. };
  55. int main()
  56. {
  57. //IOS;
  58. mp['+']=mp['-']=1;
  59. mp['*']=mp['/']=2;
  60. mp['(']=3,mp[')']=1;
  61. Stack s;
  62. while(s.size()) s.pop();
  63. char str[100]; cin>>str;
  64. for(int i=0;str[i];i++){
  65. //dbg(i);
  66. if(!mp.count(str[i])) cout<<str[i];
  67. else{ //处理运算符
  68. if(s.empty()) s.push(str[i]);
  69. else
  70. {
  71. if(mp[str[i]]<=mp[s.top()] && s.top()=='('){
  72. s.push(str[i]);
  73. }
  74. else if(str[i]==')'){
  75. while(true){
  76. if(s.size() && s.top()=='(') { s.pop(); break; }
  77. else if(s.size()) cout<<s.top(),s.pop();
  78. }
  79. }
  80. else if(mp[str[i]]>mp[s.top()]) s.push(str[i]);
  81. else{
  82. while(true){
  83. if(s.size() && s.top()=='(') { s.push(str[i]); break; }
  84. if(s.empty() || mp[str[i]]>mp[s.top()]){ s.push(str[i]); break; }
  85. else cout<<s.top(),s.pop();
  86. }
  87. }
  88. }
  89. }
  90. }
  91. while(s.size()) cout<<s.top(),s.pop();
  92. cout<<endl;
  93. //PAUSE;
  94. return 0;
  95. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号