Advertisement
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(beg);
- break;
- case 7: return 0;
- default:
- cout << "Âàì ñëåäóåò ââåñòè íîìåð îò 1 äî 5" << endl;
- cin.get();
- break;
- }
- }
- return 0;
- }
- //-----------------------------------------------------------------------------
- void searchname(book * beg)
- {
- book *temp = beg;
- char fname[d_n];
- system ("cls");
- if (!beg) {
- MessageBox(0, "Ñïèñîê ïóñò", "Óâåäîìëåíèå", MB_ICONINFORMATION | MB_SETFOREGROUND);
- return;
- }
- cout << "Ââåäèòå íàçâàíèå êíèãè äëÿ ïîèñêà" << endl;
- cin >> fname;
- while (temp) {
- if (strcmp(fname, temp->name) == 0) {
- 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
Advertisement