Question: can someone write detailed comments for this code #include stdafx.h #include #include #include library.h #include using namespace std; bool checkIn(const int ID, Book inventory[], const
can someone write detailed comments for this code
#include "stdafx.h"
#include
#include
#include "library.h"
#include
using namespace std;
bool checkIn(const int ID, Book inventory[], const int numBooks)
{
int ind = findBook(ID, inventory, numBooks);
if (ind == -1)
return false;
inventory[ind].checkedOut = true;
return true;
}
void checkIn(Book inventory[], const int numBooks)
{
int ID;
cout << "Enter book ID u want to check in" << endl;
cin >> ID;
checkIn(ID, inventory, numBooks);
}
bool checkOut(const int ID, Book inventory[], const int numBooks)
{
int ind = findBook(ID, inventory, numBooks);
if (ind == -1)
return false;
inventory[ind].checkedOut = false;
return true;
}
void checkOut(Book inventory[], const int numBooks)
{
int ID;
cout << "Enter book ID u want to check out" << endl;
cin >> ID;
checkOut(ID, inventory, numBooks);
}
//Sorting
void sort(Book inventory[], int numBooks)
{
for (int i = 0; i { for (int j = 0; j < numBooks-1; j++) { if (inventory[j].ID > inventory[j + 1].ID) { string tauthor, ttitle; int tid; bool tcb; tauthor = inventory[j].author; ttitle = inventory[j].title; tid = inventory[j].ID; tcb = inventory[j].checkedOut; inventory[j].author = inventory[j + 1].author; inventory[j].title = inventory[j + 1].title; inventory[j].ID = inventory[j + 1].ID; inventory[j].checkedOut = inventory[j + 1].checkedOut; inventory[j + 1].author= tauthor; inventory[j+1].title= ttitle; inventory[j + 1].ID= tid; inventory[j + 1].checkedOut= tcb ; } } } } bool addBook(const std::string & author, const std::string & title, const int ID, Book inventory[], int & numBooks) { inventory[numBooks].author = author; inventory[numBooks].title = title; inventory[numBooks].ID = ID; inventory[numBooks].checkedOut = false; numBooks++; return true; } void addNewBook(int & currID, Book inventory[], int & numBooks) { string author; string title; string dummy; getline(cin,dummy); cout << "Enter author name" << endl; getline(cin, author); cout << "Enter title " << endl; getline(cin, title); cout << "Enter Bookd ID " << endl; cin >> currID; addBook(author, title, currID, inventory, numBooks); sort(inventory, numBooks); } void removeBook(Book inventory[], int & numBooks) { int ID; cout << "Enter book ID u want to find" << endl; cin >> ID; removeBook(ID, inventory, numBooks); } bool removeBook(const int ID, Book inventory[], int & numBooks) { int ind = findBook(ID, inventory, numBooks); if (ind == -1) return false; for (int i = ind ; i < numBooks+1; i++) inventory[i] = inventory[i + 1]; numBooks--; return true; } bool loadBooks(const std::string & fileName, Book inventory[], int & numBooks, int & lastID) { std::ifstream file(fileName); std::string line; std::getline(file, line); //cout << fileName << endl; while (std::getline(file, line)) { //cout << line << endl; stringstream s(line); string author; string title; int ID; string stat; bool cf; s >> author; s >> title; s >> ID; s >> stat; if (stat == "t") cf = true; else cf = false; int ind = findBook(ID, inventory, numBooks); if (ind != -1) continue; inventory[numBooks].author = author; inventory[numBooks].title = title; inventory[numBooks].ID = ID; inventory[numBooks].checkedOut = cf; numBooks++; } file.close(); cout << numBooks << endl; return true; } bool saveBooks(const std::string & fileName, const Book inventory[], const int numBooks, const int lastID) { ofstream myfile(fileName); myfile << numBooks << " "; for (int i = 0; i < numBooks; i++) { myfile << inventory[i].author << "\t" << inventory[i].title << "\t" << inventory[i].ID << "\t"; if (inventory[i].checkedOut) myfile << "t "; else myfile << "f "; } myfile.close(); return true; } int findBook(const int ID, const Book inventory[], const int numBooks) { //Binary Search int i = 0, j = numBooks; while (i <= j) { int mid = (i + j) / 2; if (inventory[mid].ID == ID) return mid; if (inventory[mid].ID > ID) j = mid - 1; else i = mid + 1; } /*for (int i = 0; i < numBooks; i++) { if (inventory[i].ID == ID) return i; }*/ return -1; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
