Advertisement
tdttvd

4.7

Feb 24th, 2022
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. #define MAX 100
  8.  
  9. typedef struct
  10. {
  11.     string name;
  12.     int books[5];
  13. }
  14. lib;
  15.  
  16. typedef struct
  17. {
  18.     int id;
  19.     int year;
  20.     string genre;
  21. }
  22. product;
  23.  
  24. typedef struct auth
  25. {
  26.     string name;
  27.     int books[5];
  28.     auth()
  29.     {
  30.         for (int i = 0; i < 5; ++i) books[i] = 0;
  31.     }
  32. }
  33. auth;
  34.  
  35.  
  36. void manage(string library, string book, string author){
  37.     // TODO
  38.     // Save info
  39.     lib libs[MAX];
  40.     product books[MAX];
  41.     auth authors[MAX];
  42.  
  43.     // Read file library
  44.     ifstream inLib(library);
  45.     int n;
  46.     inLib >> n;
  47.     for (int i = 0; i < n; ++i)
  48.     {
  49.         inLib >> libs[i].name;
  50.         for (int j = 0; j < 5; ++j)
  51.         {
  52.             inLib >> libs[i].books[j];
  53.         }
  54.     }
  55.  
  56.     // Read file book
  57.     ifstream inBook(book);
  58.     int m;
  59.     inBook >> m;
  60.     for (int i = 0; i < m; ++i)
  61.     {
  62.         inBook >> books[i].id;
  63.         inBook >> books[i].year;
  64.         inBook >> books[i].genre;
  65.     }
  66.  
  67.     // Read file author
  68.     ifstream inAuth(author);
  69.     int p;
  70.     inAuth >> p;
  71.     string s;
  72.     getline(inAuth, s);
  73.     int id, index;
  74.     for (int i = 0; i < p; ++i)
  75.     {
  76.         getline(inAuth, s);
  77.         ofstream outTemp("temp.txt");
  78.         outTemp << s;
  79.         outTemp.close();
  80.         ifstream inTemp("temp.txt");
  81.         inTemp >> authors[i].name;
  82.         index = 0;
  83.         while (inTemp >> id)
  84.         {
  85.             authors[i].books[index] = id;
  86.             ++index;
  87.         }
  88.         inTemp.close();
  89.     }
  90.  
  91.     // Read input
  92.     string lib_name, auth_name;
  93.     cin >> lib_name;
  94.     cin >> auth_name;
  95.  
  96.     int auth_index = -1;
  97.     for (int i = 0; i < p; ++i)
  98.     {
  99.         if (auth_name == authors[i].name)
  100.         {
  101.             auth_index = i;
  102.             break;
  103.         }
  104.     }
  105.  
  106.     int lib_index = -1;
  107.     for (int i = 0; i < n; ++i)
  108.     {
  109.         if (lib_name == libs[i].name)
  110.         {
  111.             lib_index = i;
  112.             break;
  113.         }
  114.     }
  115.  
  116.     if (auth_index == -1||lib_index == -1)
  117.     {
  118.         cout << "False";
  119.         return;
  120.     }
  121.  
  122.     for (int i = 0; i < 5; ++i)
  123.     {
  124.         id = authors[auth_index].books[i];
  125.         if (id == 0) break;
  126.         for (int j = 0; j < 5; ++j)
  127.         {
  128.             if (id == libs[lib_index].books[j])
  129.             {
  130.                 cout << "True";
  131.                 return;
  132.             }
  133.         }
  134.     }
  135.  
  136.     cout << "False";
  137. }
  138.  
  139. int main()
  140. {
  141.     string library = "library.txt";
  142.     string book = "book.txt";
  143.     string author = "author.txt";
  144.     manage(library, book, author);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement