Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "CTree.h"
  3. #include <cmath>
  4. #define _USE_MATH_DEFINES
  5. #include <math.h>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. CTree::CTree()
  11. {
  12. root = new CNode;
  13. }//CTree::CTree()
  14. CTree::CTree(CTree &otherCTree)
  15. {
  16. root = new CNode(*otherCTree.root);
  17. }//CTree::CTree(CTree &otherCTree)
  18. CTree::~CTree()
  19. {
  20. delete root;
  21. }//CTree::~CTree()
  22.  
  23.  
  24. CTree CTree::operator+(CTree otherCTree)
  25. {
  26. CTree *main_tree = new CTree(*this);
  27. CNode *node_to_remove = main_tree->findOperationOrValue(main_tree->root);
  28. otherCTree.root->setParent(node_to_remove->getParent());
  29. int index = 0;
  30. while (node_to_remove->getParent()->getChild(index) != node_to_remove)
  31. index++;
  32. node_to_remove->getParent()->removeChild(index);
  33. node_to_remove->getParent()->addChild();
  34. node_to_remove->getParent()->changeNode(otherCTree.root);
  35. return(*main_tree);
  36. }
  37. void CTree::operator=(CTree &otherTree)
  38. {
  39. if (this->root != NULL)
  40. delete root;
  41. this->root = new CNode(*otherTree.root);
  42. }
  43.  
  44. void CTree::buildTree(CTree *&tree)
  45. {
  46. CNode *current_node = tree->getRoot();
  47. bool loop_conndition = true;
  48. int i = 1;
  49. while (i < formulaVector.size() && loop_conndition)
  50. {
  51. if (current_node->getOperationOrVariable().compare("") == 0)
  52. {
  53. current_node->setOperationOrVariable(formulaVector[i]);
  54. if (isMathOperation(formulaVector[i]))
  55. {
  56. current_node->addChild();
  57. current_node = current_node->getLastChild();
  58. i++;
  59. }//if (isMathOperation(formulaVector[i]))
  60. else
  61. {
  62. if (current_node == root)
  63. loop_conndition = false; //po co to tak wlasciwie
  64. else
  65. current_node = current_node->getParent();
  66. }//else if (isMathOperation(formulaVector[i]))
  67. }//if (currentNode->getOperationOrVariable().compare("") == 0)
  68. else
  69. {//jezeli cos jest wpisane w wiezle
  70. if (current_node->getNumberOfArguments() == current_node->getChildrenSize())
  71. {
  72. if (current_node == root)
  73. loop_conndition = false; //po co to tak wlasciwie
  74. else
  75. current_node = current_node->getParent();
  76. }//if (currentNode->getNumberOfArguments() == currentNode->getNumberOfChildren)
  77. else
  78. {
  79. current_node->addChild();
  80. current_node = current_node->getLastChild();
  81. i++;
  82. }//else if (currentNode->getNumberOfArguments() == currentNode->getNumberOfChildren)
  83. }// else if (currentNode->getOperationOrVariable().compare("") == 0)
  84. }//while (i < formulaVector.size() && loop_condition)
  85. }
  86. void CTree::enterFormula(string formula)
  87. {
  88. formula += SPACE;
  89. formulaVector.clear();
  90. //input += SPACE;
  91. string command = EMPTY;
  92. int i = 0;
  93. while (i < formula.size())
  94. {
  95. if (formula[i] == CHAR_SPACE && command.compare(EMPTY) != 0)
  96. {
  97. formulaVector.push_back(command);
  98. command = EMPTY;
  99. }//if (input[0] == CHAR_SPACE && command.compare(EMPTY) != 0)
  100. else
  101. {
  102. command += formula[i];
  103. }
  104. i++;
  105. }//while (i < static_cast<int>(input.size()))
  106. //wyswietlFormule();
  107. }//void Manager::enterFormula(string formula)
  108. void CTree::wyswietlFormule()
  109. {
  110.  
  111. for (int i = 0; i < formulaVector.size(); i++)
  112. cout<<formulaVector[i];
  113. }
  114. bool CTree::setValuesVariables()
  115. {
  116. for (int i = 1; i < formulaVector.size(); i++)
  117. {
  118. variablesVector[findIndexToChange()]->setOperationOrVariable(formulaVector[i]);
  119. }
  120. return true;
  121. }
  122. int CTree::findIndexToChange()
  123. {
  124. int index = -1;
  125. bool found = false;
  126. for (int j = 0; j < variablesVector.size() && !found; j++)
  127. {
  128. if (!isNumber(variablesVector[j]->getOperationOrVariable()))
  129. {
  130. index = j;
  131. found = true;
  132. }
  133. }
  134. return index;
  135. }
  136.  
  137.  
  138. void CTree::preOrder(CNode *&currentNode, string *&preOrderTree)
  139. {
  140. if (!root == NULL)
  141. {
  142. *preOrderTree += currentNode->getOperationOrVariable() + SPACE;
  143. if (!currentNode->isLeaf())
  144. {
  145. for (int i = 0; i < currentNode->getChildrenSize(); i++)
  146. {
  147. preOrder(currentNode->getChild(i), preOrderTree);
  148. }//for (int i = 0; i < currentNode->getChildrenSize(); i++)
  149. }//if (!currentNode->isLeaf())
  150. }//if (!root == NULL)
  151. }
  152. void CTree::writeVariables(CNode *currentNode, vector<CNode *> *variablesVector)
  153. {
  154. if (!root == NULL)
  155. {
  156. if (isNameOfVairable(currentNode->getOperationOrVariable()) || isNumber(currentNode->getOperationOrVariable()))
  157. {
  158. bool is_already_in_vector = false;
  159. for (int i = 0; i <variablesVector->size(); i++)
  160. {
  161. if ((*variablesVector)[i]->getOperationOrVariable() == currentNode->getOperationOrVariable())
  162. is_already_in_vector = true;
  163. }//for (int i = 0; i < variablesVector->size(); i++)
  164. if (!is_already_in_vector)
  165. {
  166. variablesVector->push_back(currentNode);
  167. }
  168.  
  169. }//if (isNameOfVairable(currentNode->getOperationOrVariable())
  170. if (!currentNode->isLeaf())
  171. {
  172. for (int i = 0; i < currentNode->getChildrenSize(); i++)
  173. writeVariables(currentNode->getChild(i), variablesVector);
  174. }//if (!currentNode->isLeaf())
  175. }//if (!root == NULL)
  176. }
  177. void CTree::writeVariables(CNode *currentNode)
  178. {
  179. writeVariables(currentNode, &variablesVector);
  180. }
  181.  
  182. bool CTree::shouldTreeBeRepair1(bool &should_repair)
  183. {
  184. preOrderAndCheck(this->root, should_repair);
  185. if (should_repair)
  186. repairTree();
  187. return should_repair;
  188. }
  189.  
  190. bool CTree::isNameOfVairable(string name)
  191. {
  192. return (!isNumber(name) && !isMathOperation(name));
  193. }
  194. bool CTree::isNumber(string number)
  195. {
  196. char* p;
  197. strtol(number.c_str(), &p, 10);
  198. return *p == 0;
  199. }
  200. bool CTree::isMathOperation(string operation)
  201. {
  202. if (operation.compare(MINUS) == 0
  203. || operation.compare(PLUS) == 0
  204. || operation.compare(MULTIPLICATION) == 0
  205. || operation.compare(DIVISION) == 0
  206. || operation.compare(SIN) == 0
  207. || operation.compare(COS) == 0)
  208. return true;
  209. else
  210. return false;
  211. }
  212. void CTree::preOrderAndCheck(CNode *&currentNode, bool &should_repair)
  213. {
  214. should_repair = false;
  215. if (currentNode->getNumberOfArguments() == currentNode->getChildrenSize())
  216. {
  217. for (int i = 0; i < currentNode->getChildrenSize(); i++)
  218. preOrderAndCheck(currentNode->getChild(i), should_repair);
  219. }
  220. else
  221. {
  222. should_repair = true;
  223. }
  224. }
  225. void CTree::repairTree()
  226. {
  227. repairHelp(this->root);
  228. preOrderAndRepair(this->root);
  229. }
  230. void CTree::repairHelp(CNode *&currenNode)
  231. {
  232. while (currenNode->getNumberOfArguments() != currenNode->getChildrenSize())
  233. {
  234. currenNode->addChild();
  235. }//while (currenNode->getNumberOfArguments() >= currenNode->getChildrenSize())
  236. if (!currenNode->isLeaf())
  237. {
  238. for (int i = 0; i < currenNode->getChildrenSize(); i++)
  239. repairHelp(currenNode->getChild(i));
  240. }//if (!currenNode->isLeaf())
  241. }
  242. void CTree::preOrderAndRepair(CNode *&currentNode)
  243. {
  244. if (currentNode->getOperationOrVariable().compare(EMPTY) == 0)
  245. {
  246. currentNode->setValue(DEFAULT_VALUE);
  247. }
  248. if (!currentNode->isLeaf())
  249. {
  250. for (int i = 0; i < currentNode->getChildrenSize(); i++)
  251. preOrderAndRepair(currentNode->getChild(i));
  252. }//if (!currentNode->isLeaf())
  253.  
  254. }
  255. CNode* CTree::getRoot()
  256. {
  257. return root;
  258. }
  259. CNode*& CTree::getRoot1()
  260. {
  261. return root;
  262. }
  263. CNode* CTree::findOperationOrValue(CNode *currentNode)
  264. {
  265. if (currentNode->isLeaf())
  266. return currentNode;
  267. else
  268. findOperationOrValue(currentNode->getLastChild());
  269. }
  270.  
  271. void CTree::repairNameOfVariable()
  272. {
  273.  
  274. for (int j = 1; j < formulaVector.size(); j++)
  275. {
  276. string correct_name_of_variable = EMPTY;
  277. bool should_be_repair = false;
  278. for (int i = 0; i < formulaVector[j].size(); i++)
  279. {
  280. if ((formulaVector[j][i] >= 'a' && formulaVector[j][i] <= 'z') || (formulaVector[j][i] >= 'A' && formulaVector[j][i] <= 'Z') || (formulaVector[j][i] >= '0' && formulaVector[j][i] <= '9') || isMathOperation(to_string(formulaVector[j][i])))
  281. {
  282. correct_name_of_variable += formulaVector[j][i];
  283. }
  284. else
  285. {
  286. should_be_repair = true;
  287. }
  288. }
  289. if (should_be_repair)
  290. {
  291. formulaVector.erase(formulaVector.begin() + j);
  292. formulaVector.insert(formulaVector.begin() + j, correct_name_of_variable);
  293. }
  294. }//for (int j = 1; j < formulaVector.size(); j++)
  295. }
  296. int CTree::countNotNumberInVector()
  297. {
  298. int counter = 0;
  299. for (int i = 0; i < variablesVector.size(); i++)
  300. {
  301. if (!isNumber(variablesVector[i]->getOperationOrVariable()))
  302. counter++;
  303. }
  304. return counter;
  305. }
  306.  
  307. double CTree::calculateValue(CNode *currentNode, bool *byZero)
  308. {
  309. for (int i = 0; i < variablesVector.size(); i++)
  310. variablesVector[i]->wyswietlInf1();
  311. if (isMathOperation(currentNode->getOperationOrVariable()))
  312. {
  313. if (currentNode->getOperationOrVariable() == PLUS)
  314. return calculateValue(currentNode->getChild(0), byZero) + calculateValue(currentNode->getChild(1), byZero);
  315. else if (currentNode->getOperationOrVariable() == MINUS)
  316. return calculateValue(currentNode->getChild(0), byZero) - calculateValue(currentNode->getChild(1), byZero);
  317. else if (currentNode->getOperationOrVariable() == MULTIPLICATION)
  318. return calculateValue(currentNode->getChild(0), byZero) * calculateValue(currentNode->getChild(1), byZero);
  319. else if (currentNode->getOperationOrVariable() == DIVISION)
  320. {
  321. if (calculateValue(currentNode->getChild(1), byZero) == 0)
  322. {
  323. (*byZero) = true;
  324. return 0;
  325. }//if (calculateValue(currentNode->getChild(1), byZero) == 0)
  326. else
  327. {
  328. return calculateValue(currentNode->getChild(0), byZero) / calculateValue(currentNode->getChild(1), byZero);
  329. }
  330. }//if (currentNode->getOperationOrVariable() == DIVISION)
  331. else if (currentNode->getOperationOrVariable() == SIN)
  332. return sin(calculateValue(currentNode->getChild(0), byZero)*(M_PI / HALF_ANGLE));
  333. else if (currentNode->getOperationOrVariable() == COS)
  334. return cos(calculateValue(currentNode->getChild(0), byZero)*(M_PI / HALF_ANGLE));
  335. }//if (isMathOperation(currentNode->getOperationOrVariable()))
  336. else
  337. {
  338. if (isNumber(currentNode->getOperationOrVariable()))
  339. {
  340. cout << stoi(currentNode->getOperationOrVariable());
  341. return stoi(currentNode->getOperationOrVariable());
  342. }//if (isNumber(currentNode->getOperationOrVariable()))
  343. //else
  344. //return stoi(currentNode->getOperationOrVariable());
  345. //return currentNode->getValueOfVariable();
  346. }
  347.  
  348. }
  349. CTree CTree::join(CTree tree)
  350. {
  351. CTree *other_tree = new CTree();
  352. CTree *sum_tree = new CTree();
  353. //buildTree(other_tree->getRoot1());
  354. bool *should_repair = new bool();
  355. other_tree->shouldTreeBeRepair1(*should_repair);
  356. delete should_repair;
  357. (*sum_tree) = tree + (*other_tree);
  358. delete other_tree;
  359. return (*sum_tree);
  360. }
  361.  
  362. string CTree::getCommand()
  363. {
  364. return formulaVector[0];
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement