Advertisement
tegusta

Ordinamento libri (struct)

Mar 19th, 2012
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. /*
  2. Studente : Scia Massimiliano
  3. Classe : 3IC
  4. Data : 19/03/2012  16:19
  5. Nome del file : ordinamento libri
  6. */
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10. #include <ctime>
  11. #include <cstdlib>
  12. #include <cctype>
  13. #include <windows.h>
  14. #include <fstream>
  15. #include <time.h>
  16. #include <string>
  17. #include <stdio.h>
  18. #define N 100
  19.  
  20. using namespace std;
  21.  
  22. /*void struct_swap(book *a, book *b){
  23.     book temp;
  24.     temp.price=a->price;
  25.     a->price=b->price;
  26.     b->price=temp.price;
  27. }//struct_swap*/
  28.  
  29. struct book{
  30.     float price;
  31.     string title;
  32.     string author;
  33. };//book
  34.  
  35. void bg_col(){
  36.     system("color 70");
  37. }
  38.  
  39. void end(void){
  40.     fflush(stdin);
  41.     cout<<"\n\nPremere Invio per continuare.";
  42.     getchar();
  43. }//end
  44.  
  45. int dim(){
  46.     int n;
  47.     cout<<"\nQuanti libri vuoi inserire? ";
  48.     cin>>n;
  49.     return n;
  50. }//dim
  51.  
  52. book books_info(){
  53.     book b;
  54.     fflush(stdin);
  55.     cout<<"\nAutore/i: ";
  56.     getline(cin,b.author);
  57.     fflush(stdin);
  58.     cout<<"\nTitolo: ";
  59.     getline(cin,b.title);
  60.     fflush(stdin);
  61.     cout<<"\nPrezzo: ";
  62.     cin>>b.price;
  63.     fflush(stdin);
  64.     return b;
  65. }//books_info
  66.  
  67. void books(book b[], int n){
  68.     cout<<"\n\n";
  69.     for(int i=0;i<n;i++){
  70.         cout<<"\n\t\tLibro "<<i+1<<" :\n";
  71.         b[i]=books_info();
  72.     }//for
  73. }//books
  74.  
  75. void sort_by_price(book b[], int n){
  76.     for(int i=0;i<n;i++)
  77.         for(int j=i+1;j<n;j++)
  78.             if(b[i].price>b[j].price)
  79.                 swap(b[i],b[j]);
  80. }//sort_by_price
  81.  
  82. void sort_by_title(book b[], int n){
  83.     for(int i=0;i<n;i++)
  84.         for(int j=i+1;j<n;j++)
  85.             if(b[i].title.compare(b[j].title)>0)
  86.                 swap(b[i],b[j]);
  87. }//sort_by_title
  88.  
  89. void sort_by_author(book b[], int n){
  90.     for(int i=0;i<n;i++)
  91.         for(int j=i+1;j<n;j++)
  92.             if(b[i].author.compare(b[j].author)>0)
  93.                 swap(b[i],b[j]);
  94. }//sort_by_author
  95.  
  96. void visualize(book b[] , int n){
  97.     cout<<"\n\nLe informazioni riguardanti i libri sono:\n";
  98.     for(int i=0;i<n;i++){
  99.         cout<<"\n\nAutore/i: "<<b[i].author;
  100.         cout<<"\n\nTitolo: "<<b[i].title;
  101.         cout<<"\n\nPrezzo: "<<b[i].price;
  102.     }//for
  103. }//visualize
  104.  
  105. void visualize_p(book b[] , int n){
  106.     cout<<"\n\nOrdinamento per prezzo:\n";
  107.     for(int i=0;i<n;i++){
  108.         cout<<"\n\nAutore/i: "<<b[i].author;
  109.         cout<<"\n\nTitolo: "<<b[i].title;
  110.         cout<<"\n\nPrezzo: "<<b[i].price;
  111.     }//for
  112. }//visualize
  113.  
  114. void visualize_t(book b[] , int n){
  115.     cout<<"\n\nOrdinamento per titolo:\n";
  116.     for(int i=0;i<n;i++){
  117.         cout<<"\n\nAutore/i: "<<b[i].author;
  118.         cout<<"\n\nTitolo: "<<b[i].title;
  119.         cout<<"\n\nPrezzo: "<<b[i].price;
  120.     }//for
  121. }//visualize
  122.  
  123. void visualize_a(book b[] , int n){
  124.     cout<<"\n\nOrdinamento per autore:\n";
  125.     for(int i=0;i<n;i++){
  126.         cout<<"\n\nAutore/i: "<<b[i].author;
  127.         cout<<"\n\nTitolo: "<<b[i].title;
  128.         cout<<"\n\nPrezzo: "<<b[i].price;
  129.     }//for
  130. }//visualize
  131.  
  132. int main(){
  133.     book biblioteca[N];
  134.     bg_col();
  135.     int n=dim();
  136.     books(biblioteca,n);
  137.     visualize(biblioteca,n);
  138.     sort_by_price(biblioteca,n);
  139.     visualize_p(biblioteca,n);
  140.     sort_by_title(biblioteca,n);
  141.     visualize_t(biblioteca,n);
  142.     sort_by_author(biblioteca,n);
  143.     visualize_a(biblioteca,n);
  144.     end();
  145.     return 0;
  146. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement