中缀表达式转后缀表达式C++代码

  1. //MyStack.h
  2. #include<iostream>
  3. usingnamespacestd;
  4. template<classElemType>classMyStack
  5. {
  6. public:
  7. conststaticintMAXSIZE=100;
  8. ElemTypedata[MAXSIZE];
  9. inttop;
  10. public:
  11. voidinit();//初始化栈
  12. boolempty();//判断栈是否为空
  13. ElemTypegettop();//读取栈顶元素(不出栈)
  14. voidpush(ElemTypex);//进栈
  15. ElemTypepop();//出栈
  16. };
  17. template<classT>voidMyStack<T>::init()
  18. {
  19. this->top=0;
  20. }
  21. template<classT>boolMyStack<T>::empty()
  22. {
  23. returnthis->top==0?true:false;
  24. }
  25. template<classT>TMyStack<T>::gettop()
  26. {
  27. if(empty())
  28. {
  29. cout<<"栈为空!\n";
  30. exit(1);
  31. }
  32. returnthis->data[this->top-1];
  33. }
  34. template<classT>voidMyStack<T>::push(Tx)
  35. {
  36. if(this->top==MAXSIZE)
  37. {
  38. cout<<"栈已满!\n";
  39. exit(1);
  40. }
  41. this->data[this->top]=x;
  42. this->top++;
  43. }
  44. template<classT>TMyStack<T>::pop()
  45. {
  46. if(this->empty())
  47. {
  48. cout<<"栈为空!\n";
  49. exit(1);
  50. }
  51. Te=this->data[this->top-1];
  52. this->top--;
  53. returne;
  54. }


  1. //PrefixToPostfix.h
  2. #include<vector>
  3. usingnamespacestd;
  4. boolisoperator(charop);//判断是否为运算符
  5. intpriority(charop);//求运算符优先级
  6. voidpostfix(charpre[],charpost[],int&n);//把中缀表达式转换为后缀表达式
  7. doubleread_number(charstr[],int*i);//将数字字符串转变成相应的数字
  8. doublepostfix_value(charpost[]);//由后缀表达式字符串计算相应的中值表达式的值

  1. //PrefixToPostfix.cpp
  2. #include"MyStack.h"
  3. #include"PrefixToPostfix.h"
  4. #include<iostream>
  5. usingnamespacestd;
  6. voidmain()
  7. {
  8. MyStack<int>stack;
  9. stack.init();
  10. //charpre[]="22/(5*2+1)#";
  11. charexp[100];
  12. cout<<"输入表达式(中缀,以#结束):";
  13. cin>>exp;
  14. charpost[100];
  15. //cout<<"中缀表达式为:"<<pre<<endl;
  16. intn=0;//返回后缀表达式的长度
  17. postfix(exp,post,n);
  18. cout<<"后缀表达式为:";
  19. for(inti=0;i<n;i++)
  20. cout<<post[i];
  21. cout<<"\n由后缀表达式计算出的数值结果:";
  22. cout<<postfix_value(post)<<endl;
  23. system("pause");
  24. }
  25. boolisoperator(charop)
  26. {
  27. switch(op)
  28. {
  29. case'+':
  30. case'-':
  31. case'*':
  32. case'/':
  33. return1;
  34. default:
  35. return0;
  36. }
  37. }
  38. intpriority(charop)
  39. {
  40. switch(op)
  41. {
  42. case'#':
  43. return-1;
  44. case'(':
  45. return0;
  46. case'+':
  47. case'-':
  48. return1;
  49. case'*':
  50. case'/':
  51. return2;
  52. default:
  53. return-1;
  54. }
  55. }
  56. //把中缀表达式转换为后缀表达式,返回后缀表达式的长度(包括空格)
  57. voidpostfix(charpre[],charpost[],int&n)
  58. {
  59. inti=0,j=0;
  60. MyStack<char>stack;
  61. stack.init();//初始化存储操作符的栈
  62. stack.push('#');//首先把结束标志‘#’放入栈底
  63. while(pre[i]!='#')
  64. {
  65. if((pre[i]>='0'&&pre[i]<='9')||pre[i]=='.')//遇到数字和小数点直接写入后缀表达式
  66. {
  67. post[j++]=pre[i];
  68. n++;
  69. }
  70. elseif(pre[i]=='(')//遇到“(”不用比较直接入栈
  71. stack.push(pre[i]);
  72. elseif(pre[i]==')')//遇到右括号将其对应左括号后的操作符(操作符栈中的)全部写入后缀表达式
  73. {
  74. while(stack.gettop()!='(')
  75. {
  76. post[j++]=stack.pop();
  77. n++;
  78. }
  79. stack.pop();//将“(”出栈,后缀表达式中不含小括号
  80. }
  81. elseif(isoperator(pre[i]))
  82. {
  83. post[j++]='';//用空格分开操作数(
  84. n++;
  85. while(priority(pre[i])<=priority(stack.gettop()))
  86. {
  87. //当前的操作符小于等于栈顶操作符的优先级时,将栈顶操作符写入到后缀表达式,重复此过程
  88. post[j++]=stack.pop();
  89. n++;
  90. }
  91. stack.push(pre[i]);//当前操作符优先级大于栈顶操作符的优先级,将该操作符入栈
  92. }
  93. i++;
  94. }
  95. while(stack.top)//将所有的操作符加入后缀表达式
  96. {
  97. post[j++]=stack.pop();
  98. n++;
  99. }
  100. }
  101. doubleread_number(charstr[],int*i)
  102. {
  103. doublex=0.0;
  104. intk=0;
  105. while(str[*i]>='0'&&str[*i]<='9')//处理整数部分
  106. {
  107. x=x*10+(str[*i]-'0');
  108. (*i)++;
  109. }
  110. if(str[*i]=='.')//处理小数部分
  111. {
  112. (*i)++;
  113. while(str[*i]>='0'&&str[*i]<='9')
  114. {
  115. x=x*10+(str[*i]-'0');
  116. (*i)++;
  117. k++;
  118. }
  119. }
  120. while(k!=0)
  121. {
  122. x/=10.0;
  123. k--;
  124. }
  125. returnx;
  126. }
  127. doublepostfix_value(charpost[])
  128. {
  129. MyStack<double>stack;//操作数栈
  130. stack.init();
  131. inti=0;
  132. doublex1,x2;
  133. while(post[i]!='#')
  134. {
  135. if(post[i]>='0'&&post[i]<='9')
  136. stack.push(read_number(post,&i));
  137. elseif(post[i]=='')
  138. i++;
  139. elseif(post[i]=='+')
  140. {
  141. x2=stack.pop();
  142. x1=stack.pop();
  143. stack.push(x1+x2);
  144. i++;
  145. }
  146. elseif(post[i]=='-')
  147. {
  148. x2=stack.pop();
  149. x1=stack.pop();
  150. stack.push(x1-x2);
  151. i++;
  152. }
  153. elseif(post[i]=='*')
  154. {
  155. x2=stack.pop();
  156. x1=stack.pop();
  157. stack.push(x1*x2);
  158. i++;
  159. }
  160. elseif(post[i]=='/')
  161. {
  162. x2=stack.pop();
  163. x1=stack.pop();
  164. stack.push(x1/x2);
  165. i++;
  166. }
  167. }
  168. returnstack.gettop();
  169. }


运行结果:



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值