Jan 24, 2022

Library-using typedef, enum, struct and dynamic memory allocation(C lang)

Virtual Database Library -in C lang-(use of typedef,enum,struct and dynamic memory allocation)

#include <stdio.h>
#include <stdlib.h>

typedef enum lang
{
    Hindi,
    Marathi,
    English,
    Urdu,
    Sanskrit,
    Other
} lang;

typedef struct book_data
{
    int page_no;
    char *dataPage;
} book_data;

typedef struct book
{
    lang book_lang;
    char *book_name;
    int pages;
    book_data *bookData;
} book;

typedef struct
{
    int no_of_books;
    book *books;

} library;

// store book data
void create_page(library *lib, int book_index,int page_num, char *page_data)
{
    if (lib->books[book_index].pages <= page_num)
    {
        printf("%s :%d Page not conatain in this book.",lib->books[book_index].book_name,page_num);
        return;
    }

    lib->books[book_index].bookData[page_num].page_no = page_num;
    lib->books[book_index].bookData[page_num].dataPage = (char *)malloc(sizeof(page_data));
    lib->books[book_index].bookData[page_num].dataPage = page_data;
}

// create book
void create_book(library *libr, int book_index, int paget, char *name,lang lan)
{
    libr->books[book_index].book_lang = lan;
    libr->books[book_index].book_name = name;
    libr->books[book_index].pages = paget;
    // book_data data_one;
    libr->books[book_index].bookData = (book_data *)calloc(paget , sizeof(book_data));
}

// create library
void create_lib(library *lib, int num_books)
{
    lib->no_of_books = num_books;
    // book book_one;
    // book_data bookData_one;
    lib->books = (book *)malloc(num_books * sizeof(book));
}


int main()
{
    library library_one, library_two, library_three;

    library *libraries[3] = {&library_one, &library_two, &library_three};
   
    //creating library compartment to store books of particular lang.
    create_lib(libraries[0], 100);

    //creating first book int it
    create_book(libraries[0], 0 ,120, "Kaphan",Hindi);
    create_book(libraries[0], 1 ,1, "Harry Potter",English);     //2nd book
   
    //creating page and storing data in it
    //create_page(libraries[index],book index,page number,data)
    create_page(libraries[0], 0 ,0, "Kaphan : Welcome to the first page");
    create_page(libraries[0], 0 ,1, "Kaphan : Welcome to the second page");
    create_page(libraries[0], 0 ,2, "Kaphan : Welcome to the third page");    
    printf("\n");
    create_page(libraries[0], 1 , 0, "Harry: Welcome to the first page");
    create_page(libraries[0], 1 ,100, "Harry: Welcome to the second page");
   
    printf("\n\n");

    printf("No. of Books Stored in Lirary : %d\n", libraries[0]->no_of_books);
    printf("No. of Pages in %s book :%d\n",libraries[0]->books[0].book_name, libraries[0]->books[0].pages);
    printf("Present Page number :%d\n", libraries[0]->books[0].bookData[1].page_no);

    printf("Data in %d(th) page of %s  :%s\n", libraries[0]->books[0].bookData[0].page_no+1,libraries[0]->books[0].book_name,libraries[0]->books[0].bookData[0].dataPage);
    printf("Data in %d(th) page of %s  :%s\n", libraries[0]->books[0].bookData[2].page_no+1,libraries[0]->books[0].book_name,libraries[0]->books[0].bookData[2].dataPage);
    printf("Data in %d(th) page of %s  :%s\n", libraries[0]->books[1].bookData[0].page_no+1,libraries[0]->books[1].book_name,libraries[0]->books[1].bookData[0].dataPage);
    return 0;
}

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...