Advertisement
Guest User

__3__

a guest
Dec 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<fstream>
  5.  
  6. using namespace std;
  7.  
  8. const size_t MAX_LEN = 256;
  9.  
  10. struct book {
  11.  
  12.     char author[MAX_LEN];
  13.     char name[MAX_LEN];
  14.     int edition;
  15.     int price;
  16.  
  17. };
  18.  
  19. void book_copy(const book &a, book &b){
  20.     strcpy(b.author, a.author);
  21.     strcpy(b.name, a.name);
  22.     b.edition = a.edition;
  23.     b.price = a.price;
  24. }
  25.  
  26. void read(book &b, istream &is){
  27.     is >> b.author >> b.name >> b.edition >> b.price;
  28. }
  29. void write(const book &b, ostream &os){
  30.     os << b.author << " "
  31.        << b.name << " "
  32.        << b.edition << " "
  33.        << b.price << "\n";
  34. }
  35.  
  36. int main(){
  37.  
  38.     ifstream in("input.txt");
  39.  
  40.     int n;
  41.     in >> n;
  42.  
  43.     book *lib = new book[n];
  44.     for (int i = 0; i < n; ++i){
  45.         read(lib[i], in);
  46.     }
  47.  
  48.     for (int i = 0; i < n; ++i){
  49.         for (int j = i + 1; j < n; ++j){
  50.             if (strcmp(lib[i].name, lib[j].name) > 0){
  51.                 swap(lib[i], lib[j]);
  52.             }
  53.         }
  54.     }
  55.  
  56.     in.close();
  57.  
  58.     ofstream out("input.txt");
  59.  
  60.     out << n << "\n";
  61.     for (int i = 0; i < n; ++i){
  62.         write(lib[i], out);
  63.     }
  64.  
  65.     delete[] lib;
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement