Advertisement
Jopa322

random sort file

Nov 9th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "time.h"
  4. #include <string>
  5. using namespace std;
  6.  
  7. const int autismos = 5;
  8. const string Names[5] = {
  9. "Test1",
  10. "Test2",
  11. "Best1",
  12. "Best2",
  13. "Cest1",
  14. };
  15.  
  16. struct Bank
  17. {
  18. string name;
  19. int inc[4];
  20. };
  21. class TBank
  22. {
  23. private:
  24. Bank * mass;//
  25. int n;
  26.  
  27. public:
  28. TBank(int nn = 5);
  29. ~TBank();
  30. void input();
  31. void output();
  32. int Max(int Bank);
  33. int Sum(int Bank);
  34. void rnd();
  35. void sort(int POLE, int pr);
  36. void fileOutput(string path, string pathbin);
  37. };
  38.  
  39. TBank::TBank(int nn)
  40. {
  41. n = nn;
  42. mass = new Bank[n];
  43. }
  44.  
  45. TBank::~TBank()
  46. {
  47. delete[]mass;
  48. }
  49.  
  50.  
  51. void TBank::sort(int POLE, int pr)
  52. {
  53. Bank x;
  54. int i, j, b;
  55. for (i = 0; i < n - 1; i++)
  56. {
  57. for (j = i + 1; j < n; j++)
  58. {
  59. switch (POLE)
  60. {
  61.  
  62. case 1:
  63. b = strcmp(mass[i].name.c_str(), mass[j].name.c_str()) > 0;
  64. break;
  65. case 2: case 3: case 4: case 5:
  66. b = mass[i].inc[POLE - 2] > mass[j].inc[POLE - 2];
  67. break;
  68. case 6:
  69. b = Max(i) > Max(j);
  70. break;
  71. case 7:
  72. b = Sum(i) > Sum(j);
  73. break;
  74.  
  75. }
  76. if (pr) b = !b;
  77. if (b)
  78. {
  79. x = mass[i];
  80. mass[i] = mass[j];
  81. mass[j] = x;
  82. }
  83. }
  84. }
  85. }
  86. void TBank::input()
  87. {
  88. for (int i = 0; i < n; i++)
  89. {
  90. cout << "Введите название банка:" << endl;
  91. cin >> mass[i].name;
  92. for (int j = 0; j < 4; j++)
  93. {
  94. cout << "Введите денежные поступления за квартал " << j + 1 << endl;
  95. cin >> mass[i].inc[j];
  96. }
  97. }
  98. }
  99.  
  100. void TBank::rnd()
  101. {
  102. for (int i = 0; i < n; i++)
  103. {
  104. mass[i].name = Names[rand() % autismos];
  105. for (int j = 0; j < 4; j++)
  106. {
  107. mass[i].inc[j] = rand() % 1000 + 1;
  108. }
  109. }
  110. }
  111.  
  112. int TBank::Max(int Bank)
  113. {
  114. int max = 0;
  115. for (int j = 0; j < 4; j++)
  116. {
  117. if (mass[Bank].inc[j] > max)
  118. {
  119. max = mass[Bank].inc[j];
  120. }
  121. }
  122. return max;
  123. }
  124.  
  125. int TBank::Sum(int Bank)
  126. {
  127. int sum = 0;
  128. for (int j = 0; j < 4; j++)
  129. {
  130. sum += mass[Bank].inc[j];
  131. }
  132. return sum;
  133. }
  134.  
  135. void TBank::output()
  136. {
  137. printf("%-12s", "Банк");
  138. printf("%-12s", "1й квартал");
  139. printf("%-12s", "2й квартал");
  140. printf("%-12s", "3й квартал");
  141. printf("%-12s", "4й квартал");
  142. printf("%-12s", "Максимум");
  143. printf("%-12s", "Сумма");
  144. printf("\n");
  145.  
  146. for (int i = 0; i < n; i++)
  147. {
  148. printf("%-12s", mass[i].name.c_str());
  149. printf("%-12d", mass[i].inc[0]);
  150. printf("%-12d", mass[i].inc[1]);
  151. printf("%-12d", mass[i].inc[2]);
  152. printf("%-12d", mass[i].inc[3]);
  153. printf("%-12d", Max(i));
  154. printf("%-12d", Sum(i));
  155. cout << endl;
  156. }
  157. }
  158. void TBank::fileOutput(string path, string pathbin) {
  159. FILE *f, *b;
  160. fopen_s(&f, path.c_str(), "wt");
  161. fprintf(f,"%-12s", "Банк");
  162. fprintf(f,"%-12s", "1й квартал");
  163. fprintf(f,"%-12s", "2й квартал");
  164. fprintf(f,"%-12s", "3й квартал");
  165. fprintf(f,"%-12s", "4й квартал");
  166. fprintf(f,"%-12s", "Максимум");
  167. fprintf(f,"%-12s", "Сумма");
  168. fprintf(f,"\n");
  169.  
  170. for (int i = 0; i < n; i++)
  171. {
  172. fprintf(f,"%-12s", mass[i].name.c_str());
  173. fprintf(f,"%-12d", mass[i].inc[0]);
  174. fprintf(f,"%-12d", mass[i].inc[1]);
  175. fprintf(f,"%-12d", mass[i].inc[2]);
  176. fprintf(f,"%-12d", mass[i].inc[3]);
  177. fprintf(f,"%-12d", Max(i));
  178. fprintf(f,"%-12d", Sum(i));
  179. fprintf(f, "\n");
  180. }
  181. fclose(f);
  182.  
  183. fopen_s(&b, pathbin.c_str(), "wb");
  184. for (int i = 0; i < n; i++)
  185. {
  186. fwrite(mass + i,sizeof(Bank),1,b);
  187. }
  188. fclose(b);
  189. }
  190.  
  191.  
  192. int main()
  193. {
  194. srand(time(0));
  195. setlocale(LC_ALL, "RUS");
  196. int x;
  197. int k;
  198. int qq, wqwqw;
  199. cout << "Введите количество банков в городе:" << endl;
  200. cin >> k;
  201.  
  202. TBank a(k);
  203. cout << "1-random , 0 нет" << endl;
  204. cin >> x;
  205. if (x == 1)a.rnd();
  206. else a.input();
  207. a.output();
  208.  
  209. char option = 1;
  210. do {
  211. system("cls");
  212. a.output();
  213. cout << "Введите номер столбца (1-7) для сортировки" << endl;
  214. cin >> qq;
  215. cout << "1 - по убыванию, 0 - по возрастанию" << endl;
  216. cin >> wqwqw;
  217. a.sort(qq, wqwqw);
  218. system("cls");
  219. cout << "Отсортированная таблица: " << endl;
  220. a.output();
  221. cout << "Нажмите 0 для выхода, любую другую клавишу для продолжения" << endl;
  222. cin >> option;
  223. } while (option != '0');
  224. a.fileOutput("C:\\Users\\STUD\\Desktop\\files\\text.txt", "C:\\Users\\STUD\\Desktop\\files\\test.bin");
  225. system("pause");
  226. return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement