#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #p
ID: 3732118 • Letter: #
Question
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable: 4996)
// used to create a linked list of containers, each contaning a "book"
struct container {
struct book *book;
struct container *next;
} *list = NULL;
// used to hold book information and linked list of "frequently bought together (fbt)"
struct book {
char title[30];
char author[30];
float price;
struct fbt *fbts;
};
// used to create a linked list of frequently bought together (fbt)
struct fbt {
struct book *book;
struct fbt *next;
};
// forward declaration of functions that have already been implemented
void flush();
void branching(char);
void registration(char);
// the following forward declarations are for functions that require implementation
// HW07
// Total: 50 points for hw07
// return type // name and parameters // points
void add_book(char*, char*, float); // 15
struct book* search_book(char*); // 10
void remove_book(char*); // 15
void print_all_books(); // 10
// HW08
// Total: 50 points for hw08
struct container * buy_book(); // 15
struct container * setfbt(struct container *); // 25
void display_fbt(struct container*); // 10
int main()
{
char ch = 'i';
printf("Book Information ");
do
{
printf("Please enter your selection: ");
printf(" a: add a new book to the list ");
printf(" s: search for a book on the list ");
printf(" r: remove a book from the list ");
printf(" l: print the list of all books ");
printf(" b: buy a book ");
printf(" q: quit ");
ch = tolower(getchar());
flush();
branching(ch);
} while (ch != 'q');
return 0;
}
// consume leftover ' ' characters
void flush()
{
int c;
do c = getchar(); while (c != ' ' && c != EOF);
}
// branch to different tasks
void branching(char c)
{
switch (c)
{
case 'a':
case 's':
case 'r':
case 'l':registration(c); break;
case 'b': {
struct container * buy = buy_book();
buy = setfbt(buy);
display_fbt(buy);
break;
}
case 'q': break;
default: printf("Invalid input! ");
}
}
//
// This function will determine what info is needed and which function to send that info to.
// It uses values that are returned from some functions to produce the correct ouput.
// There is no implementation needed here, but you should trace the code and know how it works.
// It is always helpful to understand how the code works before implementing new features.
// Do not change anything in this function or you risk failing the automated test cases.
void registration(char c)
{
if (c == 'a')
{
char input[100];
printf(" Please enter the book's info in the following format: ");
printf("title:author:price ");
fgets(input, sizeof(input), stdin);
// discard ' ' chars attached to input
input[strlen(input) - 1] = '';
char* title = strtok(input, ":"); // strtok used to parse string
char * author = strtok(NULL, ":");
float price = atof(strtok(NULL, ":"));
struct book* result = search_book(title);
if (result == NULL)
{
add_book(title, author, price);
printf(" Book added to list successfully ");
}
else
printf(" That book is already on the list ");
}
else if (c == 's' || c == 'r')
{
char title[30];
printf(" Please enter the book's title: ");
fgets(title, sizeof(title), stdin);
// discard ' ' chars attached to input
title[strlen(title) - 1] = '';
struct book* result = search_book(title);
if (result == NULL)
printf(" That book is not on the list ");
else if (c == 's') {
printf(" Title: %s ", result->title);
printf("Author: %s ", result->author);
printf("Price: %.2f ", result->price);
}
else
{
remove_book(title);
printf(" Book removed from the list ");
}
}
else
{
print_all_books();
}
}
Honestly have no idea how to do this.. Can you guys please help?
// hwe7 Q1 add (15 points) /1 This function should add book to the head of the list of containers. // The function search_book () is called before calling this function, // therefore you can assume that the book is not already on the list. void add_book (char* title, char* author, float price) // hwe7 Q2 search (10 points) // In this function, you are passed the title of a book to find // If the book exists on the list, return a pointer to the requested book. If not, return NULL // (You must return a pointer to a node in your list.) struct book- search_ book(char* title)Explanation / Answer
Hi friend, Except last two FUNCTIONS, I mplemented all. Question is long.
SO please repost other (last two functions) in separate post.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable: 4996)
// used to create a linked list of containers, each contaning a "book"
struct container {
struct book *book;
struct container *next;
} *list = NULL;
// used to hold book information and linked list of "frequently bought together (fbt)"
struct book {
char title[30];
char author[30];
float price;
struct fbt *fbts;
};
// used to create a linked list of frequently bought together (fbt)
struct fbt {
struct book *book;
struct fbt *next;
};
// forward declaration of functions that have already been implemented
void flush();
void branching(char);
void registration(char);
// the following forward declarations are for functions that require implementation
// HW07
// Total: 50 points for hw07
// return type // name and parameters // points
void add_book(char*, char*, float); // 15
struct book* search_book(char*); // 10
void remove_book(char*); // 15
void print_all_books(); // 10
// HW08
// Total: 50 points for hw08
struct container * buy_book(); // 15
struct container * setfbt(struct container *); // 25
void display_fbt(struct container*); // 10
int main()
{
char ch = 'i';
printf("Book Information ");
do
{
printf("Please enter your selection: ");
printf(" a: add a new book to the list ");
printf(" s: search for a book on the list ");
printf(" r: remove a book from the list ");
printf(" l: print the list of all books ");
printf(" b: buy a book ");
printf(" q: quit ");
ch = tolower(getchar());
flush();
branching(ch);
} while (ch != 'q');
return 0;
}
// consume leftover ' ' characters
void flush()
{
int c;
do c = getchar(); while (c != ' ' && c != EOF);
}
// branch to different tasks
void branching(char c)
{
switch (c)
{
case 'a':
case 's':
case 'r':
case 'l':registration(c); break;
case 'b': {
struct container * buy = buy_book();
buy = setfbt(buy);
display_fbt(buy);
break;
}
case 'q': break;
default: printf("Invalid input! ");
}
}
//
// This function will determine what info is needed and which function to send that info to.
// It uses values that are returned from some functions to produce the correct ouput.
// There is no implementation needed here, but you should trace the code and know how it works.
// It is always helpful to understand how the code works before implementing new features.
// Do not change anything in this function or you risk failing the automated test cases.
void registration(char c)
{
if (c == 'a')
{
char input[100];
printf(" Please enter the book's info in the following format: ");
printf("title:author:price ");
fgets(input, sizeof(input), stdin);
// discard ' ' chars attached to input
input[strlen(input) - 1] = '';
char* title = strtok(input, ":"); // strtok used to parse string
char * author = strtok(NULL, ":");
float price = atof(strtok(NULL, ":"));
struct book* result = search_book(title);
if (result == NULL)
{
add_book(title, author, price);
printf(" Book added to list successfully ");
}
else
printf(" That book is already on the list ");
}
else if (c == 's' || c == 'r')
{
char title[30];
printf(" Please enter the book's title: ");
fgets(title, sizeof(title), stdin);
// discard ' ' chars attached to input
title[strlen(title) - 1] = '';
struct book* result = search_book(title);
if (result == NULL)
printf(" That book is not on the list ");
else if (c == 's') {
printf(" Title: %s ", result->title);
printf("Author: %s ", result->author);
printf("Price: %.2f ", result->price);
}
else
{
remove_book(title);
printf(" Book removed from the list ");
}
}
else
{
print_all_books();
}
}
// hw07 Q1 : add (15 points)
// This function should add book to the head of the list of containers.
// The function search_book() is called before calling this function,
// therefore you can assume that the book is not already on the list.
void add_book(char* title, char* author, float price)
{
//set up the structure of information
struct book *insertBook;
struct container *cont;
//set size of structure
insertBook = (struct book*) malloc(sizeof(struct book));
cont = (struct container*) malloc(sizeof(struct container));
//copy information and store into the node
strcpy(insertBook->title, title);
strcpy(insertBook->author, author);
insertBook->price = price;
//move along with linked list
cont->book = insertBook;
cont->next = list;
list = cont;
}
// hw07 Q2 : search (10 points)
// In this function, you are passed the title of a book to find.
// If the book exists on the list, return a pointer to the requested book. If not, return NULL.
// (You must return a pointer to a node in your list.)
struct book* search_book(char* title)
{
struct container *insertContainer = list;
while (insertContainer != NULL) {
if (strcmp(insertContainer->book->title, title) == 0) {
return insertContainer->book;
} insertContainer = insertContainer->next;
} return NULL;
}
// hw07 Q3: remove_one (15)
// In this function, you are passed the title of a book to remove the corresponding book from the list.
// The search function is called before this function so you can assume that the book is on the list.
// You will need to find the book and remove it using proper memory management to ensure no memory leaks.
void remove_book(char* title)
{
struct container *previous = list;
struct container *current = list;
if (strcmp(list->book->title, title) == 0) {
previous = list;
list = list->next;
free(previous);
return;
}
else {
current = list->next;
previous = list;
while (current != NULL && previous != NULL) {
if (strcmp(list->book->title, title) == 0) {
struct book* temp = current;
previous->next = current->next;
free(temp);
return;
}
}
previous = current;
current = current->next;
} return;
}
// hw07 Q4: print_all_books (10)
// In this function, you should print the list of available books in the list.
//If there is no book in the list, you should print "There are no books on this list!"
void print_all_books()
{
struct container *insertContainer = list;
if(insertContainer == NULL) {
printf("There are no books on this list! ");
return;
}
while (insertContainer != NULL) {
printf(" Title: %s ", insertContainer->book->title);
printf("Author: %s ", insertContainer->book->author);
printf("Price: %.2f ", insertContainer->book->price);
insertContainer = insertContainer->next;
}
}
//////////////////////////// display_fbt function////////////////////////////
void display_fbt(struct container *first){
struct container *new_container=first;
for(new_container=first;new_container!= NULL; new_container=new_container->next){
printf("%s ",new_container->value);
}
printf(" ");
}
////////////////////////////////////buy_book function////////////////////////////
struct container *buy_book(struct container *first){
struct container *temp=first;
char ch = 'y';
printf("Press 'n' to stop ");
while(temp->next!=NULL){
printf("Book Title Name : %s ",temp->value);
scanf("%c",&ch);
if(ch=='n')
break;
temp=temp->next;
}
temp->next=NULL;
return temp;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.