Advertisement
Uncleeee

KursMash

Nov 15th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <time.h>
  4. #include <fstream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <iterator>
  8. #include <cstring>
  9.  
  10. using namespace std;
  11.  
  12. int number_lines = 0;
  13.  
  14. struct autosalon
  15. {
  16. char mark[50];
  17. char firm[50];
  18. char date_manuf[50];
  19. int mileage;
  20. int price;
  21. };
  22.  
  23. void read_file(autosalon *autos, string name_file, int flag)
  24. {
  25. ifstream fin(name_file);
  26.  
  27. if (!fin.is_open()) cout<<"файл не открыт";
  28. else
  29. {
  30. if (name_file == "autosalon.txt")
  31. {
  32. number_lines = 0;
  33. while (!fin.eof())
  34. {
  35. fin >> autos[number_lines].mark >> autos[number_lines].firm >> autos[number_lines].date_manuf >> autos[number_lines].mileage >> autos[number_lines].price;
  36. if (flag==1)
  37. {
  38. cout << autos[number_lines].mark << " " << autos[number_lines].firm << " " << autos[number_lines].date_manuf << " " << autos[number_lines].mileage << " " << autos[number_lines].price;
  39. cout<< "\n";
  40. }
  41. number_lines++;
  42. }
  43. }
  44. }
  45.  
  46. }
  47.  
  48. void write_file(string name_file, string input_string)
  49. {
  50. ofstream fout(name_file, ios::app);
  51. fout << endl;
  52. fout << input_string;
  53. }
  54.  
  55. void filter_by_price(autosalon *autos, string name_file, int filter)
  56. {
  57. read_file(autos, name_file, 0);
  58.  
  59. for (int i=0; i<number_lines; i++)
  60. {
  61. if (autos[i].price <= filter)
  62. {
  63. cout << autos[i].mark << " " << autos[i].date_manuf << " " << autos[i].price;
  64. cout<< "\n";
  65. }
  66. }
  67. }
  68.  
  69.  
  70. int remove_line(const char *filename, size_t index)
  71. {
  72. vector<string> vec;
  73. ifstream file(filename);
  74. if (file.is_open())
  75. {
  76. string str;
  77. while (getline(file, str))
  78. vec.push_back(str);
  79. file.close();
  80. if (vec.size() < index)
  81. return -1;
  82. vec.erase(vec.begin() + index);
  83. ofstream outfile(filename);
  84. if (outfile.is_open())
  85. {
  86. copy(vec.begin(), vec.end(),
  87. ostream_iterator<string>(outfile, "\n"));
  88. outfile.close();
  89. return 1;
  90. }
  91. return -2;
  92. }
  93. return -3;
  94. }
  95.  
  96. void remove_by_firm(autosalon *autos, string name_file, string firm)
  97. {
  98. read_file(autos, name_file, 0);
  99.  
  100. int x[number_lines];
  101. int j = 0;
  102. for (int i=0; i<number_lines; i++)
  103. {
  104. if (autos[i].firm == firm)
  105. {
  106. x[j] = i;
  107. j++;
  108. }
  109. }
  110.  
  111. for (int i=0; i<j; i++)
  112. {
  113. remove_line("autosalon.txt", x[i]);
  114. for (int g=i+1; g<j; g++) x[g]--;
  115. }
  116. }
  117.  
  118.  
  119. void rewrite_file(string name_file, string input_string)
  120. {
  121. ofstream refout(name_file);
  122. refout << "";
  123. refout.close();
  124. ofstream rfout(name_file, ios::app);
  125. rfout << input_string;
  126. }
  127.  
  128. void new_file(autosalon *autos, string name_file, string new_name_file)
  129. {
  130. read_file(autos, name_file, 0);
  131.  
  132. ofstream refout(new_name_file);
  133. refout << "";
  134. refout.close();
  135.  
  136. ofstream anfout(new_name_file, ios::app);
  137.  
  138. for (int i=0; i<number_lines; i++)
  139. {
  140. anfout << autos[i].mark << " " << autos[i].mileage << " " << autos[i].price;
  141. anfout << endl;
  142.  
  143. cout << autos[i].mark << " " << autos[i].mileage << " " << autos[i].price;
  144. cout << endl;
  145. }
  146.  
  147. }
  148.  
  149. void set_mark_price(autosalon *autos, string name_file, string old_mark, char new_mark[50], int new_price)
  150. {
  151. read_file(autos, name_file, 0);
  152.  
  153. for (int i=0; i<number_lines; i++)
  154. {
  155. if (autos[i].mark == old_mark)
  156. {
  157. strcpy(autos[i].mark, new_mark);
  158. autos[i].price = new_price;
  159.  
  160. }
  161. }
  162.  
  163. ofstream refout(name_file);
  164. refout << "";
  165. refout.close();
  166.  
  167. ofstream anfout(name_file, ios::app);
  168.  
  169. for (int i=0; i<number_lines; i++)
  170. {
  171. anfout << autos[i].mark << " " << autos[i].firm << " " << autos[i].date_manuf << " " << autos[i].mileage << " " << autos[i].price;
  172. anfout << endl;
  173.  
  174. }
  175. }
  176.  
  177.  
  178. void menu(autosalon *autos)
  179. {
  180.  
  181. cout << "Автосалон \n";
  182. cout << "1. Меню изменения файла базы данных." << endl;
  183. cout << "2. Выдать на экран содержимое файла полной базы данных." << endl;
  184. cout << "3. Выдать на экран все автомобили, с ценой не больше заданной с указанием даты выпуска." << endl;
  185. cout << "4. Сформировать файл: марка автомобиля пробег цена" << endl;
  186. cout << "5. Выдать на экран содержимое файла: марка автомобиля пробег цена." << endl;
  187. int count;
  188. cin >> count;
  189. char choise;
  190. if (count == 1)
  191. {
  192. cout << "Нажмите 1 чтобы добавить запись в файл" << endl;
  193. cout << "Нажмите 2 чтобы затереть файл и писать с начала" << endl;
  194. cout << "Нажмите 3 чтобы для заданной марки автомобиля изменить марка цену." << endl;
  195. cout << "Нажмите 4 чтобы Удалить все записи указанной фирмы изготовителя." << endl;
  196.  
  197.  
  198. cin >> choise;
  199.  
  200. if (choise == '1')
  201. {
  202. cout << "Введите строку формата: марка фирма дата_выпуска пробег цена " << endl;
  203.  
  204. string mark, firm, date, mileage, price;
  205. cin >> mark;
  206. cin >> firm;
  207. cin >> date;
  208. cin >> mileage;
  209. cin >> price;
  210. string new_string;
  211. new_string = new_string + mark + " " + firm + " " + date + " " + mileage + " " + price;
  212. write_file("autosalon.txt", new_string);
  213. cout << endl;
  214. cout << "Чтобы показать Меню нажмите m." << endl;
  215. cout << "Чтобы показать измененный файл нажмите r." << endl;
  216. cin >> choise;
  217.  
  218. if (choise == 'm') menu(autos);
  219.  
  220. if (choise == 'r')
  221. {
  222. read_file(autos, "autosalon.txt", 1);
  223.  
  224. cout << "Показать меню? y/n " << endl;
  225.  
  226. cin >> choise;
  227.  
  228. if (choise == 'y')
  229. {
  230. cout<<endl<<endl;
  231. menu(autos);
  232. }
  233. else
  234. {
  235. cout << "End programm";
  236. }
  237. }
  238.  
  239. }
  240.  
  241. if (choise == '2')
  242. {
  243. cout << "Введите строку формата: марка фирма дата_выпуска пробег цена " << endl;
  244.  
  245. string mark, firm, date, mileage, price;
  246. cin >> mark;
  247. cin >> firm;
  248. cin >> date;
  249. cin >> mileage;
  250. cin >> price;
  251. string new_string;
  252. new_string = new_string + mark + " " + firm + " " + date + " " + mileage + " " + price;
  253. rewrite_file("autosalon.txt", new_string);
  254. cout << endl;
  255. cout << "Чтобы показать Меню нажмите m." << endl;
  256. cout << "Чтобы показать измененный файл нажмите r." << endl;
  257. cin >> choise;
  258.  
  259. if (choise == 'm') menu(autos);
  260.  
  261. if (choise == 'r')
  262. {
  263. read_file(autos, "autosalon.txt", 1);
  264.  
  265. cout << "Показать меню? y/n " << endl;
  266.  
  267. cin >> choise;
  268.  
  269. if (choise == 'y')
  270. {
  271. cout<<endl<<endl;
  272. menu(autos);
  273. }
  274. else
  275. {
  276. cout << "End programm";
  277. }
  278. }
  279. }
  280.  
  281. if (choise == '3')
  282. {
  283. char mark[50];
  284. cout << "Введите марку автомобиля" << endl;
  285. cin >> mark;
  286.  
  287. char mark1[50];
  288. cout << "Введите новую марку автомобиля" << endl;
  289. cin >> mark1;
  290.  
  291. int price;
  292. cout << "Введите новую цену автомобиля" << endl;
  293. cin >> price;
  294.  
  295. set_mark_price(autos, "autosalon.txt", mark, mark1, price);
  296.  
  297. cout << endl;
  298. cout << "Чтобы показать Меню нажмите m." << endl;
  299. cout << "Чтобы показать измененный файл нажмите r." << endl;
  300. cin >> choise;
  301.  
  302. if (choise == 'm') menu(autos);
  303.  
  304. if (choise == 'r')
  305. {
  306. read_file(autos, "autosalon.txt", 1);
  307.  
  308. cout << "Показать меню? y/n " << endl;
  309.  
  310. cin >> choise;
  311.  
  312. if (choise == 'y')
  313. {
  314. cout<<endl<<endl;
  315. menu(autos);
  316. }
  317. else
  318. {
  319. cout << "End programm";
  320. }
  321. }
  322.  
  323. }
  324.  
  325. if (choise == '4')
  326. {
  327. cout << "Введите фирму" << endl;
  328.  
  329. char firm[50];
  330. cin >> firm;
  331.  
  332. remove_by_firm(autos, "autosalon.txt", firm);
  333.  
  334. cout << endl;
  335. cout << "Чтобы показать Меню нажмите m." << endl;
  336. cout << "Чтобы показать измененный файл нажмите r." << endl;
  337. cin >> choise;
  338.  
  339. if (choise == 'm') menu(autos);
  340.  
  341. if (choise == 'r')
  342. {
  343. read_file(autos, "autosalon.txt", 1);
  344.  
  345. cout << "Показать меню? y/n " << endl;
  346.  
  347. cin >> choise;
  348.  
  349. if (choise == 'y')
  350. {
  351. cout<<endl<<endl;
  352. menu(autos);
  353. }
  354. else
  355. {
  356. cout << "End programm";
  357. }
  358. }
  359. }
  360.  
  361.  
  362.  
  363.  
  364. }
  365.  
  366. if (count == 2)
  367. {
  368. read_file(autos, "autosalon.txt", 1);
  369. cout << "Показать меню? y/n " << endl;
  370.  
  371. cin >> choise;
  372.  
  373. if (choise == 'y')
  374. {
  375. cout<<endl<<endl;
  376. menu(autos);
  377. }
  378. else
  379. {
  380. cout << "End programm";
  381. }
  382.  
  383. }
  384.  
  385. if (count == 3)
  386. {
  387. int price;
  388.  
  389. cout << "Введите цену для сортировки" << endl;
  390. cin >> price;
  391. filter_by_price(autos, "autosalon.txt", price);
  392. cout << "Показать меню? y/n " << endl;
  393.  
  394. cin >> choise;
  395.  
  396. if (choise == 'y')
  397. {
  398. cout<<endl<<endl;
  399. menu(autos);
  400. }
  401. else
  402. {
  403. cout << "End programm";
  404. }
  405. }
  406.  
  407. if ((count == 4) or (count == 5))
  408. {
  409. new_file(autos, "autosalon.txt", "autosalon2.txt");
  410.  
  411.  
  412. cout << "Показать меню? y/n " << endl;
  413. cin >> choise;
  414.  
  415. if (choise == 'y')
  416. {
  417. cout<<endl<<endl;
  418. menu(autos);
  419. }
  420. else
  421. {
  422. cout << "End programm";
  423. }
  424. }
  425.  
  426.  
  427.  
  428.  
  429. }
  430.  
  431. int main()
  432. {
  433. setlocale(LC_ALL, "rus");
  434.  
  435. autosalon *autos = new autosalon[5000];
  436.  
  437. menu(autos);
  438.  
  439. return 0;
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement