Advertisement
kqlul123

Untitled

Nov 9th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. // ConsoleApplication16.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];
  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. void TableFileOutput(string path, string pathbin);
  36. };
  37.  
  38. TGold::TGold(int N) //выдедление оперативной памяти
  39. {
  40. NumberOfCountries = N;
  41. Array = new Gold[NumberOfCountries];
  42. }
  43.  
  44. TGold::~TGold() //деструктор
  45. {
  46. delete[]Array;
  47. }
  48.  
  49. int TGold::RandomNumber(int Min, int MaximumNumber)
  50. {
  51. int rnd = Min + rand() % MaximumNumber;
  52. return rnd;
  53. }
  54.  
  55. void TGold::RandomInput() //ввод количества золотодобывающих стран и случайных данных о них
  56. {
  57. for (int i = 0; i < NumberOfCountries; i++)
  58. {
  59. int length = 5;
  60. char *randomname = new char[length];
  61. for (int z = 0; z<length ;z++)
  62. {
  63. randomname[z] = (char)(66 + rand() % 16);
  64. if (z = length) randomname[z] = '\0';
  65. }
  66.  
  67. Array[i].CountryName = randomname;
  68. for (int j = 0; j < Years; j++)
  69. {
  70. Array[i].GoldMining[j] = RandomNumber(1, 2000);
  71. }
  72. }
  73.  
  74. }
  75.  
  76. void TGold::Input() //ввод с клавиатуры количества золотодобывающих стран и данных о них
  77. {
  78. for (int i = 0; i < NumberOfCountries; i++)
  79. {
  80. cout << "Введите название страны:" << endl;
  81. cin >> Array[i].CountryName;
  82. for (int j = 0; j < Years; j++)
  83. {
  84. cout << "Введите показатель золотодобычи за год " << j + 1 << endl;
  85. cin >> Array[i].GoldMining[j];
  86. }
  87. }
  88. }
  89.  
  90.  
  91.  
  92. int TGold::SearchMaxYear(int Gold) //поиск года с максимальной добычей в стране
  93. {
  94. int MaxYear = 1;
  95. int MaxValue = Array[Gold].GoldMining[0];
  96. for (int j = 0; j < Years; j++)
  97. {
  98. if (Array[Gold].GoldMining[j] > MaxValue)
  99. {
  100. MaxValue = Array[Gold].GoldMining[j];
  101. MaxYear = j + 1;
  102. }
  103. }
  104. return MaxYear;
  105. }
  106. float TGold::AverageGold(int Gold) //поиск среднего арифметического показателя золотодобычи в стране за 1 год
  107. {
  108. int sum = 0;
  109. for (int j = 0; j < Years; j++)
  110. {
  111. sum += Array[Gold].GoldMining[j];
  112. }
  113.  
  114. return float(sum) / Years;
  115. }
  116.  
  117. void TGold::FieldSort(int Field, bool q)
  118. {
  119. Gold x;
  120. int i, j, tmp;
  121. for (i = 0; i < NumberOfCountries - 1; i++)
  122. {
  123. for (j = i + 1; j < NumberOfCountries; j++)
  124. {
  125. tmp = 0;
  126. switch (Field)
  127. {
  128. case 1:
  129. tmp = strcmp(Array[i].CountryName.c_str(), Array[j].CountryName.c_str()) > 0; break;
  130. case 2:case 3:case 4:case 5: case 6:
  131. tmp = Array[i].GoldMining[Field - 2] > Array[j].GoldMining[Field - 2]; break;
  132. case 7:
  133. tmp = SearchMaxYear(i) > SearchMaxYear(j); break;
  134. case 8:
  135. tmp = AverageGold(i) > AverageGold(j); break;
  136. }
  137. if (q) tmp = !tmp;
  138. if (tmp)
  139. {
  140. x = Array[i];
  141. Array[i] = Array[j];
  142. Array[j] = x;
  143. }
  144. }
  145.  
  146. }
  147. }
  148.  
  149. void TGold::TableOutput() //вывод данных в виде таблицы
  150. {
  151. printf("%-12s", "Страна");
  152. printf("%-12s", "1й год");
  153. printf("%-12s", "2й год");
  154. printf("%-12s", "3й год");
  155. printf("%-12s", "4й год");
  156. printf("%-12s", "5й год");
  157. printf("%-12s", "Макс. год");
  158. printf("%-12s", "Ср. арифм.");
  159. printf("\n");
  160.  
  161. for (int i = 0; i < NumberOfCountries; i++)
  162. {
  163. printf("%-12s", Array[i].CountryName.c_str());
  164. printf("%-12d", Array[i].GoldMining[0]);
  165. printf("%-12d", Array[i].GoldMining[1]);
  166. printf("%-12d", Array[i].GoldMining[2]);
  167. printf("%-12d", Array[i].GoldMining[3]);
  168. printf("%-12d", Array[i].GoldMining[4]);
  169. printf("%-12d", SearchMaxYear(i));
  170. printf("%-12f", AverageGold(i));
  171. cout << endl;
  172. }
  173.  
  174. }
  175.  
  176. void TGold::TableFileOutput(string path, string pathbin)
  177. {
  178. FILE *kq, *q;
  179. fopen_s(&kq, path.c_str(), "wt");
  180. fprintf(kq, "%-12s", "Страна");
  181. fprintf(kq, "%-12s", "1й год");
  182. fprintf(kq, "%-12s", "2й год");
  183. fprintf(kq, "%-12s", "3й год");
  184. fprintf(kq, "%-12s", "4й год");
  185. fprintf(kq, "%-12s", "5й год");
  186. fprintf(kq, "%-12s", "Макс. год");
  187. fprintf(kq, "%-12s", "Ср. арифм.");
  188. fprintf(kq, "\n");
  189.  
  190. for (int i = 0; i < NumberOfCountries; i++)
  191. {
  192. fprintf(kq, "%-12s", Array[i].CountryName.c_str());
  193. fprintf(kq, "%-12d", Array[i].GoldMining[0]);
  194. fprintf(kq, "%-12d", Array[i].GoldMining[1]);
  195. fprintf(kq, "%-12d", Array[i].GoldMining[2]);
  196. fprintf(kq, "%-12d", Array[i].GoldMining[3]);
  197. fprintf(kq, "%-12d", Array[i].GoldMining[4]);
  198. fprintf(kq, "%-12d", SearchMaxYear(i));
  199. fprintf(kq, "%-12f", AverageGold(i));
  200. fprintf(kq, "\n");
  201. }
  202. fclose(kq);
  203.  
  204.  
  205. fopen_s(&q, pathbin.c_str(), "wb");
  206. for (int i=0; i < 5; i++)
  207. {
  208. fwrite(Array + i, sizeof(Gold), 1, q);
  209. }
  210. fclose(q);
  211. }
  212.  
  213. int main()
  214. {
  215. srand(time(NULL));
  216. setlocale(LC_ALL, "Russian");
  217. int n;
  218. cout << "Введите количество золотодобывающих стран" << endl;
  219. cin >> n;
  220. TGold a(n);
  221. int vvod;
  222. cout << "Случайный ввод - 0, ввод с клавиатуры - 1" << endl;
  223. cin >> vvod;
  224. if (vvod == 0)
  225. a.RandomInput();
  226. else
  227. a.Input();
  228. a.TableOutput();
  229. int x, y;
  230.  
  231. bool w = 1;
  232. while (w)
  233. {
  234. cout << "Введите поле для сортировки (1-8)" << endl;
  235. cin >> x;
  236. cout << "Сортировка по возрастанию - 0, по убыванию - 1" << endl;
  237. cin >> y;
  238. a.FieldSort(x, y);
  239. a.TableOutput();
  240. do {
  241. cout << "Продолжать сортировать таблицу(1 - да, 0 - нет)" << endl;
  242. cin >> w;
  243. } while (w < 0 && w> 1);
  244. if (w)
  245. system("CLS");
  246. a.TableOutput();
  247. }
  248. a.TableFileOutput("C:\\Users\\stud\\Desktop\\Файлы\\text.txt", "C:\\Users\\stud\\Desktop\\Файлы\\file.bin");
  249.  
  250.  
  251. system("pause");
  252. return 0;
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement