Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. #include <sstream>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. template <class elemType>
  10. struct binaryTreeNode
  11. {
  12. elemType info;
  13. binaryTreeNode<elemType>* llink;
  14. binaryTreeNode<elemType>* rlink;
  15. };
  16.  
  17. template <class elemType>
  18. class binaryTreeType
  19. {
  20. protected:
  21. binaryTreeNode<elemType> *root;
  22.  
  23. public:
  24. bool isEmpty() const;
  25. void insert(const elemType& insertNode);
  26. void inorderTraversal() const;
  27. void postorderTraversal() const;
  28. binaryTreeType();
  29.  
  30.  
  31. private:
  32. void inorder(binaryTreeNode<elemType> *p) const;
  33. void postorder(binaryTreeNode<elemType> *p) const;
  34. };
  35.  
  36. template <class elemType>
  37. binaryTreeType<elemType>::binaryTreeType()
  38. {
  39. root = NULL;
  40. }
  41.  
  42. template <class elemType>
  43. bool binaryTreeType<elemType>::isEmpty() const {
  44. return (root == NULL);
  45. }
  46.  
  47. template <class elemType>
  48. void binaryTreeType<elemType>::inorderTraversal() const
  49. {
  50. inorder(root);
  51. }
  52.  
  53. template <class elemType>
  54. void binaryTreeType<elemType>::postorderTraversal() const
  55. {
  56. postorder(root);
  57. }
  58.  
  59. template <class elemType>
  60. void binaryTreeType<elemType>::inorder(binaryTreeNode<elemType> *p) const
  61. {
  62. if(p !=NULL)
  63. {
  64. inorder(p->llink);
  65. cout<< p->info << " ";
  66. inorder(p->rlink);
  67. }
  68. }
  69.  
  70. template <class elemType>
  71. void binaryTreeType<elemType>::postorder(binaryTreeNode<elemType> *p) const
  72. {
  73. if(p !=NULL)
  74. {
  75. postorder(p->llink);
  76. postorder(p->rlink);
  77. cout<< p->info << " ";
  78. }
  79. }
  80.  
  81. class mathProcessor : public binaryTreeType<string> {
  82. public:
  83. mathProcessor();
  84. void displayStatus();
  85. void processExpression(string &expression);
  86. double compute();
  87.  
  88. private:
  89. stack<string> theStack;
  90. double convertStringToDouble(string const &s);
  91. string convertDoubleToString(const double d);
  92. void reset();
  93.  
  94. bool isDigit(char c);
  95. bool isOperator(char c);
  96. void processExpression(binaryTreeNode<string>* p);
  97. void compute(binaryTreeNode<string> *p);
  98.  
  99. protected:
  100. string expression;
  101. int position;
  102. };
  103.  
  104. void mathProcessor :: reset() {
  105. expression = "";
  106. position = 0;
  107. theStack.empty();
  108. }
  109.  
  110. double mathProcessor::convertStringToDouble(string const &s) {
  111. istringstream iss(s);
  112. double x;
  113. iss >> x;
  114. return x;
  115. }
  116.  
  117. string mathProcessor::convertDoubleToString(const double d) {
  118. ostringstream oss;
  119. oss << d;
  120. return oss.str();
  121. }
  122.  
  123. mathProcessor::mathProcessor() {
  124. reset();
  125. }
  126.  
  127. bool mathProcessor::isDigit(char c) {
  128. return (c == 48 || c == 49 || c == 50 || c == 51 || c == 52 || c == 53 || c == 54 || c == 55 || c == 56 || c == 57);
  129. }
  130.  
  131. bool mathProcessor::isOperator(char c) {
  132. return (c == '+' || c == '-' || c == '*' || c == '/');
  133. }
  134.  
  135. void mathProcessor::processExpression(string& expression) {
  136. if(expression == ""){
  137. return;
  138. }
  139. else {
  140. reset();
  141. this->expression = expression;
  142. binaryTreeNode<string>* newNode;
  143. newNode = new binaryTreeNode<string>;
  144. root = newNode;
  145. processExpression(root);
  146. }
  147. }
  148.  
  149. void mathProcessor::processExpression(binaryTreeNode<string>* p){
  150. do{
  151. if(expression[position] == '('){
  152. binaryTreeNode<string> *newNode;
  153. newNode = new binaryTreeNode<string>;
  154. p->llink = newNode;
  155. newNode->llink = NULL;
  156. newNode->rlink = NULL;
  157. position++;
  158. processExpression(newNode);
  159. }
  160. else if (isDigit(expression[position])){
  161. string temp = " ";
  162. while(isDigit(expression[position])){
  163. temp += expression[position];
  164. position++;
  165. }
  166. p->info = temp;
  167. return;
  168. }
  169. else if(isOperator(expression[position])){
  170. p->info = expression[position];
  171. binaryTreeNode<string> *rightNode;
  172. rightNode = new binaryTreeNode<string>;
  173. p->rlink = rightNode;
  174. rightNode->llink = NULL;
  175. rightNode->rlink = NULL;
  176. position++;
  177. processExpression(rightNode);
  178. }
  179. else if(expression[position] == ')'){
  180. return;
  181. }
  182. }while(position < expression.size());
  183. }
  184.  
  185. //double mathProcessor::compute() {
  186. // compute(root);
  187. //
  188. // return;
  189. //}
  190.  
  191. void mathProcessor::compute(binaryTreeNode<string> *p) {
  192.  
  193. }
  194.  
  195. void mathProcessor::displayStatus() {
  196. cout<< "The data in an in-order traversal is: ";
  197. inorderTraversal();
  198. cout<<endl;
  199. cout<< "The data in a post-order traversal is: ";
  200. postorderTraversal();
  201. cout<<endl;
  202. }
  203.  
  204. int main() {
  205.  
  206. mathProcessor *mp = new mathProcessor;
  207. string expression = "(2+3)";
  208. mp->processExpression(expression);
  209. mp->displayStatus();
  210. //cout << "The result is: " << mp->compute() << endl;
  211. expression = "(123+456)";
  212. mp->processExpression(expression);
  213. mp->displayStatus();
  214. //cout << "The result is: " << mp->compute() << endl;
  215. expression = "(8-5)";
  216. mp->processExpression(expression);
  217. mp->displayStatus();
  218. //cout << "The result is: " << mp->compute() << endl;
  219. expression = "((3-4)-5)";
  220. mp->processExpression(expression);
  221. mp->displayStatus();
  222. //cout << "The result is: " << mp->compute() << endl;
  223. /*expression = "(3*(8/2))";
  224. mp->processExpression(expression);
  225. mp->displayStatus();*/
  226. //cout << "The result is: " << mp->compute() << endl;
  227. /*expression = "((((3+12)-7)*120)/(2+3))";
  228. mp->processExpression(expression);
  229. mp->displayStatus();*/
  230. //cout << "The result is: " << mp->compute() << endl;
  231. /*expression = "(((((9+(2*(110-(20/2))))*8)+1000)/2)-((400*2500)-1000001)";
  232. mp->processExpression(expression);
  233. mp->displayStatus();*/
  234. //cout << "The result is: " << mp->compute() << endl;*/
  235.  
  236. system("pause");
  237. return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement