Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "CTree.h"
- #include <cmath>
- #define _USE_MATH_DEFINES
- #include <math.h>
- #include <iostream>
- using namespace std;
- CTree::CTree()
- {
- root = new CNode;
- }//CTree::CTree()
- CTree::CTree(CTree &otherCTree)
- {
- root = new CNode(*otherCTree.root);
- }//CTree::CTree(CTree &otherCTree)
- CTree::~CTree()
- {
- delete root;
- }//CTree::~CTree()
- CTree CTree::operator+(CTree otherCTree)
- {
- CTree *main_tree = new CTree(*this);
- CNode *node_to_remove = main_tree->findOperationOrValue(main_tree->root);
- otherCTree.root->setParent(node_to_remove->getParent());
- int index = 0;
- while (node_to_remove->getParent()->getChild(index) != node_to_remove)
- index++;
- node_to_remove->getParent()->removeChild(index);
- node_to_remove->getParent()->addChild();
- node_to_remove->getParent()->changeNode(otherCTree.root);
- return(*main_tree);
- }
- void CTree::operator=(CTree &otherTree)
- {
- if (this->root != NULL)
- delete root;
- this->root = new CNode(*otherTree.root);
- }
- void CTree::buildTree(CTree *&tree)
- {
- CNode *current_node = tree->getRoot();
- bool loop_conndition = true;
- int i = 1;
- while (i < formulaVector.size() && loop_conndition)
- {
- if (current_node->getOperationOrVariable().compare("") == 0)
- {
- current_node->setOperationOrVariable(formulaVector[i]);
- if (isMathOperation(formulaVector[i]))
- {
- current_node->addChild();
- current_node = current_node->getLastChild();
- i++;
- }//if (isMathOperation(formulaVector[i]))
- else
- {
- if (current_node == root)
- loop_conndition = false; //po co to tak wlasciwie
- else
- current_node = current_node->getParent();
- }//else if (isMathOperation(formulaVector[i]))
- }//if (currentNode->getOperationOrVariable().compare("") == 0)
- else
- {//jezeli cos jest wpisane w wiezle
- if (current_node->getNumberOfArguments() == current_node->getChildrenSize())
- {
- if (current_node == root)
- loop_conndition = false; //po co to tak wlasciwie
- else
- current_node = current_node->getParent();
- }//if (currentNode->getNumberOfArguments() == currentNode->getNumberOfChildren)
- else
- {
- current_node->addChild();
- current_node = current_node->getLastChild();
- i++;
- }//else if (currentNode->getNumberOfArguments() == currentNode->getNumberOfChildren)
- }// else if (currentNode->getOperationOrVariable().compare("") == 0)
- }//while (i < formulaVector.size() && loop_condition)
- }
- void CTree::enterFormula(string formula)
- {
- formula += SPACE;
- formulaVector.clear();
- //input += SPACE;
- string command = EMPTY;
- int i = 0;
- while (i < formula.size())
- {
- if (formula[i] == CHAR_SPACE && command.compare(EMPTY) != 0)
- {
- formulaVector.push_back(command);
- command = EMPTY;
- }//if (input[0] == CHAR_SPACE && command.compare(EMPTY) != 0)
- else
- {
- command += formula[i];
- }
- i++;
- }//while (i < static_cast<int>(input.size()))
- //wyswietlFormule();
- }//void Manager::enterFormula(string formula)
- void CTree::wyswietlFormule()
- {
- for (int i = 0; i < formulaVector.size(); i++)
- cout<<formulaVector[i];
- }
- bool CTree::setValuesVariables()
- {
- for (int i = 1; i < formulaVector.size(); i++)
- {
- variablesVector[findIndexToChange()]->setOperationOrVariable(formulaVector[i]);
- }
- return true;
- }
- int CTree::findIndexToChange()
- {
- int index = -1;
- bool found = false;
- for (int j = 0; j < variablesVector.size() && !found; j++)
- {
- if (!isNumber(variablesVector[j]->getOperationOrVariable()))
- {
- index = j;
- found = true;
- }
- }
- return index;
- }
- void CTree::preOrder(CNode *¤tNode, string *&preOrderTree)
- {
- if (!root == NULL)
- {
- *preOrderTree += currentNode->getOperationOrVariable() + SPACE;
- if (!currentNode->isLeaf())
- {
- for (int i = 0; i < currentNode->getChildrenSize(); i++)
- {
- preOrder(currentNode->getChild(i), preOrderTree);
- }//for (int i = 0; i < currentNode->getChildrenSize(); i++)
- }//if (!currentNode->isLeaf())
- }//if (!root == NULL)
- }
- void CTree::writeVariables(CNode *currentNode, vector<CNode *> *variablesVector)
- {
- if (!root == NULL)
- {
- if (isNameOfVairable(currentNode->getOperationOrVariable()) || isNumber(currentNode->getOperationOrVariable()))
- {
- bool is_already_in_vector = false;
- for (int i = 0; i <variablesVector->size(); i++)
- {
- if ((*variablesVector)[i]->getOperationOrVariable() == currentNode->getOperationOrVariable())
- is_already_in_vector = true;
- }//for (int i = 0; i < variablesVector->size(); i++)
- if (!is_already_in_vector)
- {
- variablesVector->push_back(currentNode);
- }
- }//if (isNameOfVairable(currentNode->getOperationOrVariable())
- if (!currentNode->isLeaf())
- {
- for (int i = 0; i < currentNode->getChildrenSize(); i++)
- writeVariables(currentNode->getChild(i), variablesVector);
- }//if (!currentNode->isLeaf())
- }//if (!root == NULL)
- }
- void CTree::writeVariables(CNode *currentNode)
- {
- writeVariables(currentNode, &variablesVector);
- }
- bool CTree::shouldTreeBeRepair1(bool &should_repair)
- {
- preOrderAndCheck(this->root, should_repair);
- if (should_repair)
- repairTree();
- return should_repair;
- }
- bool CTree::isNameOfVairable(string name)
- {
- return (!isNumber(name) && !isMathOperation(name));
- }
- bool CTree::isNumber(string number)
- {
- char* p;
- strtol(number.c_str(), &p, 10);
- return *p == 0;
- }
- bool CTree::isMathOperation(string operation)
- {
- if (operation.compare(MINUS) == 0
- || operation.compare(PLUS) == 0
- || operation.compare(MULTIPLICATION) == 0
- || operation.compare(DIVISION) == 0
- || operation.compare(SIN) == 0
- || operation.compare(COS) == 0)
- return true;
- else
- return false;
- }
- void CTree::preOrderAndCheck(CNode *¤tNode, bool &should_repair)
- {
- should_repair = false;
- if (currentNode->getNumberOfArguments() == currentNode->getChildrenSize())
- {
- for (int i = 0; i < currentNode->getChildrenSize(); i++)
- preOrderAndCheck(currentNode->getChild(i), should_repair);
- }
- else
- {
- should_repair = true;
- }
- }
- void CTree::repairTree()
- {
- repairHelp(this->root);
- preOrderAndRepair(this->root);
- }
- void CTree::repairHelp(CNode *¤Node)
- {
- while (currenNode->getNumberOfArguments() != currenNode->getChildrenSize())
- {
- currenNode->addChild();
- }//while (currenNode->getNumberOfArguments() >= currenNode->getChildrenSize())
- if (!currenNode->isLeaf())
- {
- for (int i = 0; i < currenNode->getChildrenSize(); i++)
- repairHelp(currenNode->getChild(i));
- }//if (!currenNode->isLeaf())
- }
- void CTree::preOrderAndRepair(CNode *¤tNode)
- {
- if (currentNode->getOperationOrVariable().compare(EMPTY) == 0)
- {
- currentNode->setValue(DEFAULT_VALUE);
- }
- if (!currentNode->isLeaf())
- {
- for (int i = 0; i < currentNode->getChildrenSize(); i++)
- preOrderAndRepair(currentNode->getChild(i));
- }//if (!currentNode->isLeaf())
- }
- CNode* CTree::getRoot()
- {
- return root;
- }
- CNode*& CTree::getRoot1()
- {
- return root;
- }
- CNode* CTree::findOperationOrValue(CNode *currentNode)
- {
- if (currentNode->isLeaf())
- return currentNode;
- else
- findOperationOrValue(currentNode->getLastChild());
- }
- void CTree::repairNameOfVariable()
- {
- for (int j = 1; j < formulaVector.size(); j++)
- {
- string correct_name_of_variable = EMPTY;
- bool should_be_repair = false;
- for (int i = 0; i < formulaVector[j].size(); i++)
- {
- 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])))
- {
- correct_name_of_variable += formulaVector[j][i];
- }
- else
- {
- should_be_repair = true;
- }
- }
- if (should_be_repair)
- {
- formulaVector.erase(formulaVector.begin() + j);
- formulaVector.insert(formulaVector.begin() + j, correct_name_of_variable);
- }
- }//for (int j = 1; j < formulaVector.size(); j++)
- }
- int CTree::countNotNumberInVector()
- {
- int counter = 0;
- for (int i = 0; i < variablesVector.size(); i++)
- {
- if (!isNumber(variablesVector[i]->getOperationOrVariable()))
- counter++;
- }
- return counter;
- }
- double CTree::calculateValue(CNode *currentNode, bool *byZero)
- {
- for (int i = 0; i < variablesVector.size(); i++)
- variablesVector[i]->wyswietlInf1();
- if (isMathOperation(currentNode->getOperationOrVariable()))
- {
- if (currentNode->getOperationOrVariable() == PLUS)
- return calculateValue(currentNode->getChild(0), byZero) + calculateValue(currentNode->getChild(1), byZero);
- else if (currentNode->getOperationOrVariable() == MINUS)
- return calculateValue(currentNode->getChild(0), byZero) - calculateValue(currentNode->getChild(1), byZero);
- else if (currentNode->getOperationOrVariable() == MULTIPLICATION)
- return calculateValue(currentNode->getChild(0), byZero) * calculateValue(currentNode->getChild(1), byZero);
- else if (currentNode->getOperationOrVariable() == DIVISION)
- {
- if (calculateValue(currentNode->getChild(1), byZero) == 0)
- {
- (*byZero) = true;
- return 0;
- }//if (calculateValue(currentNode->getChild(1), byZero) == 0)
- else
- {
- return calculateValue(currentNode->getChild(0), byZero) / calculateValue(currentNode->getChild(1), byZero);
- }
- }//if (currentNode->getOperationOrVariable() == DIVISION)
- else if (currentNode->getOperationOrVariable() == SIN)
- return sin(calculateValue(currentNode->getChild(0), byZero)*(M_PI / HALF_ANGLE));
- else if (currentNode->getOperationOrVariable() == COS)
- return cos(calculateValue(currentNode->getChild(0), byZero)*(M_PI / HALF_ANGLE));
- }//if (isMathOperation(currentNode->getOperationOrVariable()))
- else
- {
- if (isNumber(currentNode->getOperationOrVariable()))
- {
- cout << stoi(currentNode->getOperationOrVariable());
- return stoi(currentNode->getOperationOrVariable());
- }//if (isNumber(currentNode->getOperationOrVariable()))
- //else
- //return stoi(currentNode->getOperationOrVariable());
- //return currentNode->getValueOfVariable();
- }
- }
- CTree CTree::join(CTree tree)
- {
- CTree *other_tree = new CTree();
- CTree *sum_tree = new CTree();
- //buildTree(other_tree->getRoot1());
- bool *should_repair = new bool();
- other_tree->shouldTreeBeRepair1(*should_repair);
- delete should_repair;
- (*sum_tree) = tree + (*other_tree);
- delete other_tree;
- return (*sum_tree);
- }
- string CTree::getCommand()
- {
- return formulaVector[0];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement