Advertisement
Guest User

Untitled

a guest
Nov 15th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. void SetVectors(std::vector<int> &numVector, int max)
  8. {
  9.     for (int i = 0; i < max; i++)
  10.     {
  11.     numVector.push_back(i);
  12.     }
  13. };
  14.  
  15. int FindMax(int base, int argumentValue)    // Find the max power each can be raised to.
  16. {
  17.     int maxPower = 1;
  18.     int product = base;
  19.    
  20.     if (argumentValue == 1)
  21.     {
  22.         return 0;
  23.     }
  24.    
  25.     while (product < argumentValue)
  26.     {
  27.         product *= base;
  28.         maxPower++;
  29.     }
  30.    
  31.     return maxPower - 1;
  32. };
  33.  
  34. struct Expression
  35. {
  36.     int twoPower;
  37.     int threePower;
  38.     int product;
  39.   public:
  40.     Expression(int two, int three){twoPower = two; threePower = three;}
  41.     int get_product(){return pow(3,threePower)*pow(2, twoPower);}
  42.     int get_two(){return twoPower;}
  43.     int get_three(){return threePower;}
  44. };
  45. struct ExpressionNode
  46. {
  47.     private:
  48.         ExpressionNode* left = NULL;
  49.         ExpressionNode* right = NULL;
  50.    
  51.     public:
  52.         std::vector<Expression> thisExp;    // Expression held in the node.
  53.         ExpressionNode* get_left(){return left;}
  54.         void set_left(ExpressionNode* node){left = node;}
  55.         ExpressionNode* get_right(){return right;}
  56.         void set_right(ExpressionNode* node){right = node;}
  57. };
  58. class ExpressionTree
  59. {
  60.     public:
  61.         ExpressionNode* root = NULL;                   // Root of the tree.
  62.         void insert(ExpressionNode* node); // Insert a node.
  63.         ExpressionNode* find_node();    // Find a node if it's in the tree.
  64. };
  65. int CalculateExpression(std::vector<Expression> thisExp);
  66.  
  67. int main(int argc, char *argv[])
  68. {
  69.     ExpressionTree tree;
  70.     std::vector<Expression> myExpressions;
  71.     int maxTwo;
  72.     int maxThree;
  73.     std::vector<int>allTwoPowers;
  74.     std::vector<int>allThreePowers;
  75.    
  76.     int solve = atoi(argv[1]);  // Get the int from the command line.
  77.     if (solve < 5){std::cout << "Cannot be expressed. Exiting..."; return 0;}
  78.    
  79.     Expression newExp(1, 0);
  80.     Expression baseTwoExp(0, 1);
  81.    
  82.     ExpressionNode newNode;
  83.     newNode.thisExp.push_back(newExp);
  84.  
  85.     newExp.threePower = 1;
  86.     newExp.twoPower = 0;
  87.     newNode.thisExp.push_back(newExp);
  88.    
  89.     tree.insert(&newNode);  // Insert base expression as the root.
  90.    
  91.     maxTwo = FindMax(2, solve);
  92.     maxThree = FindMax(3, solve);
  93.    
  94.     SetVectors(allTwoPowers, maxTwo);
  95.     SetVectors(allThreePowers, maxThree);
  96.    
  97.     for (int threePower = 0; threePower <= maxThree; threePower++)
  98.     {
  99.     for (int twoPower = 0; twoPower <= maxTwo; twoPower++)
  100.     {
  101.         Expression newExpression(twoPower, threePower);
  102.         myExpressions.push_back(newExpression);
  103.     }
  104.     }
  105.    
  106.    
  107.    
  108.     return 0;
  109. }
  110.  
  111. void ExpressionTree::insert(ExpressionNode* node)
  112. {
  113.     ExpressionNode *current = root;
  114.  
  115.     if (root == NULL)  // The tree is empty so far; this is the root node
  116.     {
  117.     root = new ExpressionNode(*node);
  118.     return;  // No further action
  119.     }
  120.        
  121.     do
  122.     {
  123.     if (node->thisExp.size() > root->thisExp.size())
  124.     {
  125.         if (current->get_right() == NULL)  // There is no left subtree
  126.         {
  127.                 //std::cout << "Right";
  128.         current->set_right(node);
  129.         return;
  130.         }
  131.         else
  132.         {
  133.         current = current->get_right();  // Move to the existing left subtree . . .
  134.         }
  135.     }
  136.     else
  137.     {
  138.         if (current->get_left() == NULL)  // There is no right subtree
  139.         {
  140.         current->set_left(node); // Create a new node, pointed to by RIGHT
  141.         return;
  142.         }
  143.         else
  144.         {
  145.         current = current->get_left(); // Move to the existing right subtree . . .
  146.         }
  147.     }
  148.     }
  149.     while (true);
  150. } // end insert
  151.  
  152. int CalculateExpression(std::vector<Expression> thisExp)
  153. {
  154.     int sum = 0;    // Sum of the expressions.
  155.    
  156.     for (int i = 0; i < thisExp.size(); i++)
  157.     {
  158.         sum += pow(3, thisExp[i].threePower)*pow(2, thisExp[i].twoPower);
  159.     }
  160.     return sum;
  161. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement