Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <iomanip>
- #include <locale>
- #include <float.h>
- using namespace std;
- struct person { //структура person
- int number;
- char fio[50];
- int seria;
- int nomer;
- char kateg[10];
- float money;
- int day, month, year;
- };
- class List
- {
- public:
- List(); //constructor
- ~List(); //destructor
- void push_back(person newdata);
- void push_front(person newdata);
- int GetSize(){return Size;};
- void pop_front();
- void pop_back();
- void clear();
- void insert(person newdata, int index);
- void remove(int index);
- void readfile(string filename);
- int write_in_file(string filename);
- void show_list();
- void print_person(person newdata);
- void search_nodes();
- private:
- // template<typename T>
- class Node
- {
- public:
- Node *next;
- person data;
- Node(person newdata = person(), Node *next1 = nullptr){
- this->data = newdata;
- this->next = next1;
- }
- };
- int Size;
- Node *head;
- };
- List::List()
- {
- Size = 0;
- head = nullptr;
- }
- List::~List()
- {
- clear();
- }
- person write_man() //ввод информации о вкладчике
- {
- person man;
- cout<<"Введите номер счета клиента:\n";
- cin>>man.number;
- cout<<"Введите Фамилию клиента:\n";
- cin>>man.fio;
- cout<<"Введите серию паспорта клиента\n";
- cin>>man.seria;
- cout<<"Введите номер паспорта клиента\n";
- cin>>man.nomer;
- cout<<"Введите категорию вклада\n";
- cin>>man.kateg;
- cout<<"Введите сумму вклада\n";
- cin>>man.money;
- cout<<"Введите дату создания вклада\n";
- cin>>man.day>>man.month>>man.year;
- return man;
- }
- void List::print_person(person man)
- {
- cout<<"Счет: "<<man.number<<endl<<"ФИО: "<<man.fio<<endl<<"Паспорт: "<<man.seria<<" "<<man.nomer<<endl<<"Категория вклада: "<<man.kateg<<endl<<"Сумма вклада: "<<man.money<<endl<<"Дата: "<<man.year<<endl;
- }
- void List::search_nodes()
- {
- Node *cur = head;
- person rich;
- float bucks = FLT_MIN;
- std::string cat = "Srochny";
- bool Found = false;
- if (Size == 0)
- {
- cout << "Список пуст" << endl;
- system("pause");
- return;
- }
- while(cur != nullptr)
- {
- // int ravn = strcmp(cur->data.kateg, cat);
- if((cur->data.money > bucks) && (cur->data.kateg == cat))
- {
- bucks = cur->data.money;
- rich = cur->data;
- Found = true;
- }
- cur = cur->next;
- }
- cout << "Сумма наибольшего вклада категории Срочныц у клиента:" << endl;
- if (Found == true)
- {
- cout<<"----------------------------------------------------------------------------------------------------"<<endl;
- print_person(rich);
- cout<<"----------------------------------------------------------------------------------------------------"<<endl;
- }
- if(Found == false) cout<<"Нет таковых!"<<endl;
- system("pause");
- }
- int main(int argc, char* argv[])
- {
- setlocale(0, "");
- string f;
- int menu;
- List clients;
- if (argc > 1) f = argv[1];
- else f = "example.bin";
- cout << "Создать новый файл или открыть существующий?" << endl;
- cout << "1. Создать файл" << endl;
- cout << "2. Открыть файл" << endl;
- cin >> menu;
- if(menu == 1)
- {
- ofstream fout(f, ios::binary);
- fout.close();
- }
- clients.readfile(f);
- do
- {
- // system("CLS");
- cout<<"1. Просмотр списка"<<endl;
- cout<<"2. Добавить элемент в начало списка"<<endl;
- cout<<"3. Удалить элемент из начала списка"<<endl;
- cout<<"4. Добавить элемент в конец списка"<<endl;
- cout<<"5. Удалить элемент в конце списка"<<endl;
- cout<<"6. Поиск"<<endl;
- cout<<"7. Выход"<<endl<<endl;
- cin>>menu;
- cin.ignore(32767, '\n');
- // system("CLS");
- switch(menu)
- {
- case 1: clients.show_list(); break;
- case 2: clients.push_front(write_man()); break;
- case 3: clients.pop_front(); break;
- case 4: clients.push_back(write_man()); break;
- case 5: clients.pop_back(); break;
- case 6: clients.search_nodes(); break;
- default: break;
- }
- }while(menu != 7);
- if(clients.write_in_file(f))
- {
- cout<<"\nФайл сохранен"<<endl;
- }
- else
- {
- cout<<"\nФайл не сохранен"<<endl;
- }
- clients.clear();
- return 0;
- }
- void List::push_front(person newdata)//добавление элемента в начало списка
- {
- head = new Node(newdata, head);
- Size++;
- }
- void List::push_back(person newdata)//добавление элемента в конец списка
- {
- if (head == nullptr){
- head = new Node(newdata);
- }
- else{
- Node *temp = this->head;
- while(temp->next != nullptr){
- temp = temp->next;
- }
- temp->next = new Node(newdata);
- }
- Size++;
- }
- void List::pop_front()//метод удаления первого жлемента в списке
- {
- Node *temp = head;
- head = head->next;
- delete temp;
- Size--;
- }
- void List::pop_back() //удаление последнего узла
- {
- Node *temp, *cur = head;
- if(Size > 1)
- {
- for(int i = 0; i != Size-2; i++)
- {
- cur = cur->next;
- }
- }
- else if(Size == 1)
- {
- delete cur;
- head = nullptr;
- Size--;
- cout << "Узел был удален!" << endl;
- system("pause");
- return;
- }
- else if(Size == 0)
- {
- cout << "Список пуст" << endl;
- system("pause");
- return;
- }
- temp = cur;
- cur = cur->next;
- temp->next = nullptr;
- delete cur;
- Size--;
- cout << "Узел был удален!" << endl;
- system("pause");
- }
- void List::clear()//удаление всего списка
- {
- while(Size){
- pop_front();
- }
- }
- //void List::insert(person newdata, int index)//добавление элемента в список по индексу
- //{
- // if (index == 0){
- // push_front(newdata);
- // }
- // else{
- // Node *prev = this->head;
- // for(int i = 0; i < index - 1; i++){
- // prev = prev->next;
- // }
- // Node *newNode = new Node (newdata, prev->next);
- // prev->next = newNode;
- // Size++;
- // }
- //}
- //
- //void List::remove(int index)//удаление элемента по индексу
- //{
- // if (index == 0){
- // pop_front();
- // }
- // else{
- // Node *prev = this->head;
- // for(int i = 0; i < index-1; i++){
- // prev = prev->next;
- // }
- // Node *temp = prev->next;
- // prev->next = temp->next;
- // delete temp;
- // Size--;
- // }
- //}
- void List::show_list()//вывод содержимого списка
- {
- Node * cur = head;
- int n = 0;
- // system("CLS");
- if(Size == 0){
- cout<<"Список пуст!"<<endl;
- system("pause");
- return;
- }
- cout<<"----------------------------------------------------------------------------------------------------"<<endl;
- cout<<"| N | Nomer cheta | Last Name | Passport | Kategori | Summa | Last operation |"<<endl;
- cout<<"----------------------------------------------------------------------------------------------------"<<endl;
- while(cur != nullptr)
- {
- 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;
- cout<<"----------------------------------------------------------------------------------------------------"<<endl;
- cur=cur->next;
- }
- system("pause");
- }
- void List::readfile(string filename)
- {
- person data;
- ifstream fin(filename, ios::binary);
- if(!fin.is_open())
- {
- cout<<"Ошибка открытия файла"<<endl;
- }
- else
- {
- cout<<"Файл открыт"<<endl;
- while(fin.read((char*)&data, sizeof(person)))
- {
- this->push_back(data);
- }
- }
- fin.close();
- system("pause");
- }
- int List::write_in_file(string filename)
- {
- Node *cur = head;
- ofstream fout(filename, ios::binary);
- if(!fout.is_open())
- {
- cout<<"Ошибка открытия файла"<<endl;
- }
- else
- {
- while(cur != nullptr)
- {
- fout.write((char*)&(cur->data), sizeof(person));
- cur=cur->next;
- }
- }
- fout.close();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement