Advertisement
vencinachev

CourseWork

Dec 29th, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX_COUNT 100
  5. #define TITLE_SIZE 50
  6. #define AUTOR_SIZE 50
  7.  
  8. void addBook();
  9. void removeBook();
  10. void printBooks();
  11. void readFromFile();
  12. void writeToFile();
  13. void trim(char*);
  14.  
  15. typedef struct
  16. {
  17.     char title[TITLE_SIZE];
  18.     char autor[AUTOR_SIZE];
  19.     int pages;
  20. } Book;
  21.  
  22. Book library[MAX_COUNT];
  23. int count = 0;
  24.  
  25. int main()
  26. {
  27.     char option;
  28.     while (1)
  29.     {
  30.         printf("1. Add new book\n");
  31.         printf("2. Remove book at given number\n");
  32.         printf("3. Print books\n");
  33.         printf("4. Insert books from file\n");
  34.         printf("5. Save books to file\n");
  35.         printf("6. Exit\n");
  36.  
  37.         printf("-> Enter option: ");
  38.         option = getch();
  39.  
  40.         switch (option)
  41.         {
  42.             case '1': addBook(); break;
  43.             case '2': removeBook(); break;
  44.             case '3': printBooks(); break;
  45.             case '4': readFromFile(); break;
  46.             case '5': writeToFile(); break;
  47.             case '6': return 0;
  48.         }
  49.         getch(); // pause
  50.     }
  51.     return 0;
  52. }
  53.  
  54.  
  55. void addBook()
  56. {
  57.     char pages[10];
  58.     printf("\nEnter book title: ");
  59.     fgets(library[count].title, TITLE_SIZE, stdin);
  60.     trim(library[count].title);
  61.     printf("Enter book autor: ");
  62.     fgets(library[count].autor, AUTOR_SIZE, stdin);
  63.     trim(library[count].autor);
  64.     printf("Enter pages: ");
  65.     fgets(pages, 10, stdin);
  66.     library[count].pages = atoi(pages);
  67.     count++;
  68.     fflush(stdin);
  69. }
  70.  
  71. void removeBook()
  72. {
  73.     int num, i;
  74.     printf("\nEnter book number: ");
  75.     scanf("%d", &num);
  76.     if (num < 0 || num >= count)
  77.     {
  78.         printf("No such book!");
  79.         return;
  80.     }
  81.     for (i = num; i < count - 1; i++)
  82.     {
  83.         library[i] = library[i + 1];
  84.     }
  85.     count--;
  86.     fflush(stdin);
  87. }
  88.  
  89. void printBooks()
  90. {
  91.     int i;
  92.     printf("\n ---> Books list <---\n");
  93.     for (i = 0; i < count; i++)
  94.     {
  95.         printf("-- Book number %d --\n", i);
  96.         printf("Title: %s\n", library[i].title);
  97.         printf("Autor: %s\n", library[i].autor);
  98.         printf("Pages: %d\n", library[i].pages);
  99.         printf("-------------\n");
  100.     }
  101. }
  102.  
  103. void readFromFile()
  104. {
  105.     char filename[30], pages[10];
  106.     FILE *libFile;
  107.     fflush(stdin);
  108.     printf("\nEnter file to read books: ");
  109.     fgets(filename, 30, stdin);
  110.     trim(filename);
  111.     if (!(libFile = fopen(filename, "r")))
  112.     {
  113.         printf("File problem!");
  114.         return;
  115.     }
  116.  
  117.     while (!feof(libFile))
  118.     {
  119.         fgets(library[count].title, TITLE_SIZE, libFile);
  120.         trim(library[count].title);
  121.         fgets(library[count].autor, AUTOR_SIZE, libFile);
  122.         trim(library[count].autor);
  123.         fgets(pages, 10, libFile);
  124.         library[count].pages = atoi(pages);
  125.         count++;
  126.     }
  127.     fflush(stdin);
  128.     fclose(libFile);
  129. }
  130.  
  131. void writeToFile()
  132. {
  133.     int i;
  134.     char filename[30], pages[10];
  135.     FILE *libFile;
  136.  
  137.     printf("\nEnter file to write books: ");
  138.     fgets(filename, 30, stdin);
  139.     trim(filename);
  140.     if (!(libFile = fopen(filename, "w")))
  141.     {
  142.         printf("File problem!");
  143.         return;
  144.     }
  145.  
  146.     for (i = 0; i < count; i++)
  147.     {
  148.         fprintf(libFile, "%s\n", library[i].title);
  149.         fprintf(libFile, "%s\n", library[i].autor);
  150.         fprintf(libFile, "%d\n", library[i].pages);
  151.     }
  152.     fflush(stdin);
  153.     fclose(libFile);
  154. }
  155.  
  156. // remove new line symbol after fgets
  157. void trim(char *str)
  158. {
  159.     str[strlen(str) - 1] = '\0';
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement