Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class Kulka
  7. {
  8. public:
  9. int wartosc;
  10. Kulka *next;
  11. Kulka *lewe_d;
  12. Kulka *prawe_d;
  13. Kulka()
  14. {
  15. wartosc = rand() % 100 + 100;
  16. next = NULL;
  17. lewe_d = NULL;
  18. prawe_d = NULL;
  19. }
  20. Kulka(int wartosc)
  21. {
  22. this->wartosc = wartosc;
  23. next = NULL;
  24. lewe_d = NULL;
  25. prawe_d = NULL;
  26. }
  27. void show_l()
  28. {
  29. cout << "Wartosc kulki: " << wartosc << endl;
  30. if (next != NULL)
  31. next->show_l();
  32. }
  33. void dodaj_l(Kulka *nowa)
  34. {
  35. if (next != 0)
  36. {
  37. if (nowa->wartosc<next->wartosc)
  38. {
  39. next->dodaj_l(nowa);
  40. }
  41. else
  42. {
  43. nowa->next = next;
  44. next = nowa;
  45. }
  46. }
  47. else
  48. next = nowa;
  49. }
  50. void srednia(double &sredn, int &liczn)
  51. {
  52. sredn = sredn + wartosc;
  53. if (next != NULL)
  54. next->srednia(sredn, liczn);
  55. else
  56. sredn = sredn / liczn;
  57. }
  58. void powysred(double &sredn)
  59. {
  60. if (wartosc>sredn)
  61. cout << "Wartosc kulki w sortowanej: " << wartosc << endl;
  62. if (next != NULL)
  63. next->powysred(sredn);
  64. }
  65. void powysred(double &sredn, Kulka *&lista2)
  66. {
  67. if (wartosc > sredn) {
  68. cout << "Wartosc kulki w sortowanej: " << wartosc << endl;
  69. Kulka *temp = NULL;
  70. temp = new Kulka(wartosc);
  71. lista2->dodaj_l(temp);
  72. }
  73. if (next != NULL)
  74. next->powysred(sredn);
  75. }
  76.  
  77. };
  78. int main()
  79. {
  80. Kulka *nowa = NULL;
  81. Kulka *lista = NULL;
  82. Kulka *lista2 = NULL;
  83. double srednia = 0;
  84. int licznik = 0;
  85. for (int i = 0; i<10; i++)
  86. {
  87. nowa = new Kulka();
  88. if (lista == NULL)
  89. lista = nowa;
  90. else
  91. {
  92. if (lista->wartosc < nowa->wartosc)
  93. {
  94. nowa->next = lista;
  95. lista = nowa;
  96. }
  97. else
  98. {
  99. lista->dodaj_l(nowa);
  100.  
  101. }
  102. }
  103. licznik++;
  104. }
  105. lista->show_l();
  106. lista->srednia(srednia, licznik);
  107. cout << "Srednia: " << srednia;
  108. cout << endl;
  109. cout << "Lista powyzej: " << endl;
  110. //lista->powysred(srednia);
  111.  
  112. lista->powysred(srednia,lista2);
  113. lista2->show_l;
  114.  
  115. cout << "Lista sort: " << endl;
  116. lista->show_l();
  117.  
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement