jkhsjdhjs

Numbersearch Operationchain

Jun 28th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <conio.h>
  5. #include <string>
  6. #include <ctime>
  7.  
  8. class node {
  9. public:
  10.     node(int v, node* p = NULL) {
  11.         this->value = v;
  12.         this->prev = p;
  13.     }
  14.     int value;
  15.     node* prev;
  16.     node* firstChild = NULL;
  17.     node* secondChild = NULL;
  18.     node* thirdChild = NULL;
  19.     node* fourthChild = NULL;
  20.     void createNodes();
  21. };
  22.  
  23. class graph {
  24. public:
  25.     graph(node* s) {
  26.         this->start = s;
  27.     }
  28.     node* start;
  29.     node* search(int);
  30. };
  31.  
  32. void node::createNodes() {
  33.     //create child nodes
  34.     if(this->value * 3 >= 0) this->firstChild = new node(this->value * 3, this);
  35.     if(this->value * 2 - 323 >= 0) this->secondChild = new node(this->value * 2 - 323, this);
  36.     if(this->value + 27 >= 0) this->thirdChild = new node(this->value + 27, this);
  37.     if(this->value - 13 >= 0) this->fourthChild = new node(this->value - 13, this);
  38. }
  39.  
  40. node* graph::search(int search) {
  41.     std::vector<node*> layer, beta;
  42.     //add start node to vector
  43.     beta.push_back(start);
  44.     while(true) {
  45.         //add previous child nodes to the list of nodes generated childs for
  46.         layer = beta;
  47.         //clear child node vector
  48.         beta.clear();
  49.         //parse every node in vector
  50.         for each(node* n in layer) {
  51.             //create child nodes for each node
  52.             n->createNodes();
  53.             //check each child node if it equals the searched number
  54.             if(n->firstChild != NULL) {
  55.                 if(n->firstChild->value == search) return n->firstChild;
  56.                 beta.push_back(n->firstChild);
  57.             }
  58.             if(n->secondChild != NULL) {
  59.                 if(n->secondChild->value == search) return n->secondChild;
  60.                 beta.push_back(n->secondChild);
  61.             }
  62.             if(n->thirdChild != NULL) {
  63.                 if(n->thirdChild->value == search) return n->thirdChild;
  64.                 beta.push_back(n->thirdChild);
  65.             }
  66.             if(n->fourthChild != NULL) {
  67.                 if(n->fourthChild->value == search) return n->fourthChild;
  68.                 beta.push_back(n->fourthChild);
  69.             }
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75. int main() {
  76.     setlocale(LC_ALL, "German");
  77.     int to_find = 0;
  78.     node* found = NULL;
  79.     std::string opChain = "";
  80.     std::cout << "Dieses Programm sucht eine Operationskette von 1 zu einer bestimmten, positiven Zahl." << std::endl << "Dabei dürfen nur die folgenden Operationen verwendet werden:" << std::endl;
  81.     std::cout << "- Multiplikation mit 3" << std::endl;
  82.     std::cout << "- Multiplikation mit 2 und anschließende Subtraktion von 323" << std::endl;
  83.     std::cout << "- Addition von 27" << std::endl;
  84.     std::cout << "- Subtraktion von 13" << std::endl;
  85.     std::cout << std::endl << "Geben Sie nun eine Zahl ein, zu der die kürzeste Operationskette von 1 aus gefunden werden soll: ";
  86.     while(!(std::cin >> to_find) || to_find < 0) {
  87.         std::cin.clear();
  88.         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  89.         std::cout << "Ihre Eingabe war fehlerhaft!" << std::endl << "Bitte wiederholen Sie Ihre Eingabe: ";
  90.     }
  91.     std::cout << "Berechne...";
  92.     clock_t begin = clock();
  93.     if(to_find != 1) {
  94.         graph*  g = new graph(new node(1));
  95.         found = g->search(to_find);
  96.         do {
  97.             if(found->value == found->prev->value * 3) {
  98.                 opChain += '1';
  99.             }
  100.             else if(found->value == found->prev->value * 2 - 323) {
  101.                 opChain += '2';
  102.             }
  103.             else if(found->value == found->prev->value + 27) {
  104.                 opChain += '3';
  105.             }
  106.             else if(found->value == found->prev->value - 13) {
  107.                 opChain += '4';
  108.             }
  109.             found = found->prev;
  110.         } while(found->prev != NULL);
  111.     }
  112.     clock_t end = clock();
  113.     double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  114.     std::cout << "Fertig!" << std::endl << "Benötigte Zeit: " << elapsed_secs << "s" << std::endl << std::endl << "Kürzester Weg von 1 zu " << to_find << " (" << opChain.length() << " Schritte):" << std::endl;
  115.     int n = 1;
  116.     for(int i = opChain.length(); i >= 0; i--) {
  117.         switch(opChain[i]) {
  118.         case '1':
  119.             std::cout << ">>Multiplikation mit 3: " << n << " * 3 = " << n * 3 << std::endl;
  120.             n *= 3;
  121.             break;
  122.         case '2':
  123.             std::cout << ">>Multiplikation mit 2 und anschließende Subtraktion von 323: " << n << " * 2 - 323 = " << n * 2 - 323 << std::endl;
  124.             n = n * 2 - 323;
  125.             break;
  126.         case '3':
  127.             std::cout << ">>Addition von 27: " << n << " + 27 = " << n + 27 << std::endl;
  128.             n += 27;
  129.             break;
  130.         case '4':
  131.             std::cout << ">>Subtraktion von 13: " << n << " - 13 = " << n - 13 << std::endl;
  132.             n -= 13;
  133.             break;
  134.         }
  135.     }
  136.     std::cout << std::endl << "Drücken Sie eine beliebige Taste zum Beenden...";
  137.     _getch();
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment