Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* MAIN.C */
- #include <stdio.h>
- #include <stdlib.h>
- #include "book_shop.h"
- #define MAX 64
- int main() {
- FILE* information = fopen("books_information.txt", "r");
- if (information != NULL) {
- printf("All right.\n");
- }
- else {
- printf("Something's wrong...\n");
- return -1;
- }
- int BooksNumber;
- fscanf(information, "%d", &BooksNumber);
- printf("\n");
- struct Book Books[10];
- printf("Entering the information about books in your shop...\n");
- ShopFilling(&information, Books, BooksNumber);
- printf("Press ENTER.\n");
- getchar();
- int UnprofitableBooks = 0;
- int* UnprofitableBooksNumbers = UnprofBooksNumbers(Books, BooksNumber, &UnprofitableBooks);
- printf("\n\nThe information about unprofitable books: \n");
- UnprofitableBooksInf(Books, UnprofitableBooksNumbers, UnprofitableBooks);
- fclose(information);
- return 0;
- }
- /* BOOK_SHOP.H */
- #ifndef BOOK_SHOP_H_INCLUDED
- #define BOOK_SHOP_H_INCLUDED
- #define MAX 64
- struct Book {
- char AuthorsName[MAX];
- char AuthorsLastname[MAX];
- char Title[MAX];
- int EditionYear; // God izdaniya
- double BuyingCost; // When the shop bought these books by someone
- double SellingCost; // When the shop sold these books to someone
- };
- void ShopFilling(FILE** information, struct Book* current_book, int books_number);
- void ShopShowing(struct Book* current_book, int books_number);
- void UnprofitableBooksInf(struct Book* current_book, int* unprofitable_books, int books_number);
- int* UnprofBooksNumbers(struct Book* current_book, int books_number, int* UnprofitableBooks);
- #endif // BOOK_SHOP_H_INCLUDED
- /* BOOK_SHOP.C */
- #include <stdio.h>
- #include <stdlib.h>
- #include "book_shop.h"
- void ShopFilling(FILE** information, struct Book* current_book, int books_number) {
- for (int i = 0; i < books_number; i++) {
- fscanf(*information, "%s", (char*)&(current_book[i].AuthorsName));
- fscanf(*information, "%s", (char*)&(current_book[i].AuthorsLastname));
- fscanf(*information, "%s", (char*)&(current_book[i].Title));
- fscanf(*information, "%d", &(current_book[i].EditionYear));
- fscanf(*information, "%lf", &(current_book[i].BuyingCost));
- fscanf(*information, "%lf", &(current_book[i].SellingCost));
- }
- }
- void UnprofitableBooksInf(struct Book* current_book, int* unprofitable_books, int books_number) {
- printf("\n%s|%8s|%10s|%18s|", "Number", "Name", "LastName", "Title");
- printf("%14s|%6s|%6s|\n", "Edition_Year", "Buy", "Sell");
- printf("__________________________________________________________________________\n\n");
- for (int i = 0; i < books_number; i++) {
- printf("%5d.|", i + 1);
- printf("%8s|", current_book[unprofitable_books[i]].AuthorsName);
- printf("%10s|", current_book[unprofitable_books[i]].AuthorsLastname);
- printf("%18s|", current_book[unprofitable_books[i]].Title);
- printf("%14d|", current_book[unprofitable_books[i]].EditionYear);
- printf("%3.2lf|", current_book[unprofitable_books[i]].BuyingCost);
- printf("%3.2lf|", current_book[unprofitable_books[i]].SellingCost);
- printf("\n___________________________________________________________________________\n\n");
- }
- }
- int* UnprofBooksNumbers(struct Book* current_book, int books_number, int* UnprofitableBooks) {
- int temp_value = *UnprofitableBooks; // = 0;
- for (int i = 0; i < books_number; i++) {
- if (current_book[i].SellingCost - current_book[i].BuyingCost < 0) {
- temp_value++;
- }
- }
- *UnprofitableBooks = temp_value;
- int* UnprofitableBooksNumbers = malloc(sizeof(int*) * temp_value);
- temp_value = 0;
- for (int i = 0; i < books_number; i++) {
- if (current_book[i].SellingCost - current_book[i].BuyingCost < 0) {
- UnprofitableBooksNumbers[temp_value] = i;
- temp_value++;
- }
- }
- return UnprofitableBooksNumbers;
- }
- /*BOOKS INFORMATION.TXT*/
- 10
- Valentin Bodrov Soul's_blossom 2017 109.99 209.99
- Lev Tolstoy War_and_peace 1981 599.99 469.99
- Ivan Bunin Dark_avenues 1961 259.99 169.99
- George Orwell 1984 2014 124.99 209.99
- Anthony Burgess Clockwork_Orange 2015 109.99 149.99
- Nansy Cox The_Vault 2006 259.99 234.99
- Catiche Lannes April_in_Lille 2009 149.99 289.99
- Andrew Richards How_do_I_survive 2013 169.99 104.99
- Janette Soult Toronto's_light 2006 249.99 254.99
- Emmett Brown Beatiche 2001 149.99 109.99
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement