Advertisement
J3st3rs_j0k3

struct_pr1_1

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