Advertisement
sharli

sortStructura

Feb 8th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<string>
  4. using namespace std;
  5.  
  6. struct book {
  7.     string name;
  8.     string autor;
  9.     double price;
  10.     int year[100];
  11. };
  12. const int n=3;
  13. void input(book&);
  14. void print(book&);
  15. void sortprice(book*, double);
  16. int main() {
  17.     int i,n;
  18.     cin >> n;
  19.    
  20.     book library[100];
  21.  
  22.     for ( i = 0; i < n; i++)
  23.     {
  24.         input(library[i]);
  25.         cout << endl;
  26.     }
  27.    
  28.     for (i = 0; i < n; i++)
  29.     {
  30.         print(library[i]);
  31.         cout << endl;
  32.     }
  33.     sortprice(library, n);
  34.     for (i = 0; i < n; i++)
  35.     {
  36.         print(library[i]);
  37.         cout << endl;
  38.     }
  39.    
  40.    
  41.     return 0;
  42. }
  43.  
  44. void input(book& P) {
  45.    
  46.     cout << "Vavedete imeto na kigata:"; cin>>P.name ;
  47.     cout << "Vavedete imeto na avtora:"; cin >> P.autor;
  48.     cout << "Vavedete price na kigata:"; cin >> P.price;
  49.     cout << "Vavedete year na izdavane:";
  50.     for (int i = 0; i < n; i++)
  51.     {
  52.         cin >> P.year[i];
  53.     }
  54.    
  55. }
  56. void print(book& P) {
  57.  
  58.     cout << "Imeto na kigata :" << P.name << endl;
  59.     cout << "Autor na kigata :" << P.autor << endl;
  60.     cout << "Price na kigata :" << P.price << endl;
  61.     cout << "Kigata e izdadena :" << P.year << endl;
  62. }
  63. void sortprice(book* a, double x){
  64.     for (int i = 0; i < n; i++)
  65.     {
  66.         for (int j = 1; j < n; j++)
  67.         {
  68.             if (a[i].year > a[j].year)
  69.                 swap(a[i], a[j]);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement