Advertisement
kqlul123

132

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