Question: Programming Assignment (50 points) 1. Based on the previous homework, you should implement hw06q1.c from scratch. The structure of this homework is similar to previous

Programming Assignment (50 points)

1. Based on the previous homework, you should implement hw06q1.c from scratch. The structure of this homework is similar to previous homework. In this homework, to do the same work in the previous homework, you must use a linked list instead of an array.

2. You should use a struct libraryRecord with elements for Book Title, Authors Name, Book Type, Book Id, Aisle number in which the book should be kept.

You will be completing a program that creates a list of books (a linked list of structs). It is a menu-driven program where the user is given the following options: CSE240 Introduction to Programming Language 2 | Page Homework 06 a) Add a new book to the list. When adding a new book to the list, the user is prompted for Book Title, Author Name, Book Type, Book Id and Aisle number of the book. The book should be added at the end of the list. If the book (name or ID) already exists in the list, then you should not add to the list. The book type is an enum type.

b) Display the list of books from the linked list.

c) Sort the list of books numerically by ID. The sorting should happen within the list. You should not create a new list (linked list of structs) of books having sorted books.

d) Delete a book from the list. When deleting a book from the list, the user is prompted for Book ID only. The book should be removed from the list and the linked list structure should be preserved / restored afterwards. Attempting to delete a book who is not in the list should return 0. Otherwise, return 1 upon a successful removal of the record.

The behavior of save() in this assignment is the same as that of the previous homework. However, you should modify it to new save() that handles a linked list instead of an array. Please refer to the specification of save() in the previous homework. You need to implement load() which is called at the start of the program. This function will read the saved file and fill in the linked list of structures. You need to read the file in the same order and manner that it is saved in save(). #include #include #include #pragma warning(disable: 4996) // for Visual Studio Only

#define MAX_BOOKS 15 #define MAX_NAME_LENGTH 25

typedef enum { Nonfiction = 0, Fiction } bookType; // enum type

struct libraryRecord { // struct for book details char bookTitle[MAX_NAME_LENGTH]; char author[MAX_NAME_LENGTH]; bookType booktype; unsigned int bookId; unsigned int aisle; };

struct libraryRecord list[MAX_BOOKS]; // declare list of books int count = 0; // the number of books currently stored in the list (initialized to 0)

// functions already implmented void flushStdIn(); void executeAction(char); void save(char* fileName);

// functions that need implementation: int add(char* bookTitle_input, char* author_input, char* booktype_input, unsigned int bookId_input, unsigned int aisle_input); // 20 points void sort(); // 10 points int delete(unsigned int bookId_input); // 10 points void load(char* fileName); // 10 points void display(); // given

int main() { char* fileName = "Book_List.txt"; load(fileName); // load list of books from file (if it exists). Initially there will be no file. char choice = 'i'; // initialized to a dummy value do { printf(" Enter your selection: "); printf("\t a: add a new book "); printf("\t d: display book list "); printf("\t r: remove a book from list "); printf("\t s: sort book list by ID "); printf("\t q: quit "); choice = getchar(); flushStdIn(); executeAction(choice); } while (choice != 'q');

save(fileName); // save list of books to file (overwrites file, if it exists) return 0; }

// flush out leftover ' ' characters void flushStdIn() { char c; do c = getchar(); while (c != ' ' && c != EOF); }

// ask for details from user for the given selection and perform that action void executeAction(char c) { char bookTitle_input[MAX_NAME_LENGTH], author_input[MAX_NAME_LENGTH]; unsigned int bookId_input, aisle_input, add_result = 0; char booktype_input[20]; switch (c) { case 'a': // input book record from user printf(" Enter book title: "); fgets(bookTitle_input, sizeof(bookTitle_input), stdin); bookTitle_input[strlen(bookTitle_input) - 1] = '\0'; // discard the trailing ' ' char printf("Enter author name: "); fgets(author_input, sizeof(author_input), stdin); author_input[strlen(author_input) - 1] = '\0'; // discard the trailing ' ' char printf("Enter book type (Fiction/ Nonfiction): "); fgets(booktype_input, sizeof(booktype_input), stdin); booktype_input[strlen(booktype_input) - 1] = '\0'; // discard the trailing ' ' char printf("Please enter book ID number: "); scanf("%d", &bookId_input); printf("Please enter aisle number: "); scanf("%d", &aisle_input); flushStdIn();

// add the book to the list add_result = add(bookTitle_input, author_input, booktype_input, bookId_input, aisle_input); if (add_result == 0) printf(" Book is already on the list! "); else if (add_result == 1) printf(" Book successfully added to the list! "); else printf(" Unable to add. Book list is full! ");

break; case 'r': printf("Please enter ID number of book to be deleted: "); scanf("%d", &bookId_input); flushStdIn(); int delete_result = delete(bookId_input); if (delete_result == 0) printf(" Book not found in the list! "); else printf(" Book deleted successfully! "); break; case 'd': display(); break; case 's': sort(); break;

case 'q': break; default: printf("%c is invalid input! ", c); } } int add(char* bookTitle_input, char* author_input, char* booktype_input, unsigned int bookId_input, unsigned int aisle_input) { // Enter code here

return 0; // edit this line as needed }

void display() { char* booktypeString = "Nonfiction"; // dummy init

for (int i = 0; i

if (list[i].booktype == Nonfiction) // find what to display for book type booktypeString = "Nonfiction"; else booktypeString = "Fiction"; printf(" Book Type: %s", booktypeString); // display book type printf(" Book ID: %d", list[i].bookId); // display book id printf(" Aisle number: %d", list[i].aisle); // display aisle number printf(" "); }

} void sort() { struct libraryRecord libraryTemp; // needed for swapping structs. Not absolutely necessary to use. // Enter code here

// display message for user to check the result of sorting. printf(" Book list sorted! Use display option 'd' to view sorted list. "); } void save(char* fileName) { FILE* file; int i, booktypeValue = 0; file = fopen(fileName, "wb"); // open file for writing

fwrite(&count, sizeof(count), 1, file); // First, store the number of books in the list

// Parse the list and write book record to file

for (i = 0; i

fwrite(&booktypeValue, sizeof(booktypeValue), 1, file); fwrite(&list[i].bookId, sizeof(list[i].bookId), 1, file); fwrite(&list[i].aisle, sizeof(list[i].aisle), 1, file); }

fclose(file); // close the file after writing } int delete(unsigned int bookId_input) { struct libraryRecord libraryTemp; // needed for swapping structs. Not absolutely necessary to use. // Enter code here

return 0; // edit this line as needed } void load(char* fileName) { // Enter code here }

Programming Assignment (50 points) 1. Based on the previous homework, you should

Expected output of each function: Add (10 points): Book_list.txt not found. Enter your selection: a: add a new book d: display book list r: remove a book from list s: sort book list by ID q: quit la Enter book title: Sapiens Enter author name: Yuval Enter book type (Fiction/ Nonfiction): Nonfiction Please enter book ID number: 1 Please enter aisle number: 1 Book successfully added to the list! Expected output of each function: Add (10 points): Book_list.txt not found. Enter your selection: a: add a new book d: display book list r: remove a book from list s: sort book list by ID q: quit la Enter book title: Sapiens Enter author name: Yuval Enter book type (Fiction/ Nonfiction): Nonfiction Please enter book ID number: 1 Please enter aisle number: 1 Book successfully added to the list

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!