Advertisement
kqlul123

Untitled

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