Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- struct book{
- int num;
- char title[20];
- char author[20];
- int year;
- };
- typedef struct book book;
- void add_book(book *ptr, char t[20], char a[20], int y);
- void show(book *ptr);
- int count(book *ptr);
- void books_of(book *ptr, char a[20]);
- int main(){
- book b_arr[100] = {
- 1, "Wonder_of_universe", "Divyansh", 2005,
- 2, "Science_behind_us", "Yuvansh", 2004,
- 3, "Eternity", "Jatin", 2007,
- };
- book *ptr;
- ptr = &b_arr[0];
- int response=-1;
- char book_title[20];
- char book_author[20];
- int book_year;
- char w_author[20];
- printf("\n\t----------Welcome to library----------\n");
- printf("\tEnter (1) to Add book to the library\n");
- printf("\tEnter (2) to Display all the book in library\n");
- printf("\tEnter (3) to List all the book of a author\n");
- printf("\tEnter (4) to Know number of book in library\n");
- printf("\tEnter (0) to Exit the library.\n");
- while(response != 0){
- printf("\nEnter you response:");
- scanf("%d", &response);
- if(response == 1){
- printf("****Please avoid using spaces, instread use '_' and enter only first name of author****\n");
- printf("Enter book title: ");
- scanf("%s", &book_title);
- printf("Enter author name: ");
- scanf("%s", &book_author);
- printf("Enter year of publication: ");
- scanf("%d", &book_year);
- add_book(ptr, book_title, book_author, book_year);
- printf("Book added sucessfully\n------------------------------\n");
- }
- else if(response == 2){
- printf("\nFollwing are the books in library\n");
- show(ptr);
- printf("------------------------------\n");
- }
- else if(response == 3){
- printf("Enter name of author: ");
- scanf("%s", &w_author);
- books_of(ptr, w_author);
- printf("------------------------------\n");
- }
- else if(response == 4){
- printf("There are total %d books in library\n", count(ptr));
- printf("------------------------------\n");
- }
- }
- return 0;
- }
- void show(book *ptr){
- int n = count(ptr);
- for(int i=0; i<n; i++){
- printf("Book Number: %d\n", ptr->num);
- printf("Book Title: %s\n", ptr->title);
- printf("Author of book: %s\n", ptr->author);
- printf("Year of publication: %d\n", ptr->year);
- printf("\n");
- ptr++;
- }
- }
- void add_book(book *ptr, char t[20], char a[20], int y){
- int n = count(ptr);
- n++;
- ptr = ptr + (n-1);
- ptr->num = n;
- strcpy(ptr->title, t);
- strcpy(ptr->author, a);
- ptr->year = y;
- }
- int count(book *ptr){
- int c=0;
- for(int i=0; i<100; i++){
- if(ptr->num != 0){
- c++;
- }
- ptr++;
- }
- return c;
- }
- void books_of(book *ptr, char au[20]){
- int c, n=0;
- for(int i=0; i<100; i++){
- c = strcmp(au, ptr->author);
- if(c==0){
- printf("Avalible book: %s\n", ptr->title);
- n++;
- }
- ptr++;
- }
- if(n == 0){
- printf("There are no such books avalible.\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement