Guest User

Untitled

a guest
Apr 14th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. //сериализовать по одному элементу
  2. #include<iostream>
  3. #include<fstream>
  4. #include<string>
  5. #include<vector>
  6. #include<algorithm>
  7. #include<iterator>
  8. #include <utility>
  9. #include <stdexcept>
  10. using namespace :: std;
  11. #pragma pack(push,1)
  12. struct PCRepair
  13. {
  14. string name;
  15. string surname;
  16. int age;
  17. string model;
  18. string status;
  19. struct Date_of_call
  20. {
  21. int day;
  22. int month;
  23. int year;
  24. }date_of_call;
  25. struct Deadline
  26. {
  27. int dday;
  28. int dmonth;
  29. int dyear;
  30. }deadline;
  31. };
  32. #pragma pack(pop)
  33. void vvod(vector<PCRepair> &z)
  34. {
  35. PCRepair fix;
  36. int choice =1;
  37. while(choice==1)
  38. {
  39. cout<<"Имя заказчика: ";
  40. cin>>fix.name;
  41. cout<<"Фамилия заказчика: ";
  42. cin>>fix.surname;
  43. cout<<"Возраст заказчика: ";
  44. cin>>fix.age;
  45. cout<<"Модель ноутбука: ";
  46. cin>>fix.model;
  47. cout<<"Статус выполнения(выполнен/выполняется): ";
  48. cin>>fix.status;
  49. cout<<"Дата поступления заказа, день: ";
  50. cin>>fix.date_of_call.day;
  51. cout<<"Месяц: ";
  52. cin>>fix.date_of_call.month;
  53. cout<<"Год: ";
  54. cin>>fix.date_of_call.year;
  55. cout<<"Дедлайн, день: ";
  56. cin>>fix.deadline.dday;
  57. cout<<"Месяц: ";
  58. cin>>fix.deadline.dmonth;
  59. cout<<"Год: ";
  60. cin>>fix.deadline.dyear;
  61. cout<<"Будете ещё вводить заказы? 1 -да, 0 - нет"<<endl;
  62. cin>>choice;
  63. z.push_back(fix);
  64. }
  65. }
  66. void output(vector<PCRepair> &z, int tmp)
  67. {
  68. cout<<"________________________________"<<endl;
  69. cout<<"| Имя: "<<z[tmp].name<<" |"<<endl;
  70. cout<<"| Фамилия: "<<z[tmp].surname<<" |"<<endl;
  71. cout<<"| Возраст: "<<z[tmp].age<<" Лет"<<" |"<<endl;
  72. cout<<"| Модель: "<<z[tmp].model<<" |"<<endl;
  73. cout<<"| Статус: "<<z[tmp].status<<" |"<<endl;
  74. cout<<"| Дата поступления: "<<z[tmp].date_of_call.day<<"."<<z[tmp].date_of_call.month<<"."<<z[tmp].date_of_call.year<<" |"<<endl;
  75. cout<<"| Дедлайн: "<<z[tmp].deadline.dday<<"."<<z[tmp].deadline.dmonth<<"."<<z[tmp].deadline.dyear<<" |"<<endl;
  76. cout<<"|________________________________|"<<endl;
  77. }
  78. int SEARCH_NAME(vector<PCRepair> &z)
  79. {
  80. PCRepair fix;
  81. int temp;
  82. cout<<"есть возможность найти 1.по ФИО"<<endl<<"2.по Модели ПК"<<endl;
  83. cin>>temp;
  84. switch(temp)
  85. {
  86. case 1:
  87. {
  88. int TETS;
  89. string namee, surnamee;
  90. cout<<"введите имя: ";
  91. cin>>namee;
  92. cout<<"введите фамилию: ";
  93. cin>>surnamee;
  94. for(int i=0; i<z.size(); i++)
  95. {
  96. if(z[i].name == namee && z[i].surname == surnamee)
  97. {
  98. output(z,i);
  99. TETS = i;
  100. return TETS;
  101. }
  102. }
  103. break;
  104. }
  105.  
  106. case 2:
  107. {
  108. string mod;
  109. cout<<"введите модель: ";
  110. cin>>mod;
  111. int TETS;
  112. for(int i=0; i<z.size(); i++)
  113. {
  114. if(z[i].model == mod)
  115. {
  116. output(z,i);
  117. TETS = i;
  118. return TETS;
  119. }
  120. }
  121. break;
  122. }
  123.  
  124. default:
  125. {
  126. cout<<"введено не верное число";
  127. return 0;
  128. break;
  129. }
  130.  
  131. }
  132. }
  133. void change (vector<PCRepair> &z)
  134. {
  135. string tmp;
  136. int i;
  137. cout<<"найдите заказ который вам нужен."<<endl;
  138. i = SEARCH_NAME(z);
  139. cout<<"хотите ли вы изменить статус на противоположный? y/n"<<endl;
  140. cin>>tmp;
  141. if(tmp=="y"&&z[i].status=="выполняется")
  142. {
  143. z[i].status="выполнен";
  144. cout<<"изменения внесены, спасибо"<<endl;
  145. }
  146. if(tmp=="y"&&z[i].status=="выполнен")
  147. {
  148. z[i].status="выполняется";
  149. cout<<"изменения внесены, спасибо"<<endl;
  150. }
  151. else
  152. {
  153. cout<<"изменений не произошло."<<endl;
  154. }
  155. }
  156. void change (vector<PCRepair> &z)
  157. {
  158. string tmp;
  159. int i;
  160. cout<<"найдите заказ который вам нужен."<<endl;
  161. i = SEARCH_NAME(z);
  162. cout<<"хотите ли вы изменить статус на противоположный? y/n"<<endl;
  163. cin>>tmp;
  164. if(tmp=="y"&&z[i].status=="выполняется")
  165. {
  166. z[i].status="выполнен";
  167. cout<<"изменения внесены, спасибо"<<endl;
  168. }
  169. if(tmp=="y"&&z[i].status=="выполнен")
  170. {
  171. z[i].status="выполняется";
  172. cout<<"изменения внесены, спасибо"<<endl;
  173. }
  174. else
  175. {
  176. cout<<"изменений не произошло."<<endl;
  177. }
  178. }
  179. int main()
  180. {
  181. int point;
  182. vector<PCRepair> Bubuntu;
  183. PCRepair YEA;
  184. do
  185. {
  186. cout<<" **************1.ввод структур*****************"<<endl;
  187. cout<<" * ***************2.вывод структур************* *"<<endl;
  188. cout<<" * *************3.поиск элемента************* *"<<endl;
  189. cout<<" * *******4.сортировка по убыванию********* *"<<endl;
  190. cout<<" * ******5.сериализация вектора********** *"<<endl;
  191. cout<<" * *****6.десериализация вектора******** *"<<endl;
  192. cout<<" ************************7.выход***************************"<<endl;
  193. cin>>point;
  194. switch(point)
  195. {
  196. case 1:
  197. vvod(Bubuntu);
  198. break;
  199. case 2:
  200. {
  201. for(int i=0;i<Bubuntu.size();i++)
  202. {
  203. output(Bubuntu,i);
  204. }
  205. break;
  206. }
  207. case 3:
  208. {
  209. int temp;
  210. temp=SEARCH_NAME(Bubuntu);
  211. break;
  212. }
  213. case 4:
  214. output(Bubuntu, 0);
  215. break;
  216. case 5:
  217. {
  218. ofstream out("TEST.txt", std::ios_base::binary | std::ios_base::out);
  219. auto size = Bubuntu.size();
  220. out.write(reinterpret_cast<char*>(&size), sizeof(size));
  221. PCRepair* p = Bubuntu.data();
  222. out.write(reinterpret_cast<char*>(p), sizeof(PCRepair) * size);
  223.  
  224.  
  225. break;
  226. }
  227. case 6 :
  228. try {
  229. ifstream in{"TEST.txt", ios_base::binary};
  230. in.exceptions(in.exceptions() | ios_base::failbit);
  231.  
  232. decltype(Bubuntu.size()) size{};
  233. in.read(reinterpret_cast<char*>(&size), sizeof(size));
  234. if (!size)
  235. throw runtime_error{"Coudn\'t read file!\n"}; // Необходимо #include <stdexcept>
  236.  
  237. vector<PCRepair> v(size);
  238. auto ptrData = v.data();
  239. in.read(reinterpret_cast<char*>(ptrData), sizeof(PCRepair) * size);
  240. Bubuntu = std::move(v); // Необходимо #include <utility>
  241. break;
  242. }
  243. catch (exception const& e) {
  244. cerr << e.what() << endl;
  245. break;
  246. }
  247. default:
  248. cout<<"вы ввели некорректное число"<< endl;
  249. break;
  250. }
  251. }while(point!=7);
  252. return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment