write it in C programming Write a program that manages the bookshelf Use book st
ID: 3902347 • Letter: W
Question
write it in C programming
Write a program that manages the bookshelf Use book structure Struct book char name[30]; char semi_ISBN[5] int page; 4 digits Extend previous assignment problem (chapter 10) Add "save & load" functionality. There are 6 options menu 1. Display my bock s) . Add a book 3. Delete a book 4. Sort my books Display the book(s) on your bookshelf Add a book to the bookshelf Delete a book from the bookshelf Sort the books in alphabetic order Save the list of books as a text file Exit the program Save as a text file 6, Exit Choose a number:Explanation / Answer
You have not provided the previous code. So I have to code the complete program. I have completed everything except loading from file because of less time.. Have implementing the saving part. Please psot loading part as saperate question
Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 50
typedef struct
{
char name[30];
char semi_ISBN[5];
int page;
} book;
void display_books(book *, int);
void add_book(book *, int *);
void delete_book(book *, int *);
void sort_books(book *, int);
void saveToFile(book *,int);
int main()
{
int choice, n = 0;
book bookshelf[MAX_BOOKS];
do
{
printf(" ***** Menu ****** ");
printf("1. Display my book(s) ");
printf("2. Add a book ");
printf("3. Delete a book ");
printf("4. Sort my books ");
printf("5. Save as a text file ");
printf("6. Exit ");
printf("************ ");
printf("Choose a number: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
display_books(bookshelf, n);
break;
case 2:
add_book(bookshelf, &n);
break;
case 3:
delete_book(bookshelf, &n);
break;
case 4:
sort_books(bookshelf, n);
break;
case 5:
saveToFile(bookshelf, n);
case 6:
printf("Good Bye! ");
break;
}
} while (choice != 5);
return 0;
}
void saveToFile(book *b,int n) {
FILE * fp;
/* open the file for writing*/
fp = fopen ("books.txt","w");
/* write 10 lines of text into the file stream*/
int i;
for(i = 0; i < n;i++){
fprintf (fp, "%s:%s:%d ",b[i].name, b[i].semi_ISBN, b[i].page);
}
/* close the file*/
fclose (fp);
printf(" File saved successfully as books.txt. ");
}
void display_books(book *b, int n){
printf(" There is %d book(s) ", n);
int i;
for(i=0; i<n; i++){
printf("%s (ISBN %s), %d pages ", b[i].name, b[i].semi_ISBN, b[i].page);
}
}
void add_book(book *b, int *n){
printf(" Please enter the name : ");
scanf(" %[^ ]s", b[*n].name);
printf("semi_ISBN : ");
scanf("%s", b[*n].semi_ISBN);
printf("the number of page : ");
scanf("%d", &b[*n].page);
(*n)++;
}
void delete_book(book *b, int *n){
char bookname[30];
printf(" Please enter the book name to be removed : ");
scanf(" %[^ ]s", bookname);
int i, j, found = 0;
for(i=0; i<*n; i++){
if(!strcmp(bookname, b[i].name)){
for(j = i; j<(*n)-1; j++){
book t = b[j];
b[j] = b[j+1];
b[j+1] = t;
}
found = 1;
(*n)--;
}
}
if(found)
printf("%s is removed from your bookshelf ", bookname);
else
printf("%s is not in your bookshelf ", bookname);
}
void sort_books(book *b, int n){
int i, j;
for(i=0;i<n; i++){
for(j=0;j<n-i-1;j++){
if(strcmp(b[j].name, b[j+1].name)>1){
book t = b[j];
b[j] = b[j+1];
b[j+1] = t;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.