Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int compareNumbers(int number1, int number2);
  4. typedef struct Book
  5. {
  6.     char author[100];
  7.     char title[100];
  8.     int yearOfPublication;
  9.     int numberOfPages;
  10. } Book;
  11.  
  12. int main(int argc, char* argv[])
  13. {
  14.     char temp[100] ;
  15.     FILE * fPointer;
  16.     Book books[100];
  17.     fPointer = fopen(argv[1], "r");
  18.     int n = sizeof(books)/sizeof(Book);
  19.     int i;
  20.     while(!feof(fPointer))
  21.     {
  22.  
  23.         for(i = 0; i < n; i++ )
  24.         {
  25.             fgets(books[i].author,150,fPointer);
  26.             fgets(books[i].title,150,fPointer);
  27.             fgets(temp,150,fPointer);
  28.             books[i].yearOfPublication = atoi(temp);
  29.             fgets(temp,150,fPointer);
  30.             books[i].numberOfPages = atoi(temp);
  31.             printf("cos");
  32.         }
  33.     }
  34.     for(i = 0; i < n; i++ )
  35.     {
  36.         if( argv[2] == 'a')
  37.         {
  38.             qsort(books,n,sizeof(Book),strcmp(books[i].author,books[i + 1].author));
  39.  
  40.         }
  41.         else if( argv[2] == 't')
  42.         {
  43.             qsort(books,n,sizeof(Book),strcmp(books[i].title,books[i + 1].title));
  44.         }
  45.         else if ( argv[2] == 'y')
  46.         {
  47.             qsort(books,n,sizeof(Book),compareNumbers(books[i].yearOfPublication,books[i + 1].yearOfPublication));
  48.  
  49.         }
  50.     }
  51.  
  52.     for(i = 0; i < n; i++)
  53.     {
  54.         printf("Author: %s\nTitle: %s\nYear of publication:%d\nNumber of pages: %d\n\n",books[i].author,books[i].title,books[i].yearOfPublication,books[i].numberOfPages);
  55.     }
  56.  
  57.     return 0;
  58. }
  59. int compareNumbers(int number1, int number2)
  60. {
  61.     if(number1 < number2)
  62.         return -1;
  63.     else
  64.         return 0;
  65. }
  66. y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement