Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with these problems! please help #include <iostream> #include <fstre

ID: 3737037 • Letter: I

Question

I need help with these problems! please help

#include <iostream>

#include <fstream>

#include <string>

#include "Container.h"

#include "Book.h"

#include "Buy.h"

using namespace std;

// forward declarations

void flush();

void branching(char);

void helper(char);

void add_book(string, string, float);

void change_price(Book *);

Book* search_book(string,string);

void remove_book(string,string);

void print_all(Container*);

Container* list = NULL; // global list

int main()

{

char ch = 'i';

do {

cout << "Please enter your selection ";

cout << " a: add a new book to the list ";

cout << " c: change the price of a book ";

cout << " b: buy a book ";

cout << " r: remove a book from the list ";

cout << " p: print all books on the list ";

cout << " q: quit ";

cin >> ch;

flush();

branching(ch);

} while (ch != 'q');

list = NULL;

return 0;

}

void flush()

{

int c;

do c = getchar(); while (c != ' ' && c != EOF);

}

void branching(char c)

{

switch (c) {

case 'a':

case 'b':

case 'c':

case 'r':

case 'p':

helper(c);

break;

case 'q':

break;

default:

printf(" Invalid input! ");

}

}

// The helper function is used to determine how much data is needed and which function to send that data to.

// It uses pointers and values that are returned from some functions to produce the correct ouput.

// There is no implementation needed here, but you should study this function 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 helper(char c)

{

string title, author;

float price;

if (c == 'p')

print_all(list);

else

{

cout << endl << "Please enter the books's title: " << endl;

cin >> title;

cout << "Please enter the books's author: " << endl;

cin >> author;

cout << "Please enter the price of the book: " << endl;

cin >> price;

flush();

Book* book_result = search_book(title,author);

if (c == 'a') // add book

{

if (book_result == NULL)

{

add_book(title, author,price);

cout << endl << "Book was added." << endl << endl;

}

else

cout << endl << "Book is already on the list." << endl << endl;

}

else if (c == 'c')

{

if (book_result == NULL)

{

cout << endl << "Book was not found." << endl << endl;

return;

}

change_price(book_result);

}

else if (c == 'b') // buy book

{

if (book_result == NULL)

{

cout << endl << "Book was not found." << endl << endl;

return;

}

string date;

cout << "Please enter the date of the buying: " << endl;

cin >> date; flush();

book_result->buyBook(date);

cout << endl << "Date of buying is added." << endl << endl;

}

else if (c == 'r') // remove book

{

if (book_result == NULL)

{

cout << endl << "Book was not found." << endl << endl;

return;

}

remove_book(title,author);

cout << endl << "Book is removed from the list." << endl << endl;

}

}

}

// Q3: Add Book (5 points)

// This function will be used to add a new book to the head of you linked list of containers, no need for sorting.

// The search function is called before this function, therefore you can assume the book is not already on the list.

void add_book(string title, string author, float price)

{

// code here

}

// Q4: Search Book (5 points)

// This function will be used to search for a book on the list.

// You must traverse the list and return a pointer to a 'Book' with the desired title AND author.

// If the book does not exist on the list, return NULL. (See helper function for use of this function).

Book* search_book(string title,string author)

{

// code here

}

// Q5: Change price (5 points)

// This function will be used to change the price of a book.

// The function gets a pointer to the book and change the value of price using the set function inside the book class.

void change_price(Book * book)

{

// code here

}

// Q6: Remove Book (15 points)

// This function will be used to remove a book from the list.

// Traverse the list and use the parameters to remove the book.

// You must remove all the elements in the buys linked list.

void remove_book(string title, string author)

{

// code here

}

// Question 7. Print_all (10 points for this question)

// Write a recursive function to implement the print_all function.

// You will not receive any point if you use a while loop.

// Please use comments to indicate the four steps (1 point each step):

// (1) size-n problem, (2) stopping condition and return value,

// (3) size-(n-1) problem, (4) construct the solution of size-n problem

// You should print out the information of a book and also the number of times a book has been bought.

// For each time that a book has been bought, you should print out the date.

// You should use the methods inside the book.cpp file to print.

void print_all(Container* top)

{

// code here

}

Explanation / Answer


//Considering Container is like this
struct Container
{  
Book book;
struct Container *next;
}*start;


Ans 3
-------------------------------------------------------------------------------------------
void add_book(string title, string author, float price)
{
struct Container *temp, *s;
temp = new(struct Container);
if(temp == NULL)
{
cout<<"Memory not allocated"<<endl;
return ;
}
else
{

//initializing new book wiht given values
Book book(title, author, price);

//initializing book in Container with this new book
temp->book = book;
temp->next = NULL;
  
//add the elemnt in the end of linked list
s = start;
while (s->next != NULL)
{
s = s->next;

}
s->next = temp;
  
  
}

Ans 4
-----------------------------------------------------------------------------------------

Book* search_book(string title, string author)
{
struct Container *temp;
temp = start;

if (temp == NULL )
return NULL;


do {
temp = temp->next;
if (temp->book.author == author && temp->book.title == title)
return temp;
}while(temp != NULL);

return NULL;
}


Ans 5
--------------------------------------------------------------------------------------------

void change_price(Book * book)
{
Book* book_result = search_book(book.title, book.author);
if(book_result != NULL)
{
//callign set function inside book class
book_result.setprice();
}

}


Ans 6
----------------------------------------------------------------------------------------------

void remove_book(string title, string author)
{
// Store head node
struct Container *temp , *prev;
temp = start;

// If head node itself holds the key to be deleted
if (temp != NULL && temp->book.author == author && temp->book.title == title)
{
start = temp->next; // Changed head
free(temp); // free old head
return;
}

// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL temp->book.author == author && temp->book.title == title)
{
prev = temp;
temp = temp->next;
}

// If key was not present in linked list
if (temp == NULL) return;

// Unlink the node from linked list
prev->next = temp->next;

free(temp); // Free memory
}

Ans 7
-----------------------------------------------------------------------------------------
As I am not aware of book class and functions inside it I am just displaying the book information recursively


void print_all(Container* top)
{  
if(top == NULL)
{
cout<<" ";
return;
}

cout<<top->book.title<<" "<<top->book.author<<endl;

cout<<print_all(top->next);
}