Advertisement
czlowiekzgon

Untitled

Oct 26th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <ctime>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. struct struktura
  11. {
  12. int a;
  13. char ch;
  14. float value;
  15. };
  16.  
  17. struktura ** alokacja(int n);
  18. void wyswietl(struktura ** tab, int n);
  19. void zwolnij_pamiec(struktura ** tab, int n);
  20. void sortowanie(struktura ** tab, int n);
  21. int ile_powtorzen(struktura ** tab, int n, char ch);
  22. bool czy_liczba_powtarza(struktura **tab, int n, int liczba);
  23.  
  24. int main()
  25. {
  26.  
  27. clock_t begin, end;
  28. double time_spent;
  29. begin = clock();
  30.  
  31. ifstream infile;
  32. infile.open("inlab01.txt", ios::in);
  33. if (infile.fail()) {
  34. cout << "problemy z plikiem";
  35. return 0;
  36. }
  37.  
  38. int n;
  39. char ch;
  40.  
  41. infile >> n;
  42. infile >> ch;
  43.  
  44. struktura ** tab;
  45.  
  46. int n = 10000;
  47. if (n > 10000) {
  48. cout << "zakres jest mniejszy od ilosc strukutr" << endl;
  49. return 0;
  50. }
  51.  
  52. int ile;
  53. tab = alokacja(n);
  54. //wyswietl(tab, n);
  55. sortowanie(tab, n);
  56. cout << endl;
  57. wyswietl(tab, n);
  58.  
  59. ile = ile_powtorzen(tab, n, 'D');
  60. cout << "litera D powtorzyła sie " << ile << endl;
  61. zwolnij_pamiec(tab, n);
  62.  
  63. end = clock();
  64. time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  65.  
  66. cout << time_spent;
  67.  
  68.  
  69.  
  70. }
  71. struktura** alokacja(int n) {
  72. srand(time(NULL));
  73. struktura ** tab = new struktura *[n];
  74. for (int i = 0; i < n; i++) {
  75. tab[i] = new struktura;
  76. bool czy_juz_bylo = false;
  77. int liczba;
  78. while (czy_juz_bylo == false) {
  79. liczba = rand() % 10001 - 1000;
  80. czy_juz_bylo = czy_liczba_powtarza(tab, i, liczba);
  81. }
  82.  
  83. tab[i]->a = liczba;
  84. tab[i]->ch = (rand() % 23) + 66;
  85. tab[i]->value = 1000 + (i + 1);
  86.  
  87. }
  88. return tab;
  89. }
  90. void wyswietl(struktura**tab, int n) {
  91. for (int i = 0; i < 20; i++) {
  92. cout << tab[i]->a << endl;
  93.  
  94. cout << tab[i]->ch << endl;
  95. cout << tab[i]->value << endl;
  96. cout << endl;
  97.  
  98. }
  99. }
  100. void zwolnij_pamiec(struktura ** tab, int n) {
  101. for (int i = 0; i < n; i++) {
  102. delete tab[i];
  103. }
  104.  
  105. delete[] tab;
  106. }
  107. void sortowanie(struktura ** tab, int n) {
  108. if (n != 1) {
  109. for (int i = 0; i < n; i++) {
  110. int ile = 0;
  111. for (int j = 0; j < (n - 1) - i; j++) {
  112.  
  113. if (tab[j]->a > tab[j + 1]->a) {
  114.  
  115. struktura *zmienna_pomoc;
  116. zmienna_pomoc = tab[j + 1];
  117. tab[j + 1] = tab[j];
  118. tab[j] = zmienna_pomoc;
  119. ile++;
  120. }
  121.  
  122. }
  123.  
  124. if (ile == 0) {
  125. return;
  126. }
  127.  
  128. }
  129. }
  130.  
  131.  
  132. }
  133. int ile_powtorzen(struktura ** tab, int n, char ch) {
  134. int ile = 0;
  135. for (int i = 0; i < n; i++) {
  136. if (tab[i]->ch == ch) {
  137. ile++;
  138. }
  139. }
  140. return ile;
  141. }
  142. bool czy_liczba_powtarza(struktura **tab, int n, int liczba) {
  143.  
  144. for (int i = 0; i < n; i++) {
  145.  
  146. if (tab[i]->a == liczba) {
  147.  
  148. return false;
  149. }
  150.  
  151. }
  152. return true;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement