Advertisement
huutho_96

Tính Biểu Thức

May 28th, 2015
1,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. //https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833?ref=bookmarks
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. template <class KieuDuLieuTongQuat>
  8. class Stack
  9. {
  10. private:
  11. struct Node
  12. {
  13. KieuDuLieuTongQuat Value;
  14. Node *pNext;
  15. };
  16. Node *pHead;
  17. void Huy()
  18. {
  19. Node *p = pHead;
  20. while (pHead)
  21. {
  22. p = pHead;
  23. pHead = p->pNext;
  24. delete p;
  25. }
  26. }
  27. Node *CreateNode(KieuDuLieuTongQuat x)
  28. {
  29. Node * p = new Node;
  30. if (!p) exit(1);
  31. p->Value = x;
  32. p->pNext = NULL;
  33. return p;
  34. }
  35. public:
  36. Stack();
  37. ~Stack()
  38. {
  39. Huy();
  40. }
  41. bool Empty()
  42. {
  43. return (pHead == NULL);
  44. }
  45. int Size()
  46. {
  47. Node *p = pHead;
  48. KieuDuLieuTongQuat i = 0;
  49. while (p)
  50. {
  51. p = p->pNext;
  52. i++;
  53. }
  54. return i;
  55. }
  56. KieuDuLieuTongQuat Top()
  57. {
  58. if (pHead)
  59. return pHead->Value;
  60. return -1;
  61. }
  62. void Push(KieuDuLieuTongQuat x)
  63. {
  64. Node * p = CreateNode(x);
  65. if (!pHead)
  66. pHead = p;
  67. else
  68. {
  69. p->pNext = pHead;
  70. pHead = p;
  71. }
  72. }
  73. void Pop()
  74. {
  75. if (pHead)
  76. {
  77. Node *p = pHead;
  78. pHead = p->pNext;
  79. delete p;
  80. }
  81. }
  82. };
  83.  
  84. template <class KieuDuLieuTongQuat>
  85. Stack<KieuDuLieuTongQuat>::Stack()
  86. {
  87. pHead = NULL;
  88. }
  89.  
  90. bool LaSo(char c)
  91. {
  92. return (c >= '0' && c <= '9');
  93. }
  94. bool LaTT(char c)
  95. {
  96. return (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^');
  97. }
  98. int UT(char c)
  99. {
  100.  
  101. if (c == '+' || c == '-') return 1;
  102. if (c == '*' || c == '/' || c == '%') return 2;
  103. if (c == '^') return 3;
  104. return 0;
  105. }
  106.  
  107.  
  108. string ChuyenTrungToSangHauTo(string TT)
  109. {
  110. string HT = "";
  111. Stack<char> S;
  112. int len = TT.size();
  113. for (int i = 0; i < len; i++)
  114. {
  115. if (LaSo(TT[i]))
  116. {
  117. while (LaSo(TT[i]))
  118. HT += TT[i++];
  119. HT += ' ';
  120. }
  121. if (TT[i] == '(')
  122. S.Push(TT[i]);
  123. if (TT[i] == ')')
  124. {
  125. while (S.Top() != '(')
  126. {
  127. HT += S.Top();
  128. S.Pop();
  129. }
  130. S.Pop();
  131. }
  132. if (LaTT(TT[i]))
  133. {
  134. while (!S.Empty() && UT(S.Top()) >= UT(TT[i]))
  135. {
  136. HT += S.Top();
  137. S.Pop();
  138. }
  139. S.Push(TT[i]);
  140. }
  141. }
  142. while (!S.Empty())
  143. {
  144. HT += S.Top();
  145. S.Pop();
  146. }
  147. return HT;
  148. }
  149. float TinhHauTo(string HT)
  150. {
  151. Stack<float> S;
  152. float x = 0, a, b, c;
  153. int len = HT.size();
  154. for (int i = 0; i < len; i++)
  155. {
  156. if (LaSo(HT[i]))
  157. {
  158. while (LaSo(HT[i]))
  159. x = x * 10 + HT[i++] - 48;
  160. S.Push(x);
  161. x = 0;
  162. }
  163. if (LaTT(HT[i]))
  164. {
  165. a = S.Top();
  166. S.Pop();
  167. b = S.Top();
  168. S.Pop();
  169. if (HT[i] == '-') c = b - a;
  170. if (HT[i] == '*') c = b * a;
  171. if (HT[i] == '/') c = b / a;
  172. if (HT[i] == '+') c = b + a;
  173. if (HT[i] == '%') c = int(b) % int(a);
  174. if (HT[i] == '^') c = pow(b , a);
  175. S.Push(c);
  176. }
  177. }
  178. return S.Top();
  179. }
  180. void main()
  181. {
  182. string a = ChuyenTrungToSangHauTo("10-3*(14-2) / (2 + 4 + 8)");
  183. cout << TinhHauTo(a);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement