Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.27 KB | None | 0 0
  1. /* MAIN.C */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #include "book_shop.h"
  6.  
  7. #define MAX 64
  8.  
  9. int main() {
  10.   FILE* information = fopen("books_information.txt", "r");
  11.   if (information != NULL) {
  12.     printf("All right.\n");
  13.   }
  14.   else {
  15.     printf("Something's wrong...\n");
  16.     return -1;
  17.   }
  18.   int BooksNumber;
  19.   fscanf(information, "%d", &BooksNumber);
  20.   printf("\n");
  21.  
  22.   struct Book Books[10];
  23.   printf("Entering the information about books in your shop...\n");
  24.   ShopFilling(&information, Books, BooksNumber);
  25.   printf("Press ENTER.\n");
  26.   getchar();
  27.  
  28.   int UnprofitableBooks = 0;
  29.   int* UnprofitableBooksNumbers = UnprofBooksNumbers(Books, BooksNumber, &UnprofitableBooks);
  30.  
  31.   printf("\n\nThe information about unprofitable books: \n");
  32.   UnprofitableBooksInf(Books, UnprofitableBooksNumbers, UnprofitableBooks);
  33.  
  34.   fclose(information);
  35.   return 0;
  36. }
  37.  
  38. /* BOOK_SHOP.H */
  39. #ifndef BOOK_SHOP_H_INCLUDED
  40. #define BOOK_SHOP_H_INCLUDED
  41.  
  42. #define MAX 64
  43.  
  44. struct Book {
  45.   char AuthorsName[MAX];
  46.   char AuthorsLastname[MAX];
  47.   char Title[MAX];
  48.   int EditionYear; // God izdaniya
  49.   double BuyingCost; // When the shop bought these books by someone
  50.   double SellingCost; // When the shop sold these books to someone
  51. };
  52.  
  53. void ShopFilling(FILE** information, struct Book* current_book, int books_number);
  54. void ShopShowing(struct Book* current_book, int books_number);
  55. void UnprofitableBooksInf(struct Book* current_book, int* unprofitable_books, int books_number);
  56. int* UnprofBooksNumbers(struct Book* current_book, int books_number, int* UnprofitableBooks);
  57.  
  58. #endif // BOOK_SHOP_H_INCLUDED
  59.  
  60. /* BOOK_SHOP.C */
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63.  
  64. #include "book_shop.h"
  65.  
  66. void ShopFilling(FILE** information, struct Book* current_book, int books_number) {
  67.   for (int i = 0; i < books_number; i++) {
  68.     fscanf(*information, "%s", (char*)&(current_book[i].AuthorsName));
  69.     fscanf(*information, "%s", (char*)&(current_book[i].AuthorsLastname));
  70.     fscanf(*information, "%s", (char*)&(current_book[i].Title));
  71.     fscanf(*information, "%d", &(current_book[i].EditionYear));
  72.     fscanf(*information, "%lf", &(current_book[i].BuyingCost));
  73.     fscanf(*information, "%lf", &(current_book[i].SellingCost));
  74.   }
  75. }
  76.  
  77. void UnprofitableBooksInf(struct Book* current_book, int* unprofitable_books, int books_number) {
  78.   printf("\n%s|%8s|%10s|%18s|", "Number", "Name", "LastName", "Title");
  79.   printf("%14s|%6s|%6s|\n", "Edition_Year", "Buy", "Sell");
  80.   printf("__________________________________________________________________________\n\n");
  81.   for (int i = 0; i < books_number; i++) {
  82.     printf("%5d.|", i + 1);
  83.     printf("%8s|", current_book[unprofitable_books[i]].AuthorsName);
  84.     printf("%10s|", current_book[unprofitable_books[i]].AuthorsLastname);
  85.     printf("%18s|", current_book[unprofitable_books[i]].Title);
  86.     printf("%14d|", current_book[unprofitable_books[i]].EditionYear);
  87.     printf("%3.2lf|", current_book[unprofitable_books[i]].BuyingCost);
  88.     printf("%3.2lf|", current_book[unprofitable_books[i]].SellingCost);
  89.     printf("\n___________________________________________________________________________\n\n");
  90.  
  91.   }
  92. }
  93.  
  94. int* UnprofBooksNumbers(struct Book* current_book, int books_number, int* UnprofitableBooks) {
  95.   int temp_value = *UnprofitableBooks; // = 0;
  96.   for (int i = 0; i < books_number; i++) {
  97.     if (current_book[i].SellingCost - current_book[i].BuyingCost < 0) {
  98.       temp_value++;
  99.     }
  100.   }
  101.   *UnprofitableBooks = temp_value;
  102.  
  103.   int* UnprofitableBooksNumbers = malloc(sizeof(int*) * temp_value);
  104.   temp_value = 0;
  105.  
  106.   for (int i = 0; i < books_number; i++) {
  107.     if (current_book[i].SellingCost - current_book[i].BuyingCost < 0) {
  108.       UnprofitableBooksNumbers[temp_value] = i;
  109.       temp_value++;
  110.     }
  111.   }
  112.   return UnprofitableBooksNumbers;
  113. }
  114.  
  115. /*BOOKS INFORMATION.TXT*/
  116. 10
  117. Valentin Bodrov Soul's_blossom 2017 109.99 209.99
  118. Lev Tolstoy War_and_peace 1981 599.99 469.99
  119. Ivan Bunin Dark_avenues 1961 259.99 169.99
  120. George Orwell 1984 2014 124.99 209.99
  121. Anthony Burgess Clockwork_Orange 2015 109.99 149.99
  122. Nansy Cox The_Vault 2006 259.99 234.99
  123. Catiche Lannes April_in_Lille 2009 149.99 289.99
  124. Andrew Richards How_do_I_survive 2013 169.99 104.99
  125. Janette Soult Toronto's_light 2006 249.99 254.99
  126. Emmett Brown Beatiche 2001 149.99 109.99
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement