SkeptaProgrammer

Untitled

Nov 14th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <fstream>
  5. #include<string>
  6. #include <algorithm>
  7. #include <interface.h>
  8. using namespace std;
  9.  
  10. const int SIZE = 250;
  11.  
  12. struct CarBrand
  13. {
  14. string brand;
  15. vector<string> colors;
  16. };
  17.  
  18. struct CarInfo
  19. {
  20. char brand[SIZE];
  21. char color[SIZE];
  22. char fullName[SIZE];
  23. char licensePlate[SIZE];
  24. };
  25.  
  26. int numberOfRecords(char* direction)
  27. {
  28. CarInfo item;
  29. ifstream file;
  30. file.open(direction);
  31. file.seekg(0, ios::end);
  32. int result = file.tellg() / sizeof(item);
  33. file.close();
  34. return result;
  35. }
  36.  
  37. vector<CarInfo> getAllRecordsFromFile(char* direction)
  38. {
  39. int countOfRecords = numberOfRecords(direction);
  40. vector<CarInfo> arrayOfRecords(countOfRecords);
  41. fstream file(direction, ios::binary | ios::in);
  42. for (int i = 0; i < countOfRecords; i++)
  43. file.read(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  44. file.close();
  45. return arrayOfRecords;
  46. }
  47.  
  48. void whichColor(char* direction)
  49. {
  50. vector <string> brands;
  51. vector<CarInfo> car1 = getAllRecordsFromFile(direction);
  52. for (int j = 0; j < car1.size(); j++)
  53. if(find(brands.begin(),brands.end(), car1[j].brand)==brands.end()) // если элемент отсутствует в векторе (==), присутствует(!=)
  54. brands.push_back(car1[j].brand);
  55. vector<CarBrand> car(brands.size());
  56. for (int i = 0; i < car.size(); i++)
  57. {
  58. car[i].brand = brands[i];
  59. for (int j = 0; j < car1.size(); j++)
  60. {
  61. if (car1[j].brand == brands[i])
  62. car[i].colors.push_back(car1[j].color);
  63. }
  64. //sort(car[i].colors.begin(), car[i].colors.end());
  65. }
  66. for (int i = 0; i < car.size(); i++)
  67. {
  68. cout << car[i].brand << "\n";
  69. for (int j = 0; j < car[i].colors.size(); j++)
  70. cout << car[i].colors[j] << "\n";
  71. }
  72. int count=0, maxcount=1;
  73. vector<string> listOfColors;
  74. for (int k = 0; k < car.size(); k++)
  75. {
  76. listOfColors.clear();
  77. for (int i = 0; i < car[k].colors.size(); i++) // Перебираем все элементы массива.
  78. {
  79. count = 0; // Счетчик в 0.
  80. for (int j = i; j < car[k].colors.size(); j++) // Перебираем все элементы от i до конца.
  81. if (car[k].colors[i] == car[k].colors[j]) // Если элемент [i] совпадает с одним из последующих [j],
  82. count++;
  83. if (count == maxcount) // Если очередная буква встречается maxcount раз,
  84. listOfColors.push_back(car[k].colors[i]); // То занесём её в список.
  85. if (count > maxcount) // Если число больше максимального,
  86. {
  87. maxcount = count; // тогда оно максимальное.
  88. listOfColors.clear();
  89. listOfColors.push_back(car[k].colors[i]);
  90. }
  91. }
  92. cout << car[k].brand << " ";
  93. for (int i = 0; i < listOfColors.size(); i++)
  94. cout << listOfColors[i] << " ";
  95. cout << endl;
  96.  
  97. }
  98. }
  99.  
  100. void showRecords(char* direction)
  101. {
  102. int countOfRecords = numberOfRecords(direction);
  103. vector<CarInfo> arrayOfRecords = getAllRecordsFromFile(direction);
  104. for (int i = 0; i < countOfRecords; i++)
  105. {
  106. cout << "\n" << i + 1 << " item\n";
  107. cout << "brand " << arrayOfRecords[i].brand;
  108. cout << "\ncolor " << arrayOfRecords[i].color;
  109. cout << "\nfull name " << arrayOfRecords[i].fullName;
  110. cout << "\nlicense plate " << arrayOfRecords[i].licensePlate << "\n\n";
  111. }
  112. }
  113.  
  114. void addRecordToBack(char* direction)
  115. {
  116. ofstream file;
  117. CarInfo item;
  118. int n = 0;
  119. cout << "Введите кол-во новых записей: "; n = Input(0, 10);
  120. file.open(direction, ios::binary | ios::out | ios::app);
  121. for (int i = 0; i < n; i++)
  122. {
  123. cin.clear();
  124. cout << i + 1 << " item\n";
  125. cout << "brand "; cin.getline(item.brand, SIZE, '\n');
  126. cout << "color "; cin.getline(item.color, SIZE, '\n');
  127. cout << "full name "; cin.getline(item.fullName, SIZE, '\n');
  128. cout << "license plate "; cin.getline(item.licensePlate, SIZE, '\n');
  129. file.write(reinterpret_cast<char*>(&item), sizeof(item));
  130. }
  131. file.close();
  132. }
  133. //
  134.  
  135.  
  136.  
  137. void deleteRecord(char* direction)
  138. {
  139. int countOfRecords = numberOfRecords(direction), n, k = 0;
  140. vector<CarInfo> arrayOfRecords(countOfRecords - 1);
  141. cout << "Введите номер записи для удаления: "; n = Input(0, countOfRecords + 1);
  142. fstream file(direction, ios::binary | ios::in);
  143. for (int i = 0; i < countOfRecords; i++)
  144. {
  145. if (i != n - 1)
  146. {
  147. file.read(reinterpret_cast<char*>(&arrayOfRecords[k]), sizeof(arrayOfRecords[k]));
  148. k++;
  149. }
  150. }
  151. file.close();
  152. file.open(direction, ios::binary | ios::out);
  153. file.close();
  154. file.open(direction, ios::binary | ios::out);
  155. for (int i = 0; i < countOfRecords - 1; i++)
  156. file.write(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  157. file.close();
  158. }
  159.  
  160. void editRecord(char* direction)
  161. {
  162. int n, nField, k = 0;
  163. int countOfRecords = numberOfRecords(direction);
  164. vector<CarInfo> arrayOfRecords = getAllRecordsFromFile(direction);
  165. cout << "Кол-во записей: " << countOfRecords << "\n";
  166. cout << "Введите номер записи для редактирования: "; n = Input(0, countOfRecords + 1);
  167. cout << "\n 1 - brand, 2 - color, 3 - full name, 4 - license plate\n";
  168. cout << "Введите номер поля для редактирования: "; nField = Input(1, 5);
  169.  
  170. switch (nField)
  171. {
  172. case 1:
  173. {
  174. cin.clear();
  175. cout << "brand "; cin.getline(arrayOfRecords[n - 1].brand, SIZE, '\n');
  176. break;
  177. }
  178. case 2:
  179. {
  180. cin.clear();
  181. cout << "color "; cin.getline(arrayOfRecords[n - 1].color, SIZE, '\n');
  182. break;
  183. }
  184. case 3:
  185. {
  186. cin.clear();
  187. cout << "full name "; cin.getline(arrayOfRecords[n - 1].fullName, SIZE, '\n');
  188. break;
  189. }
  190. case 4:
  191. {
  192. cin.clear();
  193. cout << "license plate "; cin.getline(arrayOfRecords[n - 1].licensePlate, SIZE, '\n');
  194. break;
  195. }
  196. }
  197. fstream file(direction, ios::binary | ios::out);
  198. for (int i = 0; i < countOfRecords; i++)
  199. file.write(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  200. file.close();
  201. }
Advertisement
Add Comment
Please, Sign In to add comment