Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <stdlib.h>
- #include <string.h>
- #include <iomanip>
- #include <conio.h>
- #include <iostream>
- #include <locale.h>
- #include <windows.h>
- using namespace std;
- const int d_n = 20;
- //-------------------------------------------
- struct book{
- char autor[d_n];
- char name[d_n];
- char izdatelstvo[d_n];
- char janr[d_n];
- int d,m,g,cost;
- book *next;
- book *pred;
- };
- //-------------------------------------------
- book* dob(book* end, const book& s);
- book* dob_first(const book& s);
- book* udal(book* beg);
- book vvod_book();
- book*variant(book* beg,const book& s);
- void print(const book& s);
- void prosmotr(book* beg);
- void sort (const book*s);
- void searchname(book * beg);
- int read_file(char* filename, book** beg, book** end);
- int write_file(char* filename, book* temp);
- int menu();
- //--------основная программа-----------------------------------
- int main(){
- ShowWindow(GetConsoleWindow(), SW_MAXIMIZE); // полноэкранный режим
- setlocale(LC_ALL, "Rus");
- book *beg = NULL,
- *end = NULL,input;
- char filename[20];
- int n=0;
- while (1){
- switch (menu())
- {
- case 1:
- if (beg)
- end = dob(end,vvod_book());
- else
- {
- beg = dob_first(vvod_book());
- end = beg;
- }
- n++;
- break;
- case 2:
- beg = udal(beg);
- cout << "Нажмите любую клавишу" << endl;
- n=0;
- cin.get();
- break;
- case 3:
- prosmotr(beg);
- cout<<"!!!"<<n;
- cin.get();
- break;
- case 4:
- write_file(filename,beg);
- break;
- case 5:
- read_file(filename, &beg, &end);
- break;
- case 6:
- // beg = variant();
- searchname(book * beg);
- cin.get();
- break;
- case 7: return 0;
- default:
- cout << "Вам следует ввести номер от 1 до 5" << endl;
- cin.get();
- break;
- }
- }
- return 0;
- }
- //-----------------------------------------------------------------------------
- void searchname(book * beg)
- {
- book * temp = beg;
- char fname;
- system ("cls");
- if (!beg) {
- MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
- return;
- }
- cout << "Введите название книги для поиска" << endl;
- cin >> fname;
- while (temp) {
- if (fname == temp->s.book) {
- cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
- cout<<"| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |"<<endl;
- cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
- print(*temp);
- system ("pause");
- return;
- }
- temp = temp->next;
- }
- cout << "Книги с таким названием не найдено" << endl;
- system ("pause");
- }
- //----------------------------------------------------------------
- sort(const book& s,book* beg){
- /* if (!beg){
- book*temp=new book;
- if (!temp)
- {
- cout <<"Не хватает памяти!"<<endl;
- return 0;
- }
- *temp=s;
- temp->next=NULL;
- beg=temp;
- return beg;
- }*/
- // book*T=new book;
- /* book *temp=new book;
- *temp=*beg;
- book *temp2=new book;
- *temp2=*beg->next;
- for (temp;temp->next!=NULL;temp->next)
- {
- for (temp2;temp2!=NULL;temp2->next)
- {
- if (s[temp].g != s[temp2].g)
- {
- if(s[temp].g > s[temp2].g)
- {
- T=s[temp];
- s[temp]=s[temp2];
- s[temp2]=T;
- continue;
- }
- }
- else if (s[temp].m != s[temp2].m)
- {
- if (s[temp].m > s[temp2].m)
- {
- T=s[temp];
- s[temp]=s[temp2];
- s[temp2]=T;
- continue;
- }
- }
- else if (s[temp].d > s[temp2].d)
- {
- T=s[temp];
- s[temp]=s[temp2];
- s[temp2]=temp;
- }
- }
- }*/
- }
- //-----------------------------------------------------------------------------
- void prosmotr(book *beg)
- {
- if (!beg)
- {
- cout << "список пустой" << endl;
- cin.get();
- return ;
- }
- book *temp = beg;
- cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
- cout<<"| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |"<<endl;
- cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
- while (temp)
- {
- print(*temp);
- temp = temp->next;
- }
- cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
- cin.get();
- }
- //-----------------------------------------------------------------------------
- void print(const book& s)
- {
- cout << "|" << s.autor << setw( 20 - strlen(s.autor) ) << "|";
- cout << s.name << setw( 21 - strlen(s.name) ) << "|";
- cout << s.izdatelstvo << setw( 19 - strlen(s.izdatelstvo) ) << "|";
- cout << s.janr << setw( 17 - strlen(s.izdatelstvo) ) << "|";
- if (s.d<10) cout << 0 << s.d << ".";
- if (s.m<10) cout << 0 << s.m << "." << s.g << setw( 19 ) << "|";
- cout << s.cost << setw( 16 ) << "|" << endl;
- }
- //--------------ф-я добавления или создания эл-та-------------------------------
- book* dob(book *end,const book& s)
- {
- book *newE = new book;
- *newE = s;
- newE->next = 0;
- end->next = newE;
- end = newE;
- return end;
- }
- //--------------создание первого эл-та------------------------------
- book* dob_first(const book& s)
- {
- book *beg = new book;
- *beg = s;
- beg->next = 0;
- return beg;
- }
- //-----------------------------------------------------------------------------
- book vvod_book()
- {
- char buf[10];
- book s;
- cout << "Введите автора:" << endl;
- while (1){
- cin>>s.autor;
- if (atoi(s.autor)==0)
- break;
- cout<<"Ошибка ввода!"<<endl;
- }
- cout << "Введите название книги:" << endl;
- while (1){
- cin>>s.name;
- if (atoi(s.name)==0)
- break;
- cout<<"Ошибка ввода!"<<endl;
- }
- cout << "Введите издательство:" << endl;
- while (1){
- cin>>s.izdatelstvo;
- if (atoi(s.izdatelstvo)==0)
- break;
- cout<<"Ошибка ввода!"<<endl;
- }
- cout << "Введите жанр:" << endl;
- while (1){
- cin>>s.janr;
- if (atoi(s.janr)==0)
- break;
- cout<<"Ошибка ввода!"<<endl;
- }
- cout << "Введите дату поступления:" << endl;
- cout <<"Введите день:";
- while(1){
- cin>>s.d;
- if ((s.d>=1)&&(s.d<=31))
- break;
- cout<<"Ошибка ввода!Ведите правильную дату!"<<endl;
- }
- cout <<"Введите месяц:";
- while(1){
- cin>>s.m;
- if ((s.m>=1)&&(s.m<=12))
- break;
- cout<<"Ошибка ввода!Ведите правильную дату!"<<endl;
- }
- cout <<"Введите год:";
- while(1){
- cin>>s.g;
- if ((s.g>=1900)&&(s.g<=2050))
- break;
- cout<<"Ошибка ввода!Ведите правильную дату!"<<endl;
- }
- cout << "Введите стоимость книги:" << endl;
- cin>>s.cost;
- return s;
- }
- //-----------------------------------------------------------------------------
- /*book* variant(book* beg,const book& s)
- {
- if (!beg)
- {
- book* temp=new book;
- if(!temp)
- {
- cout<<"Не хватает памяти"<<endl;
- return NULL;
- }
- *temp = s;
- temp->next=NULL;
- beg=temp;
- return beg;
- }
- book*new_book=new book;
- if (!new_book)
- {
- cout<<"Не хватает памяти"<<endl;
- return beg;
- }
- book* temp=beg;
- *new_book=s;
- new_book->next=NULL;
- if (strcmp(new_book->name,temp->name)<=0)
- {
- new_book->next=temp;
- return new_book;
- }
- else
- {
- if(!temp->next&&strcmp(new_book->name,temp->name)>=0)
- {
- temp->next=new_book;
- return beg;
- }
- }
- while (temp->next->next && strcmp(new_book->name,temp->next->name)>=0)
- {
- temp = temp->next;
- }
- if(!temp->next->next)
- {
- if(strcmp(new_book->name,temp->next->name)>=0)
- {
- temp->next->next=new_book;
- return beg;
- }
- else
- {
- new_book->next=temp->next;
- temp->next=new_book;
- return beg;
- }
- }
- else
- {
- new_book->next=temp->next;
- temp->next=new_book;
- return beg;
- }
- }*/
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- int read_file(char* filename,book** beg, book** end)
- {
- cout<<"Введите название файла"<<endl;
- cin>>filename;
- ifstream fin(filename,ios::in);
- if (!fin)
- {
- cout<<"Нет файла"<<filename<<endl;
- return 1;
- }
- book s;
- *beg = 0;
- fin.seekg(0, ios::beg);
- while (fin>>s.autor)
- {
- fin>>s.name;
- fin>>s.izdatelstvo;
- fin>>s.janr;
- fin>>s.d;
- fin>>s.m;
- fin>>s.g;
- fin>>s.cost;
- if (*beg)
- *end=dob(*end,s);
- else
- {
- *beg=dob_first(s); *end=*beg;
- }
- }
- cout<<"Считывание прошло успешно"<<endl;
- cin.get();
- cin.get();
- return 0;
- }
- //-----------------------------------------------------------------------------
- int write_file(char* filename, book* temp)
- {
- cout<<"Введите название файла"<<endl;
- cin>>filename;
- ofstream fout(filename, ios_base::app); // открытие файла
- if (!fout)
- {
- cout << "Не могу открыть файл для записи" << endl;
- return 1;
- }
- while (temp)// пока temp!=0 выводим эл-ты в файл
- {
- fout << temp->autor << endl;
- fout << temp->name << endl;
- fout << temp->izdatelstvo << endl;
- fout << temp->janr << endl;
- fout << temp->d<< endl;
- fout <<temp->m<< endl;
- fout <<temp->g<< endl;
- fout << temp->cost << endl;
- temp = temp->next;
- }
- cout << "Данные сохранены в файле: " << filename << endl;
- cout << "==============================" << endl;
- cout << "Нажмите любую клавишу" << endl;
- cin.get();
- return 0;
- }
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- int menu()
- {
- char buf[10];
- int item;
- do
- {
- system("CLS");
- cout << "1 - Создать список или добавить новый эл-т" << endl;
- cout << "2 - Удаление всех элементов списка" << endl;
- cout << "3 - Просмотр списка" << endl;
- cout << "4 - Запись данных в файл" << endl;
- cout << "5 - Чтение из файла" << endl;
- cout << "6 - 5 самых старых книг" << endl;
- cout << "7 - Выход"<< endl;
- cout << "============================" << endl;
- cout << "Введите номер пункта меню" << endl;
- cin >> buf;
- cin.get();
- item = atoi(buf);
- if (!item)
- {
- cout << "Вам следует вводить число от 1 до 7" << endl;
- cin.get();
- }
- }
- while (!item);
- return item;
- }
- //-----------------------------------------------------------------------------
- book* udal(book *beg)
- {
- book *temp;
- if (!beg)
- {
- cout << "список пустой" << endl;
- cin.get();
- return 0;
- }
- while (beg)
- {
- temp = beg;
- beg = beg->next;
- delete temp;
- }
- cout << "==============================" << endl;
- cout << "====удаление прошло успешно===" << endl;
- cout << "==============================" << endl;
- return beg;
- }
Advertisement
Add Comment
Please, Sign In to add comment