Advertisement
SkeptaProgrammer

Untitled

May 31st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. // syrykh_5.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <fstream>
  9. using namespace std;
  10. /*
  11. написать условие задачи
  12. разбить на модули
  13. переписать удаление
  14. найти самый популярный цвет для каждой марки
  15. */
  16. const int SIZE = 250;
  17.  
  18. struct CarInfo
  19. {
  20.     char brand[SIZE];
  21.     char color[SIZE];
  22.     char fullName[SIZE];
  23.     char licensePlate[SIZE];
  24. };
  25. int Input(int lowBorder, int highBorder)
  26. {
  27.     int input;
  28.  
  29.         while (!(cin >> input && input >= lowBorder && input < highBorder))
  30.         {
  31.             cout << "\nВвод некорректен. Повторите ввод: ";
  32.             cin.clear();
  33.             while (cin.get() != '\n');
  34.         }
  35.         while (cin.get() != '\n');
  36.     return input;
  37. }
  38.  
  39. int numberOfRecords(char* direction)
  40. {
  41.     CarInfo item;
  42.     ifstream file;
  43.     file.open(direction);
  44.     //cout << file.is_open() << " " << sizeof(item);
  45.     file.seekg(0, ios::end);
  46.     int result = file.tellg() / sizeof(item);
  47.     cout << result;
  48.     file.close();
  49.     return result;
  50. }
  51.  
  52. void clearStream()
  53. {
  54.     cin.clear();
  55.     while (cin.get() != '\n');
  56. }
  57.  
  58. void showRecords(char* direction)
  59. {
  60.     int countOfRecords = numberOfRecords(direction);
  61.     vector<CarInfo> arrayOfRecords(countOfRecords);
  62.     fstream file(direction, ios::binary | ios::in);
  63.     for (int i = 0; i < countOfRecords; i++)
  64.     {
  65.         file.read(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  66.         cout << "\n" << i + 1 << " item\n";
  67.         cout << "brand " << arrayOfRecords[i].brand;
  68.         cout << "\ncolor " << arrayOfRecords[i].color;
  69.         cout << "\nfull name " << arrayOfRecords[i].fullName;
  70.         cout << "\nlicense plate " << arrayOfRecords[i].licensePlate << "\n\n";
  71.     }
  72.     file.close();
  73. }
  74.  
  75. void addRecordToBack(char* direction)
  76. {
  77.     ofstream file;
  78.     CarInfo item;
  79.     int n = 0;
  80.     cout << "Введите кол-во новых записей: "; n=Input(0,10);
  81.     file.open(direction, ios::binary | ios::out  | ios::app);
  82.     for (int i = 0; i < n; i++)
  83.     {
  84.         clearStream();
  85.         cout << i + 1 << " item\n";
  86.         cout << "brand "; cin.getline(item.brand, SIZE, '\n');
  87.         cout << "color "; cin.getline(item.color, SIZE, '\n');
  88.         cout << "full name "; cin.getline(item.fullName, SIZE, '\n');
  89.         cout << "license plate "; cin.getline(item.licensePlate, SIZE, '\n');
  90.         file.write(reinterpret_cast<char*>(&item), sizeof(item));
  91.     }
  92.     file.close();
  93.     showRecords(direction);
  94. }
  95. //
  96.  
  97.  
  98.  
  99. void deleteRecord(char* direction)
  100. {
  101.     showRecords(direction);
  102.     int countOfRecords = numberOfRecords(direction), n,k=0;
  103.     vector<CarInfo> arrayOfRecords(countOfRecords);
  104.     vector <CarInfo> arrayWithoutRecord(countOfRecords - 1);
  105.     cout << "Введите номер записи для удаления: "; n=Input(0,countOfRecords+1);
  106.     fstream file(direction, ios::binary | ios::in);
  107.     for (int i = 0; i < countOfRecords; i++)
  108.         file.read(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  109.     for (int i = 0; i < countOfRecords - 1; i++)
  110.     {
  111.         if (i != n - 1)
  112.         {
  113.             arrayWithoutRecord.push_back(arrayOfRecords[i]);
  114.             //k++;
  115.         }
  116.     }
  117.     file.close();
  118.     file.open(direction, ios::binary | ios::out);
  119.     file.close();
  120.     file.open(direction, ios::binary | ios::out);
  121.     for (int i = 0; i < countOfRecords-1; i++)
  122.         file.write(reinterpret_cast<char*>(&arrayWithoutRecord[i]), sizeof(arrayWithoutRecord[i]));
  123.     file.close();
  124.     showRecords(direction);
  125. }
  126.  
  127. void editRecord(char* direction)
  128. {
  129.     showRecords(direction);
  130.     int n, nField, k = 0; char temp[SIZE];
  131.     int countOfRecords = numberOfRecords(direction);
  132.     vector<CarInfo> arrayOfRecords(countOfRecords);
  133.     fstream file(direction, ios::binary | ios::in | ios::out);
  134.     for (int i = 0; i < countOfRecords; i++)
  135.         file.read(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  136.     cout << "Кол-во записей: " << countOfRecords << "\n";
  137.     cout << "Введите номер записи для редактирования: "; n=Input(0,countOfRecords+1);
  138.     cout << "\n 1 - brand, 2 - color, 3 - full name, 4 - license plate\n";
  139.     cout << "Введите номер поля для редактирования: "; nField = Input(1,5);
  140.  
  141.     switch (nField)
  142.     {
  143.         case 1:
  144.         {
  145.             clearStream();
  146.             cout << "brand "; cin.getline(arrayOfRecords[n - 1].brand, SIZE, '\n');
  147.             break;
  148.         }
  149.         case 2:
  150.         {
  151.             clearStream();
  152.             cout << "color "; cin.getline(arrayOfRecords[n - 1].color, SIZE, '\n');
  153.             break;
  154.         }
  155.         case 3:
  156.         {
  157.             clearStream();
  158.             cout << "full name "; cin.getline(arrayOfRecords[n - 1].fullName, SIZE, '\n');
  159.             break;
  160.         }
  161.         case 4:
  162.         {
  163.             clearStream();
  164.             cout << "license plate "; cin.getline(arrayOfRecords[n - 1].licensePlate, SIZE, '\n');
  165.             break;
  166.         }
  167.         default:
  168.             cout << "Введён неверный номер поля";
  169.             break;
  170.     }
  171.     file.seekg(0);
  172.     for (int i = 0; i < countOfRecords; i++)
  173.         file.write(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  174.     file.close();
  175.     showRecords(direction);
  176. }  
  177.  
  178. int main()
  179. {
  180.     setlocale(0, ""); char repeat;
  181.     char *direction = new char[SIZE];
  182.     int nOperation = 0;
  183.     cout << "Укажите абсолютный путь к файлу: ";
  184.     cin.get(direction, SIZE);
  185.     do
  186.     {
  187.         fstream file(direction, ios::binary | ios::in);
  188.         if (file.is_open())
  189.         {
  190.             file.close();
  191.             cout << "1 - показать все записи \n2 - добавить записи\n3 - редактировать запись \n4 - удалить запись\n";
  192.             nOperation = Input(1, 5);
  193.             switch (nOperation)
  194.             {
  195.             case 1:
  196.             {
  197.                 showRecords(direction);
  198.                 break;
  199.             }
  200.             case 2:
  201.             {
  202.                 addRecordToBack(direction);
  203.                 break;
  204.             }
  205.             case 3:
  206.             {
  207.                 editRecord(direction);
  208.                 break;
  209.             }
  210.             case 4:
  211.             {
  212.                 deleteRecord(direction);
  213.                 break;
  214.             }
  215.             }
  216.         }
  217.         else
  218.         {
  219.             cout << "Указан неверный путь к файлу.\n";
  220.             clearStream();
  221.         }
  222.         cout << "Для завершения введите n\n";
  223.  
  224.     } while (cin >> repeat&& repeat != 'n');
  225.  
  226.     delete[] direction;
  227.     return 0;
  228.  
  229.     //C:\onlyformydoggers\file with info.dat
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement