首页 > 编程语言 > 详细

c++:栈的应用之表达式求值

时间:2015-10-23 00:16:08      阅读:360      评论:0      收藏:0      [点我收藏+]

 Stack.hpp

  1. #pragma once


  2. template <class T>

  3. class Stack{

  4. private:

  5. T* _array;

  6. size_t _capacity;

  7. int _topindex;

  8. public:

  9. Stack()          //构造函数

  10. :_array(0)

  11. , _capacity(0)

  12. , _topindex(-1)

  13. {}

  14. void Push(const T& x){                         //入栈操作

  15. if (_topindex + 1 == _capacity){

  16. _capacity = 2 * _capacity + 3;

  17. T* tmp = new T[_capacity];

  18. if (tmp == NULL){

  19. cout << "failed new" << endl;

  20. exit(-1);

  21. }

  22. memcpy(tmp, _array, sizeof(T) * (_topindex + 1));

  23. delete _array;

  24. _array = tmp;

  25. }

  26. _array[++_topindex] = x;

  27. }

  28. void Pop(){                                      //出栈

  29. if (_topindex > -1){

  30. //cout << _array[_topindex] << endl;

  31. _topindex--;

  32. }

  33. } 

  34. bool empty(){                  //清空

  35. return _topindex = -1;

  36. }

  37. const T& top(){                             //取栈顶元素

  38. //cout << _array[_topindex] << endl;

  39. return _array[_topindex];

  40. }

  41. };


  42. main.cpp

  43. #include<iostream>

  44. #include<string>

  45. #include"Stack.hpp"

  46. using namespace std;


  47. enum Type{

  48. ADD,

  49. SUB,

  50. MUL,

  51. DIV,

  52. OP_NUM,

  53. };


  54. struct Cell{

  55. Type _type;

  56. int num;

  57. };


  58. Cell RPNExp[] = {

  59. OP_NUM, 12,

  60. OP_NUM, 3,

  61. OP_NUM, 4,

  62. ADD, 1,

  63. MUL, 1,

  64. OP_NUM, 6,

  65. SUB, 1,

  66. OP_NUM, 8,

  67. OP_NUM, 2,

  68. DIV, 1,

  69. ADD, 1,

  70. };


  71. int main(){   

  72. Stack<int> s2;    

  73. int i = 0;

  74. for (int i = 0; i < sizeof(RPNExp) / sizeof(RPNExp[0]);i++){  //取出元素

  75.     int left, right;             //两个操作数

  76. switch (RPNExp[i]._type){        //将栈中数据出栈

  77. case ADD:                         //如果是加号,取两个操作数

  78. left = s2.top();

  79. s2.Pop();

  80. right = s2.top();

  81. s2.Pop();

  82. s2.Push(left + right);         //新的值入栈

  83. break;

  84. case SUB:                       //如果是减号,取两个操作数

  85. left = s2.top();

  86. s2.Pop();

  87. right = s2.top();

  88. s2.Pop();

  89. s2.Push(right - left);

  90. break;

  91. case MUL:                         //如果是乘号,取两个操作数

  92. left = s2.top();

  93. s2.Pop();

  94. right = s2.top();

  95. s2.Pop();

  96. s2.Push(left * right);

  97. break;

  98. case DIV:                        //如果是除号,取两个操作数

  99. left = s2.top();

  100. s2.Pop();

  101. right = s2.top();

  102. s2.Pop();

  103. s2.Push(right / left);

  104. break;

  105. case OP_NUM:                     //如果数字,入栈

  106. s2.Push(RPNExp[i].num);

  107. break;

  108. default:

  109. return -1;

  110. }

  111. }

  112. cout << s2.top() << endl;

  113. return 0;

  114. }

本文出自 “moLova” 博客,请务必保留此出处http://molova.blog.51cto.com/10594266/1705349

c++:栈的应用之表达式求值

原文:http://molova.blog.51cto.com/10594266/1705349

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!