Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #define null NULL
- #define file FILE
- #define usersPath "users.dat"
- #define booksPath "books.dat"
- using namespace std;
- /*
- * Author: MyZone
- *
- */
- struct user{
- char* login;
- char* pass;
- bool admin;
- user* next;
- user* prev;
- /*~user(){
- //тут ошибка
- //if(this->login) delete[] this->login;
- //if(this->pass) delete[] this->pass;
- if(this->next) delete this->next;
- }*/
- };
- struct book{
- int id;
- char* name;
- char** authors;
- int authorsNumber;
- int birthyear;
- char* publishing;
- float price;
- book* next;
- book* prev;
- /*~book(){
- if(this->name) delete[] this->name;
- if(this->authors){
- for(int i=0;i<authorsNumber;i++){
- delete[] this->authors[i];
- }
- delete[] this->authors;
- }
- if(this->publishing) delete[] this->publishing;
- if(this->next) delete this->next;
- }*/
- };
- struct author{
- char* name;
- author* next;
- author* prev;
- /*~author(){
- //delete[] this->name;
- if(this->next) delete this->next;
- }*/
- };
- struct publishing{
- char* name;
- author* authors;
- int authorsNumber;
- book* books;
- int booksNumber;
- publishing* next;
- publishing* prev;
- /*~publishing(){
- //delete[] this->name;
- if(this->authors){
- while(this->authors->prev) this->authors = this->authors->prev;
- delete this->authors;
- }
- if(this->books){
- while(this->books->prev) this->books = this->books->prev;
- delete this->books;
- }
- if(this->next) delete this->next;
- }*/
- };
- void init();
- void deinit();
- void resetIDs(int firstID);
- void swap(book* a,book* b);
- void save();
- void load();
- char* registrateUser();
- char* login();
- char* addBook();
- char* deleteBook();
- void trace();
- char* sortList();
- void tracePublishings(int type);
- //глобальные переменные
- user* currentUser;
- user* users;
- book* books;
- int bookNumber;
- //
- void main(){
- init();
- char* command = new char[256];
- for(;;){
- cout<<"\\>";
- cin>>command;
- if(!strcmp(command,"help")){
- cout<<"\tlogin \tUse this to login is system.\n";
- cout<<"\tregister \tUse this to register.\n";
- cout<<"\tadd \tUse this to add book.\n";
- cout<<"\tdelete \tUse this to add book.\n";
- cout<<"\ttrace \tUse this to show books list.\n";
- cout<<"\tsort \tUse this to sort books.\n";
- cout<<"\tf1 \tUse this to show .\n";
- cout<<"\tf2 \tUse this to show .\n";
- cout<<"\tf3 \tUse this to show .\n";
- cout<<"\help \tUse this to show help.\n";
- cout<<"\texit \tUse this to exit.\n";
- }else if(!strcmp(command,"login")){
- cout<<login();
- }else if(!strcmp(command,"register")){
- cout<<registrateUser();
- }else if(!strcmp(command,"add")){
- cout<<addBook();
- }else if(!strcmp(command,"delete")){
- cout<<deleteBook();
- }else if(!strcmp(command,"trace")){
- trace();
- }else if(!strcmp(command,"sort")){
- cout<<sortList();
- }else if(!strcmp(command,"exit")){
- deinit();
- }else if(!strcmp(command,"f1")){
- tracePublishings(0);
- }else if(!strcmp(command,"f2")){
- tracePublishings(1);
- }else if(!strcmp(command,"f3")){
- tracePublishings(3);
- }
- }
- }
- void init(){
- currentUser = null;
- users = new user;
- users->login = "";
- users->pass = "";
- users->admin = false;
- users->prev = null;
- users->next = null;
- books = new book;
- books->name = null;
- books->authors = null;
- books->publishing = null;
- books->next = null;
- books->prev = null;
- bookNumber = 0;
- //reading files
- //users
- file* usersFile = fopen(usersPath,"rb");
- if(usersFile==null) return;
- while(!feof(usersFile)){
- user* newUser = new user;
- newUser->login = new char[256];
- newUser->pass = new char[256];
- fread(strrev(newUser->login),1,256,usersFile);
- fread(strrev(newUser->pass),1,256,usersFile);
- fread(&newUser->admin,1,1,usersFile);
- newUser->login = strrev(newUser->login);
- newUser->pass = strrev(newUser->pass);
- newUser->next = users->next;
- newUser->prev = users;
- if(users->next) users->next->prev = newUser;
- users->next = newUser;
- users = users->next;
- }
- fclose(usersFile);
- //books
- file* booksFile = fopen(booksPath,"rb");
- if(booksFile==null) return;
- while(true){
- book* newBook = new book;
- newBook->name = new char[256];
- newBook->publishing = new char[256];
- fread(newBook->name,1,256,booksFile);
- fread(newBook->publishing,1,256,booksFile);
- fread(&newBook->birthyear,1,4,booksFile);
- fread(&newBook->price,1,4,booksFile);
- fread(&newBook->authorsNumber,1,4,booksFile);
- if(feof(booksFile)){
- newBook->authors = null;
- newBook->next = null;
- delete newBook;
- break;
- }
- newBook->authors = new char*[newBook->authorsNumber];
- for(int i=0;i<newBook->authorsNumber;i++){
- newBook->authors[i] = new char[256];
- fread(newBook->authors[i],1,256,booksFile);
- }
- newBook->next = books->next;
- newBook->prev = books;
- if(books->next) books->next->prev = newBook;
- books->next = newBook;
- books = books->next;
- bookNumber++;
- }
- resetIDs(0);
- fclose(booksFile);
- }
- void deinit(){
- while(users->prev) users = users->prev;
- while(books->prev) books = books->prev;
- delete books;
- delete users;
- exit(0);
- }
- char* registrateUser(){
- file* usersFile = fopen(usersPath,"ab");
- if(!usersFile) return "File system error";
- user* newUser = new user;
- char* key = new char[256];
- newUser->login = new char[256];
- newUser->pass = new char[256];
- cout<<"\tYour login: ";
- cin>>newUser->login;
- cout<<"\tPassword: ";
- cin>>newUser->pass;
- cout<<"\tAdmin key: "; cin>>key;
- newUser->admin = !strcmp(key,"lol")?true:false;
- newUser->prev = users->prev;
- newUser->next = users;
- if(users->prev) users->prev->next=newUser;
- users->prev = newUser;
- //save 2 file
- fwrite(strrev(newUser->login),1,256,usersFile);
- fwrite(strrev(newUser->pass),1,256,usersFile);
- fwrite(&newUser->admin,1,1,usersFile);
- fclose(usersFile);
- delete key;
- return (newUser->admin?"\tAdmin account registered successfully.\n":"\tUser account registered successfully.\n");
- }
- char* login(){
- char* log = new char[256];
- char* pas = new char[256];
- cout<<"\tYour login: ";
- cin>>log;
- cout<<"\tPassword: ";
- cin>>pas;
- while(users->prev) users=users->prev;
- while(users->next){
- if(!strcmp(pas,users->next->pass) && !strcmp(log,users->next->login)){
- currentUser = users;
- return (users->next->admin?"\tLogin successfully. Admin mode is on.\n":"\tLogin successfully. Admin mode is off.\n");
- }
- users=users->next;
- }
- return "\tError, invalid login or password.\n";
- }
- book* getBookByID(int id){
- while(books->prev) books=books->prev;
- while(books->next){
- if(books->next->id==id){
- return books->next;
- }else{
- books=books->next;
- }
- }
- return null;
- }
- void resetIDs(int firstID){
- while(books->prev) books=books->prev;
- while(books->next){
- books->id=firstID;
- books=books->next;
- firstID++;
- }
- books->id=firstID;
- }
- void swap(book* a,book* b){
- book temp = *a;
- a->authors = b->authors;
- a->authorsNumber = b->authorsNumber;
- a->birthyear = b->birthyear;
- a->name = b->name;
- a->price = b->price;
- a->publishing = b->publishing;
- b->authors = temp.authors;
- b->authorsNumber = temp.authorsNumber;
- b->birthyear = temp.birthyear;
- b->name = temp.name;
- b->price = temp.price;
- b->publishing = temp.publishing;
- }
- char* sortList(){
- resetIDs(0);
- for(int i=1;i<=bookNumber;i++){
- for(int j=i+1;j<=bookNumber;j++){
- book* a = getBookByID(i);
- book* b = getBookByID(j);
- if(strcmp(a->name,b->name)==1){
- swap(a,b);
- }
- }
- }
- for(int i=1;i<=bookNumber;i++){
- for(int j=i+1;j<=bookNumber;j++){
- book* a = getBookByID(i);
- book* b = getBookByID(j);
- if(a->birthyear>b->birthyear){
- swap(a,b);
- }
- }
- }
- resetIDs(0);
- file* booksFile = fopen(booksPath,"wb");
- while(books->prev) books = books->prev;
- while(books->next){
- fwrite(books->next->name,1,256,booksFile);
- fwrite(books->next->publishing,1,256,booksFile);
- fwrite(&books->next->birthyear,1,4,booksFile);
- fwrite(&books->next->price,1,4,booksFile);
- fwrite(&books->next->authorsNumber,1,4,booksFile);
- for(int i=0;i<books->next->authorsNumber;i++){
- fwrite(books->next->authors[i],1,256,booksFile);
- }
- books = books->next;
- }
- fclose(booksFile);
- return "\tSorted successfully.\n";
- }
- char* addBook(){
- if(!currentUser) return "\tAccess denied. Please, login before.\n";
- if(!currentUser->admin) return "\tAccess denied. You need admin rights to add the book.\n";
- book* newBook = new book;
- newBook->name = new char[256];
- newBook->publishing = new char[256];
- cout<<"\tName: "; cin>>newBook->name;
- cout<<"\tAuthors number: "; cin>>newBook->authorsNumber;
- newBook->authors = new char*[newBook->authorsNumber];
- for(int i=0;i<newBook->authorsNumber;i++){
- newBook->authors[i] = new char[256];
- cout<<"\t\t"<<i+1<<". Author: ";
- cin>>newBook->authors[i];
- }
- cout<<"\tPublishing: "; cin>>newBook->publishing;
- cout<<"\tYear: "; cin>>newBook->birthyear;
- cout<<"\tPrice: "; cin>>newBook->price;
- while(books->next) books=books->next;
- newBook->prev = books;
- newBook->next = books->next;
- if(books->next) books->next->prev=newBook;
- books->next = newBook;
- resetIDs(0);
- file* booksFile = fopen(booksPath,"ab");
- fwrite(newBook->name,1,256,booksFile);
- fwrite(newBook->publishing,1,256,booksFile);
- fwrite(&newBook->birthyear,1,4,booksFile);
- fwrite(&newBook->price,1,4,booksFile);
- fwrite(&newBook->authorsNumber,1,4,booksFile);
- for(int i=0;i<newBook->authorsNumber;i++){
- fwrite(newBook->authors[i],1,256,booksFile);
- }
- fclose(booksFile);
- bookNumber++;
- return "\tBook added successfully.\n";
- }
- char* deleteBook(){
- if(!currentUser) return "\tAccess denied. Please, login before.\n";
- if(!currentUser->admin) return "\tAccess denied. You need admin rights to add the book.\n";
- book* bookForDelete = null;
- char* bookName = new char[256];
- cout<<"\tBook title: ";
- cin>>bookName;
- while(books->prev) books = books->prev;
- while(books->next){
- if(!strcmp(books->next->name,bookName)){
- bookForDelete = books->next;
- break;
- }
- books = books->next;
- }
- if(!bookForDelete) return "\tWrong book title.\n";
- if(bookForDelete->next) bookForDelete->next->prev = bookForDelete->prev;
- if(bookForDelete->prev) bookForDelete->prev->next = bookForDelete->next;
- delete[] bookForDelete->name;
- delete[] bookForDelete->publishing;
- for(int i=0;i<bookForDelete->authorsNumber;i++){
- delete[] bookForDelete->authors[i];
- }
- delete[] bookForDelete->authors;
- delete bookForDelete;
- file* booksFile = fopen(booksPath,"wb");
- while(books->prev) books = books->prev;
- while(books->next){
- fwrite(books->next->name,1,256,booksFile);
- fwrite(books->next->publishing,1,256,booksFile);
- fwrite(&books->next->birthyear,1,4,booksFile);
- fwrite(&books->next->price,1,4,booksFile);
- fwrite(&books->next->authorsNumber,1,4,booksFile);
- for(int i=0;i<books->next->authorsNumber;i++){
- fwrite(books->next->authors[i],1,256,booksFile);
- }
- books = books->next;
- }
- fclose(booksFile);
- resetIDs(0);
- return "\tBook deleted successfully.\n";
- }
- void trace(){
- if(!currentUser){
- cout<<"\tAccess denied. Please, login before.\n";
- }else{
- while(books->prev) books = books->prev;
- while(books->next){
- cout<<"\t"<<books->next->id<<". "<<books->next->name<<"\n";
- cout<<"\t Authors:\n";
- for(int i=0;i<books->next->authorsNumber;i++){
- cout<<"\t\t"<<i+1<<". "<<books->next->authors[i]<<"\n";
- }
- cout<<"\t Year: "<<books->next->birthyear<<"\n";
- cout<<"\t Price: "<<books->next->price<<"\n";
- cout<<"\t Publishing: "<<books->next->publishing<<"\n";
- books = books->next;
- }
- }
- }
- void tracePublishings(int type){
- if(!currentUser){
- cout<<"\tAccess denied. Please, login before.\n";
- }else{
- publishing * publishings = new publishing;
- publishings->name = new char[256];
- publishings->next = null;
- publishings->prev = null;
- while(books->prev) books = books->prev;
- while(books->next){
- bool firstFlag = true;
- while(publishings->prev) publishings = publishings->prev;
- while(publishings->next){
- if(!strcmp(publishings->next->name, books->next->publishing)){
- book* newBook = new book;
- *newBook = *books->next;
- newBook->next = publishings->next->books->next;
- newBook->prev = publishings->next->books;
- if(publishings->next->books->next) publishings->next->books->next->prev = newBook;
- publishings->next->books->next = newBook;
- publishings->next->booksNumber++;
- for(int i=0;i<books->next->authorsNumber;i++){
- bool secondFlag = true;
- while(publishings->next->authors->prev) publishings->next->authors = publishings->next->authors->prev;
- while(publishings->next->authors->next){
- if(!strcmp(publishings->next->authors->next->name, books->next->authors[i])){
- secondFlag = false;
- break;
- }
- publishings->next->authors = publishings->next->authors->next;
- }
- if(secondFlag){
- author* newAuthor = new author;
- newAuthor->name = new char[256];
- strcpy(newAuthor->name, books->next->authors[i]);
- publishings->next->authorsNumber++;
- newAuthor->next = publishings->next->authors->next;
- newAuthor->prev = publishings->next->authors;
- if(publishings->next->authors->next) publishings->next->authors->next->prev = newAuthor;
- publishings->next->authors->next = newAuthor;
- }
- }
- firstFlag = false;
- break;
- }
- publishings = publishings->next;
- }
- if(firstFlag){
- publishing * newPublishing = new publishing;
- newPublishing->name = new char[256];
- newPublishing->authorsNumber = 0;
- newPublishing->books = new book;
- *newPublishing->books = *books->next;
- newPublishing->books->next = null;
- newPublishing->books->prev = null;
- newPublishing->booksNumber = 1;
- newPublishing->authors = new author;
- newPublishing->authors->next = null;
- newPublishing->authors->prev = null;
- strcpy(newPublishing->name, books->next->publishing);
- for(int i=0;i<books->next->authorsNumber;i++){
- author* newAuthor = new author;
- newAuthor->name = new char[256];
- strcpy(newAuthor->name, books->next->authors[i]);
- newPublishing->authorsNumber++;
- newAuthor->next = newPublishing->authors->next;
- newAuthor->prev = newPublishing->authors;
- if(newPublishing->authors->next) newPublishing->authors->next->prev = newAuthor;
- newPublishing->authors->next = newAuthor;
- }
- newPublishing->next = publishings->next;
- newPublishing->prev = publishings;
- if(publishings->next) publishings->next->prev = newPublishing;
- publishings->next = newPublishing;
- }
- books = books->next;
- }
- ///// out
- if(type==0){
- int i = 0;
- while(publishings->prev) publishings = publishings->prev;
- while(publishings->next){
- if(publishings->next->authorsNumber>1){
- i++;
- cout<<"\t"<<i<<". "<<publishings->next->name<<"\n";
- while(publishings->next->authors->prev) publishings->next->authors = publishings->next->authors->prev;
- while(publishings->next->authors->next){
- cout<<"\t\t- "<<publishings->next->authors->next->name<<"\n";
- publishings->next->authors = publishings->next->authors->next;
- }
- }
- publishings = publishings->next;
- }
- }else if(type==1){
- while(publishings->prev) publishings = publishings->prev;
- for(int i=1;publishings->next;i++){
- cout<<"\t"<<i<<". "<<publishings->next->name<<" - "<<publishings->next->booksNumber;
- if(publishings->next->booksNumber<2){
- cout<<" book.\n";
- }else{
- cout<<" books.\n";
- }
- while(publishings->next->books->prev) publishings->next->books = publishings->next->books->prev;
- for(int i=0;i<publishings->next->booksNumber;publishings->next->books = publishings->next->books->next, i++){
- cout<<"\t\t"<<i+1<<". "<<publishings->next->books->name<<"\n";
- }
- publishings = publishings->next;
- }
- }else{
- int begin;
- int end;
- cout<<"\tBegin year: ";
- cin>>begin;
- cout<<"\tEnd year: ";
- cin>>end;
- cout<<"\n\tPublishings:\n";
- while(publishings->prev) publishings = publishings->prev;
- while(publishings->next){
- bool leftFlag = true;
- for(int i=begin;i<=end;i++){
- bool rightFlag = true;
- while(publishings->next->books->prev) publishings->next->books = publishings->next->books->prev;
- for(int j=0;j<publishings->next->booksNumber;j++){
- if(publishings->next->books->birthyear==i){
- rightFlag = false;
- break;
- }
- publishings->next->books = publishings->next->books->next;
- }
- if(rightFlag){
- leftFlag = false;
- break;
- }
- }
- if(leftFlag){
- cout<<"\t\t - "<<publishings->next->name<<"\n";
- }
- publishings = publishings->next;
- }
- }
- //деструктор
- //while(publishings->prev) publishings = publishings->prev;
- //delete publishings;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement