Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <stdlib.h>
- #include <math.h>
- void SetVectors(std::vector<int> &numVector, int max)
- {
- for (int i = 0; i < max; i++)
- {
- numVector.push_back(i);
- }
- };
- int FindMax(int base, int argumentValue) // Find the max power each can be raised to.
- {
- int maxPower = 1;
- int product = base;
- if (argumentValue == 1)
- {
- return 0;
- }
- while (product < argumentValue)
- {
- product *= base;
- maxPower++;
- }
- return maxPower - 1;
- };
- struct Expression
- {
- int twoPower;
- int threePower;
- int product;
- public:
- Expression(int two, int three){twoPower = two; threePower = three;}
- int get_product(){return pow(3,threePower)*pow(2, twoPower);}
- int get_two(){return twoPower;}
- int get_three(){return threePower;}
- };
- struct ExpressionNode
- {
- private:
- ExpressionNode* left = NULL;
- ExpressionNode* right = NULL;
- public:
- std::vector<Expression> thisExp; // Expression held in the node.
- ExpressionNode* get_left(){return left;}
- void set_left(ExpressionNode* node){left = node;}
- ExpressionNode* get_right(){return right;}
- void set_right(ExpressionNode* node){right = node;}
- };
- class ExpressionTree
- {
- public:
- ExpressionNode* root = NULL; // Root of the tree.
- void insert(ExpressionNode* node); // Insert a node.
- ExpressionNode* find_node(); // Find a node if it's in the tree.
- };
- int CalculateExpression(std::vector<Expression> thisExp);
- int main(int argc, char *argv[])
- {
- ExpressionTree tree;
- std::vector<Expression> myExpressions;
- int maxTwo;
- int maxThree;
- std::vector<int>allTwoPowers;
- std::vector<int>allThreePowers;
- int solve = atoi(argv[1]); // Get the int from the command line.
- if (solve < 5){std::cout << "Cannot be expressed. Exiting..."; return 0;}
- Expression newExp(1, 0);
- Expression baseTwoExp(0, 1);
- ExpressionNode newNode;
- newNode.thisExp.push_back(newExp);
- newExp.threePower = 1;
- newExp.twoPower = 0;
- newNode.thisExp.push_back(newExp);
- tree.insert(&newNode); // Insert base expression as the root.
- maxTwo = FindMax(2, solve);
- maxThree = FindMax(3, solve);
- SetVectors(allTwoPowers, maxTwo);
- SetVectors(allThreePowers, maxThree);
- for (int threePower = 0; threePower <= maxThree; threePower++)
- {
- for (int twoPower = 0; twoPower <= maxTwo; twoPower++)
- {
- Expression newExpression(twoPower, threePower);
- myExpressions.push_back(newExpression);
- }
- }
- return 0;
- }
- void ExpressionTree::insert(ExpressionNode* node)
- {
- ExpressionNode *current = root;
- if (root == NULL) // The tree is empty so far; this is the root node
- {
- root = new ExpressionNode(*node);
- return; // No further action
- }
- do
- {
- if (node->thisExp.size() > root->thisExp.size())
- {
- if (current->get_right() == NULL) // There is no left subtree
- {
- //std::cout << "Right";
- current->set_right(node);
- return;
- }
- else
- {
- current = current->get_right(); // Move to the existing left subtree . . .
- }
- }
- else
- {
- if (current->get_left() == NULL) // There is no right subtree
- {
- current->set_left(node); // Create a new node, pointed to by RIGHT
- return;
- }
- else
- {
- current = current->get_left(); // Move to the existing right subtree . . .
- }
- }
- }
- while (true);
- } // end insert
- int CalculateExpression(std::vector<Expression> thisExp)
- {
- int sum = 0; // Sum of the expressions.
- for (int i = 0; i < thisExp.size(); i++)
- {
- sum += pow(3, thisExp[i].threePower)*pow(2, thisExp[i].twoPower);
- }
- return sum;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement