Advertisement
Niloy007

Nidhu's Problem

Jun 3rd, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  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 = 3;
  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\n", 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.     index = -1;
  65.     for (i = 0; i < inputSize; i++) {
  66.         if (strcmp(book[i].authorName, userInput) == 0) {
  67.             index = i;
  68.             break;
  69.         }
  70.     }
  71.  
  72.     // Author wise book
  73.     if (index == -1) {
  74.         printf("Author is not in the list\n");
  75.     } else {
  76.         fprintf(authorWiseBook, "%s %d %.2lf %s %d %s", book[index].name, book[index].id, book[index].price, book[index].authorName, book[index].publicationYear, book[index].issueDate);
  77.     }
  78.  
  79.     // Publication year wise book
  80.     for (i = 0; i < inputSize; i++) {
  81.         if(book[i].publicationYear >= 1991 && book[i].publicationYear <= 2000) {
  82.             fprintf(publicationYearFile, "%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);
  83.         }
  84.     }
  85.  
  86.     // Take input from user
  87.     printf("Enter more two book information: \n");
  88.     for (i = 0; i < 2; i++) {
  89.         printf("Enter book Name: ");
  90.         fflush(stdin);
  91.         scanf("%s", &book[i].name);
  92.         fflush(stdout);
  93.         printf("Enter book id: ");
  94.         scanf("%d", &book[i].id);
  95.         printf("Enter book price: ");
  96.         scanf("%lf", &book[i].price);
  97.         printf("Enter book author name: ");
  98.         scanf("%s", &book[i].authorName);
  99.         printf("Enter publication year: ");
  100.         scanf("%d", &book[i].publicationYear);
  101.         printf("Enter issue date: ");
  102.         fflush(stdin);
  103.         gets(book[i].issueDate);
  104.         fflush(stdout);
  105.     }
  106.  
  107.     // Append the infromation in input file
  108.     for (i = 0; i < 2; i++) {
  109.         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);
  110.     }
  111.  
  112.     fclose(input);
  113.     fclose(output);
  114.     fclose(authorWiseBook);
  115.     fclose(publicationYearFile);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement