Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- struct library {
- char name[200];
- int id;
- double price;
- char authorName[200];
- int publicationYear;
- char issueDate[200];
- };
- typedef struct library Library;
- int main() {
- Library book[20];
- FILE *input = fopen("input.txt", "r+");
- FILE *output = fopen("output.txt", "a");
- FILE *authorWiseBook = fopen("author_wise_book.txt", "a");
- FILE *publicationYearFile = fopen("publication_yr_1991_2000_book.txt", "a");
- int i, inputSize = 3;
- for (i = 0; i < inputSize; i++) {
- fscanf(input, "%s", &book[i].name);
- fscanf(input, "%d %lf", &book[i].id, &book[i].price);
- fscanf(input, "%s", &book[i].authorName);
- fscanf(input, "%d", &book[i].publicationYear);
- fgets(book[i].issueDate, 100, input);
- }
- // write book info in output file
- for (i = 0; i < inputSize; i++) {
- 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);
- }
- double max = 0, min = 0;
- int index = 0;
- for (i = 0; i < inputSize; i++) {
- if (book[i].price > max) {
- max = book[i].price;
- index = i;
- }
- }
- // printing height price book info in console
- printf("Height price book info:\n");
- 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);
- index = 0;
- for (i = 0; i < inputSize; i++) {
- if (book[i].price < min) {
- min = book[i].price;
- index = i;
- }
- }
- // printing lowest price book info in console
- printf("Lowest price book info:\n");
- 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);
- char userInput[200];
- printf("Enter a author name for search:\n");
- scanf("%s", &userInput);
- index = -1;
- for (i = 0; i < inputSize; i++) {
- if (strcmp(book[i].authorName, userInput) == 0) {
- index = i;
- break;
- }
- }
- // Author wise book
- if (index == -1) {
- printf("Author is not in the list\n");
- } else {
- 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);
- }
- // Publication year wise book
- for (i = 0; i < inputSize; i++) {
- if(book[i].publicationYear >= 1991 && book[i].publicationYear <= 2000) {
- 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);
- }
- }
- // Take input from user
- printf("Enter more two book information: \n");
- for (i = 0; i < 2; i++) {
- printf("Enter book Name: ");
- fflush(stdin);
- scanf("%s", &book[i].name);
- fflush(stdout);
- printf("Enter book id: ");
- scanf("%d", &book[i].id);
- printf("Enter book price: ");
- scanf("%lf", &book[i].price);
- printf("Enter book author name: ");
- scanf("%s", &book[i].authorName);
- printf("Enter publication year: ");
- scanf("%d", &book[i].publicationYear);
- printf("Enter issue date: ");
- fflush(stdin);
- gets(book[i].issueDate);
- fflush(stdout);
- }
- // Append the infromation in input file
- for (i = 0; i < 2; i++) {
- 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);
- }
- fclose(input);
- fclose(output);
- fclose(authorWiseBook);
- fclose(publicationYearFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement