Advertisement
kqlul123

Untitled

Oct 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "cstdlib"
  4. #include "ctime"
  5. #include "string"
  6.  
  7. using namespace std;
  8. const int Kolichestvo = 10;
  9. const string RandomNames[Kolichestvo] = { "Russia" , "USA", "China","Japan","Ukraine","Grysha","Korea","Kazakhstan","Italy","France" };
  10. const int Years = 5;
  11. struct Gold
  12. {
  13. string CountryName;
  14. int GoldMining[Years];
  15. };
  16.  
  17. class TGold
  18. {
  19. private:
  20. Gold * Array;
  21. int NumberOfCountries;
  22. public:
  23. TGold(int);
  24. ~TGold();
  25. void Input();
  26. void RandomInput();
  27. void TableOutput();
  28. int SearchMaxYear(int Gold);
  29. float AverageGold(int Gold);
  30. int RandomNumber(int Min = 1, int MaximumNumber = 10);
  31. void FieldSort(int Field, bool q);
  32. };
  33.  
  34. TGold::TGold(int N) //выдедление оперативной памяти
  35. {
  36. NumberOfCountries = N;
  37. Array = new Gold[NumberOfCountries];
  38. }
  39.  
  40. TGold::~TGold() //деструктор
  41. {
  42. delete[]Array;
  43. }
  44.  
  45. int TGold::RandomNumber(int Min, int MaximumNumber)
  46. {
  47. int rnd = Min + rand() % MaximumNumber;
  48. return rnd;
  49. }
  50.  
  51. void TGold::RandomInput() //ввод количества золотодобывающих стран и случайных данных о них
  52. {
  53. for (int i = 0; i < NumberOfCountries; i++)
  54. {
  55. Array[i].CountryName = RandomNames[rand() % Kolichestvo];
  56. for (int j = 0; j < Years; j++)
  57. {
  58. Array[i].GoldMining[j] = RandomNumber(1, 2000);
  59. }
  60. }
  61.  
  62. }
  63.  
  64. void TGold::Input() //ввод с клавиатуры количества золотодобывающих стран и данных о них
  65. {
  66. for (int i = 0; i < NumberOfCountries; i++)
  67. {
  68. cout << "Введите название страны:" << endl;
  69. cin >> Array[i].CountryName;
  70. for (int j = 0; j < Years; j++)
  71. {
  72. cout << "Введите показатель золотодобычи за год " << j + 1 << endl;
  73. cin >> Array[i].GoldMining[j];
  74. }
  75. }
  76. }
  77.  
  78.  
  79.  
  80. int TGold::SearchMaxYear(int Gold) //поиск года с максимальной добычей в стране
  81. {
  82. int MaxYear = 1;
  83. int MaxValue = Array[Gold].GoldMining[0];
  84. for (int j = 0; j < Years; j++)
  85. {
  86. if (Array[Gold].GoldMining[j] > MaxValue)
  87. {
  88. MaxValue = Array[Gold].GoldMining[j];
  89. MaxYear = j + 1;
  90. }
  91. }
  92. return MaxYear;
  93. }
  94. float TGold::AverageGold(int Gold) //поиск среднего арифметического показателя золотодобычи в стране за 1 год
  95. {
  96. int sum = 0;
  97. for (int j = 0; j < Years; j++)
  98. {
  99. sum += Array[Gold].GoldMining[j];
  100. }
  101.  
  102. return float(sum) / Years;
  103. }
  104.  
  105. void TGold::FieldSort(int Field, bool q)
  106. {
  107. Gold x;
  108. int i, j, tmp;
  109. for (i = 0; i < NumberOfCountries - 1; i++)
  110. {
  111. for (j = i + 1; j < NumberOfCountries; j++)
  112. {
  113. tmp = 0;
  114. switch (Field)
  115. {
  116. case 1:
  117. tmp = strcmp(Array[i].CountryName.c_str(), Array[j].CountryName.c_str()) > 0; break;
  118. case 2:case 3:case 4:case 5: case 6:
  119. tmp = Array[i].GoldMining[Field - 2] > Array[j].GoldMining[Field - 2]; break;
  120. case 7:
  121. tmp = SearchMaxYear(i) > SearchMaxYear(j); break;
  122. case 8:
  123. tmp = AverageGold(i) > AverageGold(j); break;
  124. }
  125. if (q) tmp = !tmp;
  126. if (tmp)
  127. {
  128. x = Array[i];
  129. Array[i] = Array[j];
  130. Array[j] = x;
  131. }
  132. }
  133.  
  134. }
  135. }
  136.  
  137. void TGold::TableOutput() //вывод данных в виде таблицы
  138. {
  139. printf("%-12s", "Страна");
  140. printf("%-12s", "1й год");
  141. printf("%-12s", "2й год");
  142. printf("%-12s", "3й год");
  143. printf("%-12s", "4й год");
  144. printf("%-12s", "5й год");
  145. printf("%-12s", "Макс. год");
  146. printf("%-12s", "Ср. арифм.");
  147. printf("\n");
  148.  
  149. for (int i = 0; i < NumberOfCountries; i++)
  150. {
  151. printf("%-12s", Array[i].CountryName.c_str());
  152. printf("%-12d", Array[i].GoldMining[0]);
  153. printf("%-12d", Array[i].GoldMining[1]);
  154. printf("%-12d", Array[i].GoldMining[2]);
  155. printf("%-12d", Array[i].GoldMining[3]);
  156. printf("%-12d", Array[i].GoldMining[4]);
  157. printf("%-12d", SearchMaxYear(i));
  158. printf("%-12f", AverageGold(i));
  159. cout << endl;
  160. }
  161.  
  162. }
  163.  
  164.  
  165. int main()
  166. {
  167. srand(time(NULL));
  168. setlocale(LC_ALL, "Russian");
  169. int n;
  170. cout << "Введите количество золотодобывающих стран" << endl;
  171. cin >> n;
  172. TGold a(n);
  173. int vvod;
  174. cout << "Случайный ввод - 0, ввод с клавиатуры - 1" << endl;
  175. cin >> vvod;
  176. if (vvod == 0)
  177. a.RandomInput();
  178. else
  179. a.Input();
  180. a.TableOutput();
  181.  
  182.  
  183. int x, y;
  184. cout << "vvedite pole" << endl;
  185. cin >> x;
  186. cout << "vozrastanie/ubivanie" << endl;
  187. cin >> y;
  188. a.FieldSort(x, y);
  189. a.TableOutput();
  190. system("pause");
  191. return 0;
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement