Niloy007

Nidhu's Problem's Solution

Jun 3rd, 2020
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct library {
  5.     char name[200];
  6.     int id;
  7.     double price;
  8.     char authorName[200];
  9.     int publicationYear;
  10.     char issueDate[200];
  11. };
  12.  
  13. typedef struct library Library;
  14.  
  15. int main() {
  16.     Library book[20];
  17.     FILE *input = fopen("input.txt", "r+");
  18.     FILE *output = fopen("output.txt", "a");
  19.     FILE *authorWiseBook = fopen("author_wise_book.txt", "a");
  20.     FILE *publicationYearFile = fopen("publication_yr_1991_2000_book.txt", "a");
  21.     int i, inputSize = 5;
  22.     for (i = 0; i < inputSize; i++) {
  23.         fscanf(input, "%s", &book[i].name);
  24.         fscanf(input, "%d %lf", &book[i].id, &book[i].price);
  25.         fscanf(input, "%s", &book[i].authorName);
  26.         fscanf(input, "%d", &book[i].publicationYear);
  27.         fgets(book[i].issueDate, 100, input);
  28.     }
  29.  
  30.     // write book info in output file
  31.     for (i = 0; i < inputSize; i++) {
  32.         fprintf(output, "%s %d %.2lf %s %d %s", book[i].name, book[i].id, book[i].price, book[i].authorName, book[i].publicationYear, book[i].issueDate);
  33.     }
  34.  
  35.     double max = 0, min = 0;
  36.     int index = 0;
  37.     for (i = 0; i < inputSize; i++) {
  38.         if (book[i].price > max) {
  39.             max = book[i].price;
  40.             index = i;
  41.         }
  42.     }
  43.  
  44.     // printing height price book info in console
  45.     printf("Height price book info:\n");
  46.     printf("%s %d %.2lf %s %d %s\n", book[index].name, book[index].id, book[index].price, book[index].authorName, book[index].publicationYear, book[index].issueDate);
  47.  
  48.     index = 0;
  49.     for (i = 0; i < inputSize; i++) {
  50.         if (book[i].price < min) {
  51.             min = book[i].price;
  52.             index = i;
  53.         }
  54.     }
  55.  
  56.     // printing lowest price book info in console
  57.     printf("Lowest price book info:\n");
  58.     printf("%s %d %.2lf %s %d %s\n", book[index].name, book[index].id, book[index].price, book[index].authorName, book[index].publicationYear, book[index].issueDate);
  59.  
  60.     char userInput[200];
  61.     printf("Enter a author name for search:\n");
  62.     scanf("%s", &userInput);
  63.  
  64.     // Author wise book
  65.     index = -1;
  66.     for (i = 0; i < inputSize; i++) {
  67.         if (strcmp(book[i].authorName, userInput) == 0) {
  68.             fprintf(authorWiseBook, "%s %d %.2lf %s %d %s", book[i].name, book[i].id, book[i].price, book[i].authorName, book[i].publicationYear, book[i].issueDate);
  69.             index = i;
  70.         }
  71.     }
  72.     if (index == -1) {
  73.         printf("Author is not in the book library\n");
  74.     }
  75.  
  76.     // Publication year wise book
  77.     for (i = 0; i < inputSize; i++) {
  78.         if(book[i].publicationYear >= 1991 && book[i].publicationYear <= 2000) {
  79.             fprintf(publicationYearFile, "%s %d %.2lf %s %d %s", book[i].name, book[i].id, book[i].price, book[i].authorName, book[i].publicationYear, book[i].issueDate);
  80.         }
  81.     }
  82.  
  83.     // Take input from user
  84.     printf("Enter more two book information: \n");
  85.     for (i = 0; i < 2; i++) {
  86.         printf("Enter book Name: ");
  87.         fflush(stdin);
  88.         scanf("%s", &book[i].name);
  89.         fflush(stdout);
  90.         printf("Enter book id: ");
  91.         scanf("%d", &book[i].id);
  92.         printf("Enter book price: ");
  93.         scanf("%lf", &book[i].price);
  94.         printf("Enter book author name: ");
  95.         scanf("%s", &book[i].authorName);
  96.         printf("Enter publication year: ");
  97.         scanf("%d", &book[i].publicationYear);
  98.         printf("Enter issue date: ");
  99.         fflush(stdin);
  100.         gets(book[i].issueDate);
  101.         fflush(stdout);
  102.     }
  103.  
  104.     // Append the infromation in input file
  105.     for (i = 0; i < 2; i++) {
  106.         fprintf(input, "%s %d %.2lf %s %d %s\n", book[i].name, book[i].id, book[i].price, book[i].authorName, book[i].publicationYear, book[i].issueDate);
  107.     }
  108.  
  109.     fclose(input);
  110.     fclose(output);
  111.     fclose(authorWiseBook);
  112.     fclose(publicationYearFile);
  113. }
RAW Paste Data