Advertisement
J3st3rs_j0k3

pr1_1(5)

Apr 4th, 2022
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <locale>
  6. #include <float.h>
  7. //#include <windows.h>
  8.  
  9. using namespace std;
  10.  
  11. struct person { //структура person
  12.  
  13.     int number;
  14.     char fio[50];
  15.     int seria;
  16.     int nomer;
  17.     char kateg[10];
  18.     float money;
  19.     int day, month, year;
  20. };
  21.  
  22. class List
  23. {
  24.  
  25.     public:
  26.         List(); //constructor
  27.         ~List(); //destructor
  28.         void push_back(person newdata);
  29.         void push_front(person newdata);
  30.         int GetSize(){return Size;};
  31.         void pop_front();
  32.         void pop_back();
  33.         void clear();
  34.         void insert(person newdata, int index);
  35.         void remove(int index);
  36.         void readfile(string filename);
  37.         int write_in_file(string filename);
  38.         void show_list();
  39.         void print_person(person newdata);
  40.         void search_nodes();
  41.  
  42.  
  43.     private:
  44. //        template<typename T>
  45.         class Node
  46.         {
  47.             public:
  48.                 Node *next;
  49.                 person data;
  50.                 Node(person newdata = person(), Node *next1 = nullptr){
  51.                         this->data = newdata;
  52.                         this->next = next1;
  53.                 }
  54.         };
  55.         int Size;
  56.         Node *head;
  57. };
  58.  
  59. List::List()
  60. {
  61.     Size = 0;
  62.     head = nullptr;
  63. }
  64.  
  65. List::~List()
  66. {
  67.     clear();
  68. }
  69.  
  70. person write_man() //ввод информации о вкладчике
  71. {
  72.     person man;
  73.     cout<<"Введите номер счета клиента:\n";
  74.     cin>>man.number;
  75.     cout<<"Введите Фамилию клиента:\n";
  76.     cin>>man.fio;
  77.     cout<<"Введите серию паспорта клиента\n";
  78.     cin>>man.seria;
  79.     cout<<"Введите номер паспорта клиента\n";
  80.     cin>>man.nomer;
  81.     cout<<"Введите категорию вклада\n";
  82.     cin>>man.kateg;
  83.     cout<<"Введите сумму вклада\n";
  84.     cin>>man.money;
  85.     cout<<"Введите дату создания вклада\n";
  86.     cin>>man.day>>man.month>>man.year;
  87.     return man;
  88. }
  89.  
  90. void List::print_person(person man)
  91. {
  92.     cout<<"Счет: "<<man.number<<endl<<"ФИО: "<<man.fio<<endl<<"Паспорт: "<<man.seria<<" "<<man.nomer<<endl<<"Категория вклада: "<<man.kateg<<endl<<"Сумма вклада: "<<man.money<<endl<<"Дата: "<<setw(2)<<setfill('0')<<man.day<<'/'<<setw(2)<<setfill('0')<<man.month<<'/'<<setw(4)<<man.year<<endl;
  93. }
  94.  
  95. void List::search_nodes()
  96. {
  97.     Node *cur = head;
  98.     person rich;
  99.     float bucks = FLT_MIN;
  100.     std::string cat = "Srochny";
  101.     bool Found = false;
  102.     system("cls");
  103.     if (Size == 0)
  104.     {
  105.         cout << "Список пуст" << endl;
  106.         system("pause");
  107.         return;
  108.     }
  109.     while(cur != nullptr)
  110.     {
  111. //      int ravn = strcmp(cur->data.kateg, cat);
  112.         if((cur->data.money > bucks) && (cur->data.kateg == cat))
  113.         {
  114.               bucks = cur->data.money;
  115.               rich = cur->data;
  116.               Found = true;
  117.         }
  118.         cur = cur->next;
  119.     }
  120.   cout << "Сумма наибольшего вклада категории Срочныц у клиента:" << endl;
  121.   if (Found == true)
  122.   {
  123.       cout<<"----------------------------------------------------------------------------------------------------"<<endl;
  124.       print_person(rich);
  125.       cout<<"----------------------------------------------------------------------------------------------------"<<endl;
  126.   }
  127.   if(Found == false) cout<<"Нет таковых!"<<endl;
  128.   system("pause");
  129. }
  130.  
  131. int main(int argc, char* argv[])
  132. {
  133.     setlocale(LC_ALL, "");
  134.     string f;
  135.     int menu;
  136.     List clients;
  137.     if (argc > 1) f = argv[1];
  138.     else f = "example.bin";
  139.     cout << "Создать новый файл или открыть существующий?" << endl;
  140.     cout << "1. Создать файл" << endl;
  141.     cout << "2. Открыть файл" << endl;
  142.     cin >> menu;
  143.     if(menu == 1)
  144.     {
  145.         ofstream fout(f, ios::binary);
  146.         fout.close();
  147.     }
  148.     clients.readfile(f);
  149.     do
  150.     {
  151.         system("cls"); //чистим консоль
  152.         cout<<"1. Просмотр списка"<<endl;
  153.         cout<<"2. Добавить элемент в начало списка"<<endl;
  154.         cout<<"3. Удалить элемент из начала списка"<<endl;
  155.         cout<<"4. Добавить элемент в конец списка"<<endl;
  156.         cout<<"5. Удалить элемент в конце списка"<<endl;
  157.         cout<<"6. Поиск"<<endl;
  158.         cout<<"7. Выход"<<endl<<endl;
  159.  
  160.         cin>>menu;
  161.         cin.ignore(32767, '\n');
  162.         system("cls");
  163.         switch(menu)
  164.         {
  165.             case 1: clients.show_list(); break;
  166.             case 2: clients.push_front(write_man()); break;
  167.             case 3: clients.pop_front(); break;
  168.             case 4: clients.push_back(write_man()); break;
  169.             case 5: clients.pop_back(); break;
  170.             case 6: clients.search_nodes(); break;
  171.             default: break;
  172.         }
  173.     }while(menu != 7);
  174.  
  175.     if(clients.write_in_file(f))
  176.     {
  177.         cout<<"\nФайл сохранен"<<endl;
  178.     }
  179.     else
  180.     {
  181.         cout<<"\nФайл не сохранен"<<endl;
  182.     }
  183.     clients.clear();
  184.     return 0;
  185. }
  186.  
  187.  
  188.  
  189. void List::push_front(person newdata)//добавление элемента в начало списка
  190. {
  191.     head = new Node(newdata, head);
  192.     Size++;
  193. }
  194.  
  195. void List::push_back(person newdata)//добавление элемента в конец списка
  196. {
  197.     if (head == nullptr){
  198.         head = new Node(newdata);
  199.     }
  200.     else{
  201.         Node *temp = this->head;
  202.         while(temp->next != nullptr){
  203.             temp = temp->next;
  204.         }
  205.         temp->next = new Node(newdata);
  206.     }
  207.     Size++;
  208. }
  209.  
  210. void List::pop_front()//метод удаления первого жлемента в списке
  211. {
  212.     Node *temp = head;
  213.     head = head->next;
  214.     delete temp;
  215.     Size--;
  216.     cout<<"Узел был удален"<<endl;
  217. }
  218.  
  219. void List::pop_back() //удаление последнего узла
  220. {
  221.     Node *temp, *cur = head;
  222.     if(Size > 1)
  223.     {
  224.         for(int i = 0; i != Size-2; i++)
  225.         {
  226.             cur = cur->next;
  227.         }
  228.     }
  229.     else if(Size == 1)
  230.     {
  231.         delete cur;
  232.         head = nullptr;
  233.         Size--;
  234.         cout << "Узел был удален" << endl;
  235.         system("pause");
  236.         return;
  237.     }
  238.     else if(Size == 0)
  239.     {
  240.         cout << "Список пуст" << endl;
  241.         system("pause");
  242.         return;
  243.     }
  244.     temp = cur;
  245.     cur = cur->next;
  246.     temp->next = nullptr;
  247.     delete cur;
  248.     Size--;
  249.     cout << "Узел был удален!" << endl;
  250.     system("pause");
  251. }
  252.  
  253. void List::clear()//удаление всего списка
  254. {
  255.     while(Size){
  256.         pop_front();
  257.     }
  258. }
  259.  
  260. //void List::insert(person newdata, int index)//добавление элемента в список по индексу
  261. //{
  262. //     if (index == 0){
  263. //        push_front(newdata);
  264. //     }
  265. //     else{
  266. //        Node *prev = this->head;
  267. //        for(int i = 0; i < index - 1; i++){
  268. //            prev = prev->next;
  269. //        }
  270. //        Node *newNode = new Node (newdata, prev->next);
  271. //        prev->next = newNode;
  272. //        Size++;
  273. //     }
  274. //}
  275. //
  276. //void List::remove(int index)//удаление элемента по индексу
  277. //{
  278. //    if (index == 0){
  279. //        pop_front();
  280. //    }
  281. //    else{
  282. //        Node *prev = this->head;
  283. //        for(int i = 0; i < index-1; i++){
  284. //            prev = prev->next;
  285. //        }
  286. //        Node *temp = prev->next;
  287. //        prev->next = temp->next;
  288. //        delete temp;
  289. //        Size--;
  290. //    }
  291. //}
  292.  
  293. void List::show_list()//вывод содержимого списка
  294. {
  295.     Node * cur = head;
  296.     int n = 0;
  297.     system("cls");
  298.     if(Size == 0){
  299.         cout<<"Список пуст!"<<endl;
  300.         system("pause");
  301.         return;
  302.     }
  303.     cout<<"----------------------------------------------------------------------------------------------------"<<endl;
  304.     cout<<"| N | Nomer cheta |        Last Name       |  Passport  |  Kategori  |    Summa   | Last operation |"<<endl;
  305.     cout<<"----------------------------------------------------------------------------------------------------"<<endl;
  306.     while(cur != nullptr)
  307.     {
  308.         cout<<"|"<<setw(3)<<++n<<"|";
  309.         cout<<setw(13)<<cur->data.number<<"|";
  310.         cout<<setw(24)<<cur->data.fio<<"|";
  311.         cout<<setw(5)<<cur->data.seria<<""<<setw(7)<<cur->data.nomer<<"|";
  312.         cout<<setw(12)<<cur->data.kateg<<"|";
  313.         cout<<setw(12)<<cur->data.money<<"|";
  314.         cout<<"      "<<setw(2)<<setfill('0')<<cur->data.day<<'/'<<setw(2)<<cur->data.month<<'/'<<setw(4)<<cur->data.year<<"|"<<setfill(' ')<<endl;
  315.         cout<<"----------------------------------------------------------------------------------------------------"<<endl;
  316.         cur = cur->next;
  317.     }
  318.     system("pause");
  319. }
  320.  
  321. void List::readfile(string filename)
  322. {
  323.     person data;
  324.     ifstream fin(filename, ios::binary);
  325.     if(!fin.is_open())
  326.     {
  327.         cout<<"Ошибка открытия файла"<<endl;
  328.     }
  329.     else
  330.     {
  331.         cout<<"Файл открыт"<<endl;
  332.         while(fin.read((char*)&data, sizeof(person)))
  333.         {
  334.             this->push_back(data);
  335.         }
  336.     }
  337.     fin.close();
  338.     system("pause");
  339. }
  340.  
  341. int List::write_in_file(string filename)
  342. {
  343.     Node *cur = head;
  344.     ofstream fout(filename, ios::binary);
  345.     if(!fout.is_open())
  346.     {
  347.         cout<<"Ошибка открытия файла"<<endl;
  348.     }
  349.     else
  350.     {
  351.         while(cur != nullptr)
  352.         {
  353.             fout.write((char*)&(cur->data), sizeof(person));
  354.             cur=cur->next;
  355.         }
  356.     }
  357.     fout.close();
  358.         return 1;
  359. }
  360.  
  361. //<<setw(16)<<cur->data.year
  362. //setfill(' ')
  363.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement