skitzFist

Lösningsförslag till Uppgifter Pekare

Nov 6th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include "Dice.h"
  3. #include <ctime>
  4.  
  5. void assignmentOne();
  6. void assignmentTwo();
  7. void assignmentThree();
  8. void assignmentFour();
  9.  
  10. int main() {
  11. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  12. srand((unsigned)time(0));
  13.  
  14. //assignmentOne();
  15. //assignmentTwo();
  16. //assignmentThree();
  17. assignmentFour();
  18.  
  19. std::getchar();
  20. return 0;
  21. }
  22.  
  23. void assignmentOne()
  24. {
  25. Dice* ptr = nullptr;
  26. int nrOfSides = 0;
  27. std::cout << "Number of sides: ";
  28. std::cin >> nrOfSides;
  29. std::cin.ignore();
  30.  
  31. ptr = new Dice(nrOfSides);
  32. ptr->toss();
  33. std::cout << "Toss: " << ptr->getValue() << std::endl;
  34.  
  35. Dice* ptr2 = new Dice(*ptr);
  36. ptr2->toss();
  37. std::cout << "Toss: " << ptr2->getValue() << std::endl;
  38.  
  39. Dice* tmp = ptr;
  40. ptr = ptr2;
  41. ptr2 = tmp;
  42.  
  43. delete ptr;
  44. delete ptr2;
  45. }
  46.  
  47. void assignmentTwo()
  48. {
  49. const int CAP = 5;
  50. const int NR_OF_FACES = 9;
  51.  
  52. Dice *arr[CAP];
  53.  
  54. for (int i = 0; i < CAP; ++i) {
  55. arr[i] = new Dice(NR_OF_FACES);
  56. arr[i]->toss();
  57. std::cout << i << ":" << arr[i]->getValue() << std::endl;
  58. }
  59.  
  60. std::cout << std::endl;
  61. Dice *arrReversed[CAP];
  62.  
  63. for (int i = 0; i < CAP; ++i) {
  64. arrReversed[i] = arr[(CAP -1) - i];
  65. std::cout << i << ":" << arrReversed[i]->getValue() << std::endl;
  66. }
  67.  
  68. for (int i = 0; i < CAP; ++i) {
  69. delete arr[i];
  70. }
  71. }
  72.  
  73. void assignmentThree()
  74. {
  75. const int CAP = 4;
  76. Dice** arr = new Dice*[CAP] {nullptr};
  77.  
  78. int nrOfSixes = 0;
  79. for (int i = 0; i < CAP; ++i) {
  80. arr[i] = new Dice();
  81. arr[i]->toss();
  82. std::cout << i << ": " << arr[i]->getValue() << std::endl;
  83. if (arr[i]->getValue() == 6) {
  84. ++nrOfSixes;
  85. }
  86. }
  87. std::cout << "Number sixes rolled: " << nrOfSixes << std::endl;
  88.  
  89. for (int i = 0; i < CAP; ++i) {
  90. delete arr[i];
  91. }
  92. delete[] arr;
  93. }
  94.  
  95. void assignmentFour()
  96. {
  97. int cap = 4;
  98. int nrOf = 0;
  99. const int NR_OF_SIDES = 11;
  100.  
  101. Dice** arr = new Dice*[cap] {nullptr};
  102. for (int i = 0; i < cap; ++i) {
  103. arr[nrOf++] = new Dice(NR_OF_SIDES);
  104. }
  105.  
  106. cap = 7;
  107. Dice** tmp = new Dice*[cap] {nullptr };
  108. for (int i = 0; i < nrOf; ++i) {
  109. tmp[i] = arr[i];
  110. }
  111. delete[]arr;
  112. arr = tmp;
  113.  
  114. for (int i = nrOf; i < cap; ++i) {
  115. arr[nrOf++] = new Dice(NR_OF_SIDES);
  116. }
  117.  
  118. for (int i = 0; i < nrOf; ++i) {
  119. arr[i]->toss();
  120. std::cout << arr[i]->getValue() << std::endl;
  121. }
  122.  
  123.  
  124. for (int i = 0; i < nrOf; ++i) {
  125. delete arr[i];
  126. }
  127. delete[] arr;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment