Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <Windows.h>
- #include <stdio.h>
- #include <cstring>
- using namespace std;
- const unsigned int NAME_SIZE = 30;
- const unsigned int CITY_SIZE = 20;
- struct Address
- {
- char name[NAME_SIZE];
- char city[CITY_SIZE];
- Address* next;
- Address* prev;
- };
- //-----------------------------------------------------------
- int menu(void)
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- char s[80]; int c;
- cout << endl;
- cout << "1. Ввод имени в список" << endl;
- cout << "2. Удаление имени списка" << endl;
- cout << "3. Вывод на экран списка" << endl;
- cout << "4. Поиск в списке" << endl;
- cout << "5. Чтение списка из файла" << endl;
- cout << "6. Запись списка в файл" << endl;
- cout << "7. " << endl;
- cout << "8. Выход" << endl;
- cout << endl;
- do
- {
- cout << "Ваш выбор: ";
- cin.sync();
- gets_s(s);
- cout << endl;
- c = atoi(s);
- } while (c < 0 || c > 8);
- return c;
- }
- //-----------------------------------------------------------
- void insert(Address* e, Address** phead, Address** plast) //Добавление в конец списка
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- Address* p = *plast;
- if (*plast == NULL)
- {
- e->next = NULL;
- e->prev = NULL;
- *plast = e;
- *phead = e;
- return;
- }
- else
- {
- p->next = e;
- e->next = NULL;
- e->prev = p;
- *plast = e;
- }
- }
- //-----------------------------------------------------------
- Address* setElement(bool bList) // Создание элемента и ввод его значений с клавиатуры
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- Address* temp1 = new Address();
- Address* temp2 = new Address();
- if (bList) {
- if (!temp1)
- {
- cerr << "Ошибка выделения памяти памяти";
- return NULL;
- }
- cout << "Введите имя: ";
- cin.getline(temp1->name, NAME_SIZE - 1, '\n');
- cin.ignore(cin.rdbuf()->in_avail());
- cin.clear();
- cout << "Введите город: ";
- cin.getline(temp1->city, CITY_SIZE - 1, '\n');
- cin.ignore(cin.rdbuf()->in_avail());
- cin.clear();
- temp1->next = NULL;
- temp1->prev = NULL;
- return temp1;
- }
- else {
- if (!temp2)
- {
- cerr << "Ошибка выделения памяти памяти";
- return NULL;
- }
- cout << "Введите имя: ";
- cin.getline(temp2->name, NAME_SIZE - 1, '\n');
- cin.ignore(cin.rdbuf()->in_avail());
- cin.clear();
- cout << "Введите город: ";
- cin.getline(temp2->city, CITY_SIZE - 1, '\n');
- cin.ignore(cin.rdbuf()->in_avail());
- cin.clear();
- temp2->next = NULL;
- temp2->prev = NULL;
- return temp2;
- }
- }
- //-----------------------------------------------------------
- void outputList(Address** phead, Address** plast) //Вывод списка на экран
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- Address* t = *phead;
- while (t)
- {
- cout << t->name << ' ' << t->city << endl;
- t = t->next;
- }
- cout << "" << endl;
- }
- //-----------------------------------------------------------
- void find(char name[NAME_SIZE], Address** phead) // Поиск имени в списке
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- Address* t = *phead;
- while (t)
- {
- if (!strcmp(name, t->name)) break;
- t = t->next;
- }
- if (!t)
- cerr << "Имя не найдено" << endl;
- else
- cout << t->name << ' ' << t->city << endl;
- }
- //-----------------------------------------------------------
- void delet(char name[NAME_SIZE], Address** phead, Address** plast) // Удаление имени
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- struct Address* t = *phead;
- while (t)
- {
- if (!strcmp(name, t->name)) break;
- t = t->next;
- }
- if (!t)
- cerr << "Имя не найдено" << endl;
- else
- {
- if (*phead == t)
- {
- *phead = t->next;
- if (*phead)
- (*phead)->prev = NULL;
- else
- *plast = NULL;
- }
- else
- {
- t->prev->next = t->next;
- if (t != *plast)
- t->next->prev = t->prev;
- else
- *plast = t->prev;
- }
- delete t;
- cout << "Элемент удален" << endl;
- }
- }
- void writeToFile(Address** phead1, Address** phead2, Address** phead3, int bList) //Запись в файл
- {
- FILE* fp;
- char filename[12];
- switch (bList) {
- case 0: {
- struct Address* t = *phead1;
- strcpy_s(filename, "mList1.bin");
- errno_t err = fopen_s(&fp, filename, "wb");
- if (err)
- {
- cerr << "Файл не открывается" << endl;
- exit(1);
- }
- cout << "Сохранение в файл" << endl;
- while (t)
- {
- fwrite(t, sizeof(struct Address), 1, fp);
- t = t->next;
- }
- fclose(fp);
- break;
- }
- case 1: {
- struct Address* t = *phead2;
- strcpy_s(filename, "mList2.bin");
- errno_t err = fopen_s(&fp, filename, "wb");
- if (err)
- {
- cerr << "Файл не открывается" << endl;
- exit(1);
- }
- cout << "Сохранение в файл" << endl;
- while (t)
- {
- fwrite(t, sizeof(struct Address), 1, fp);
- t = t->next;
- }
- fclose(fp);
- break;
- }
- case 2: {
- struct Address* t = *phead3;
- strcpy_s(filename, "mList3.bin");
- errno_t err = fopen_s(&fp, filename, "wb");
- if (err)
- {
- cerr << "Файл не открывается" << endl;
- exit(1);
- }
- cout << "Сохранение в файл" << endl;
- while (t)
- {
- fwrite(t, sizeof(struct Address), 1, fp);
- t = t->next;
- }
- fclose(fp);
- break;
- }
- }
- }
- //-----------------------------------------------------------
- void readFromFile(Address** phead, Address** plast, bool bList) //Считывание из файла
- {
- struct Address* t;
- FILE* fp;
- char filename[15];
- if (bList) {
- strcpy_s(filename, "mList2.bin");
- }
- else strcpy_s(filename, "mList1.bin");
- errno_t err = fopen_s(&fp, filename, "rb");
- if (err)
- {
- cerr << "Файл не открывается" << endl;
- exit(1);
- }
- while (*phead)
- {
- *plast = (*phead)->next;
- delete* phead;
- *phead = *plast;
- }
- *phead = *plast = NULL;
- cout << "Загрузка из файла" << endl;
- while (!feof(fp))
- {
- t = new Address();
- if (!t)
- {
- cerr << "Ошибка выделения памяти" << endl;
- return;
- }
- if (1 != fread(t, sizeof(struct Address), 1, fp)) break;
- insert(t, phead, plast);
- }
- fclose(fp);
- }
- void oneByOne(Address** phead1, Address** phead2, Address** phead3) {
- char cName[30], cCity[20];
- struct Address* t = *phead1;
- struct Address* l = *phead2;
- //struct Address* m = *phead3;
- Address* m = new Address();
- while (t || l)
- {
- //strcpy_s(cName, t->name);
- for (int i = 0; t->name[i] !=0; i++) {
- m->name[i] = t->name[i];
- }
- for (int i = 0; t->city[i] != 0; i++) {
- m->city[i] = t->city[i];
- }
- t = t->next;
- m = m->next;
- Address* m = new Address();
- for (int i = 0; l->name[i] != 0; i++) {
- m->name[i] = l->name[i];
- }
- for (int i = 0; l->city[i] != 0; i++) {
- m->city[i] = l->city[i];
- }
- m = m->next;
- l = l->next;
- }
- writeToFile(phead1, phead2, phead3, 2);
- }
- //-----------------------------------------------------------
- int main(void)
- {
- char s[80];
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- Address* head1 = NULL;
- Address* last1 = NULL;
- Address* head2 = NULL;
- Address* last2 = NULL;
- Address* head3 = NULL;
- Address* last3 = NULL;
- int bList;
- setlocale(LC_CTYPE, "Rus");
- while (true)
- {
- cout << "С каким списком будем работать?\n";
- cin.sync();
- gets_s(s);
- cout << endl;
- bList = atoi(s);
- switch (menu())
- {
- case 1: if (bList - 1) insert(setElement(bList - 1), &head2, &last2);
- else
- insert(setElement(bList - 1), &head1, &last1);
- break;
- case 2: { char dname[NAME_SIZE];
- cout << "Введите имя: ";
- cin.getline(dname, NAME_SIZE - 1, '\n');
- cin.ignore(cin.rdbuf()->in_avail());
- cin.sync();
- if (bList - 1)
- delet(dname, &head2, &last2);
- else delet(dname, &head1, &last1);
- break;
- }
- break;
- case 3:
- if (bList - 1)
- outputList(&head2, &last2);
- else outputList(&head1, &last1);
- break;
- case 4: { char fname[NAME_SIZE];
- cout << "Введите имя: ";
- cin.getline(fname, NAME_SIZE - 1, '\n');
- cin.ignore(cin.rdbuf()->in_avail());
- cin.sync();
- if (bList - 1)
- find(fname, &head2);
- else find(fname, &head1);
- }
- break;
- case 5: {
- if (bList - 1)
- readFromFile(&head2, &last2, bList - 1);
- else readFromFile(&head1, &last1, bList - 1);
- break;
- }
- case 6:
- {
- writeToFile(&head1, &head2, &head3, bList-1);
- break;
- }
- case 7: oneByOne(&head1, &head2, &head3); break;
- case 8: exit(0);
- default: exit(1);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement