Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * File: HW2_Nikita_Mikhalchenko_185014MVEB.c
- * Author: Nikita Mikhalchenko
- * Created: 19.11.2019
- * Edited: 19.11.2019
- *
- * Description:
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LEN_MAX 30
- typedef struct{
- int bookID;
- char title[LEN_MAX];
- char author[LEN_MAX];
- int year;
- float self_cost;
- float sell_price;
- }book;
- typedef struct{
- int inventoryID;
- int bookID;
- int shelfN;
- int stock;
- }inventory;
- book * ReadFile1(int *);
- inventory * ReadFile2(int *);
- void show(book *books, inventory *catalogue, int bNum);
- void edit1(book *books, inventory *catalogue, int bNum);
- void edit2(book *books, inventory *catalogue, int bNum);
- void find(book *books, inventory *catalogue, int bNum);
- int main(void){
- int bNum = 0, cNum = 0;
- int ans;
- book *books = ReadFile1(&bNum);
- inventory *catalogue = ReadFile2(&cNum);
- do{
- printf("\nChose from following: 1 - show all, 2 - edit book");
- printf("title, 3 - edit shelf number, 4 - find books");
- printf("less than N, 0 - exit: ");
- scanf("%d", &ans);
- printf("\n");
- if(ans == 1){
- show(books, catalogue, bNum);
- }else if(ans == 2){
- edit1(books, catalogue, bNum);
- }else if(ans == 3){
- edit2(books, catalogue, bNum);
- }else if(ans == 4){
- find(books, catalogue, bNum);
- }
- }while(ans != 0);
- free(books);
- free(catalogue);
- return 0;
- }
- /**
- * Here the program is reading first input file
- */
- book * ReadFile1(int *bNum){
- FILE *fp = fopen("HW2_Nikita_Mikhalchenko_185014MVEB_input1.txt",
- "r");
- book *file1;
- if (fp == NULL){
- printf( "Error opening file1\n" );
- exit(2);
- }
- if((file1 = (book *)malloc(sizeof(book))) == NULL)
- {
- printf("ERROR: malloc books\n");
- exit(1);
- }
- while(fscanf(fp, "%d %s %s %d %f %f", &file1[*bNum].bookID,
- file1[*bNum].title, file1[*bNum].author, &file1[*bNum].year,
- &file1[*bNum].self_cost, &file1[*bNum].sell_price) == 6 ){
- (*bNum)++;
- if((file1 = (book*)realloc(file1, sizeof(book)*((*bNum)+1))) == NULL)
- {
- printf("ERROR: realloc prod\n");
- exit(1);
- }
- }
- fclose(fp);
- return(file1);
- }
- /**
- * Here the program is reading second input file
- */
- inventory * ReadFile2(int *cNum){
- FILE *fp = fopen("HW2_Nikita_Mikhalchenko_185014MVEB_input2.txt",
- "r");
- inventory *file2;
- if (fp == NULL){
- printf( "Error opening file2\n" );
- exit(2);
- }
- if((file2 = (inventory *)malloc(sizeof(inventory))) == NULL)
- {
- printf("ERROR: malloc books\n");
- exit(1);
- }
- while(fscanf(fp, "%d %d %d %d", &file2[*cNum].inventoryID,
- &file2[*cNum].bookID, &file2[*cNum].shelfN,
- &file2[*cNum].stock) == 4){
- (*cNum)++;
- if((file2 = (inventory*)realloc(file2, sizeof(inventory)*((*cNum)+1))) == NULL)
- {
- printf("ERROR: realloc prod\n");
- exit(1);
- }
- }
- fclose(fp);
- return(file2);
- }
- void show(book *books, inventory *catalogue, int bNum)
- {
- FILE *outf = fopen("out.txt", "w");
- for(int i = 0; i < bNum ; i++){
- if(books[i].bookID == catalogue[i].bookID){
- printf("%6d %s %s %d %.2f %.2f %d %d %d\n", books[i].bookID,
- books[i].title, books[i].author, books[i].year,
- books[i].self_cost, books[i].sell_price,
- catalogue[i].inventoryID, catalogue[i].shelfN,
- catalogue[i].stock);
- fprintf(outf, "%d %s %s %d %.2f %.2f %d %d %d", books[i].bookID,
- books[i].title, books[i].author, books[i].year,
- books[i].self_cost, books[i].sell_price,
- catalogue[i].inventoryID, catalogue[i].shelfN,
- catalogue[i].stock);
- }
- }
- fclose(outf);
- }
- void edit1(book *books, inventory *catalogue, int bNum){
- int ID;
- char bName[LEN_MAX];
- FILE *fp = fopen("HW2_Nikita_Mikhalchenko_185014MVEB_input1.txt",
- "r");
- printf("Enter the BookID to change book title: ");
- scanf("%d", &ID);
- printf("Enter the new name: ");
- scanf("%s", bName);
- fclose(fp);
- }
- void edit2(book *books, inventory *catalogue, int bNum){
- int ID;
- int sNum;
- FILE *fp = fopen("HW2_Nikita_Mikhalchenko_185014MVEB_input2.txt",
- "r");
- printf("Enter the inventoryID to change inventory shelf: ");
- scanf("%d", &ID);
- printf("Enter the new shelf number: ");
- scanf("%d" &sNum);
- fclose(fp);
- }
- void find(book *books, inventory *catalogue, int bNum){
- int N;
- FILE *outf = fopen("out2.txt", "w");
- printf("Enter the number to find books that have less pieces left: ");
- scanf("%d", &N);
- for(int i = 0; i < bNum ; i++){
- if(catalogue[i].stock < N){
- if(books[i].bookID == catalogue[i].bookID){
- printf("%6d %s %s %d %.2f %.2f %d %d %d\n", books[i].bookID,
- books[i].title, books[i].author, books[i].year,
- books[i].self_cost, books[i].sell_price,
- catalogue[i].inventoryID, catalogue[i].shelfN,
- catalogue[i].stock);
- fprintf(outf, "%d %s %s %d %.2f %.2f %d %d %d", books[i].bookID,
- books[i].title, books[i].author, books[i].year,
- books[i].self_cost, books[i].sell_price,
- catalogue[i].inventoryID, catalogue[i].shelfN,
- catalogue[i].stock);
- }
- }
- }
- fclose(outf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement