Advertisement
Guest User

Wuerfel

a guest
Jun 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. class Quadrat {
  7. private:
  8.     int Augenwert;
  9. public:
  10.     Quadrat(int augenwert) { Augenwert = augenwert; }
  11. };
  12.  
  13.  
  14. class Wuerfel {
  15. private:
  16.     std::vector<Quadrat> Q;
  17. public:
  18.     Wuerfel();
  19.     ~Wuerfel();
  20.     int wuerfeln();
  21.     int dreimalwuerfeln();
  22. };
  23.  
  24. Wuerfel::Wuerfel()
  25. {
  26.     srand(time(NULL));          //initialisieren des Zufallsgenerators
  27.  
  28.     Quadrat *Wuerfelflaeche = NULL;
  29.  
  30.     for(int i=1; i <= 6; i++) {
  31.         Wuerfelflaeche = new Quadrat(i);        //Speicher für Quadrat allozieren und Zeiger Wuerfelfläche zuweisen
  32.         if (Wuerfelflaeche == NULL) {           //Prüfen ob Speicher bekommen
  33.             std::cerr << "Error! Kein Speicher bekommen!\n";
  34.             exit(EXIT_FAILURE);
  35.         }
  36.         Q.push_back(*Wuerfelflaeche);           //Wuerfelfläche an vector hängen
  37.     }
  38. }
  39.  
  40. Wuerfel::~Wuerfel()
  41. {
  42.     Q.clear();
  43. }
  44.  
  45. int Wuerfel::wuerfeln()
  46. {
  47.     return (rand() % 6) + 1;           //Anzahl der Werte ist 6 (rand() % 6 = Werte von 0-5) deshalb noch + 1)
  48. }
  49.  
  50. int Wuerfel::dreimalwuerfeln()
  51. {
  52.     int augenzahl = 0;
  53.  
  54.     for (int i = 0; i < 3; i++) {
  55.         augenzahl += wuerfeln();
  56.     }
  57.  
  58.     return augenzahl;
  59. }
  60.  
  61. int main()
  62. {
  63.     Wuerfel W;
  64.  
  65.     std::cout << W.wuerfeln() << std::endl;
  66.     std::cout << W.dreimalwuerfeln() << std::endl;
  67.  
  68.     return EXIT_SUCCESS;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement