can someone write detailed comments for this code #include \"stdafx.h\" #include
ID: 3752873 • Letter: C
Question
can someone write detailed comments for this code
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include "library.h"
#include <sstream>
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 <numBooks-1; 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 << " " << inventory[i].title << " " << inventory[i].ID << " ";
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;
}
Explanation / Answer
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include "library.h"
#include <sstream>
using namespace std;
// this functtion is used to check the book status
bool checkIn(const int ID, Book inventory[], const int numBooks)
{
// finding the book in the inventory, if not found return -1
int ind = findBook(ID, inventory, numBooks);
if (ind == -1)
return false;
inventory[ind].checkedOut = true;
return true;
}
// this function is used to check in the book
void checkIn(Book inventory[], const int numBooks)
{
int ID;
// taking book id from the user
cout << "Enter book ID u want to check in" << endl;
cin >> ID;
// calling user checkIn() to make the checkin
checkIn(ID, inventory, numBooks);
}
// this is used to checkout the book from inventory
bool checkOut(const int ID, Book inventory[], const int numBooks)
{
// finds the book from inventory
int ind = findBook(ID, inventory, numBooks);
// if not exist return false;
if (ind == -1)
return false;
// if exists make checkout flag false and return true
inventory[ind].checkedOut = false;
return true;
}
void checkOut(Book inventory[], const int numBooks)
{
int ID;
// read book id from user
cout << "Enter book ID u want to check out" << endl;
cin >> ID;
// call checkout function using given id
checkOut(ID, inventory, numBooks);
}
// this will sort the inventory based on the inventory id using bubble sort
void sort(Book inventory[], int numBooks)
{
for (int i = 0; i <numBooks-1; i++)
{
for (int j = 0; j < numBooks-1; j++)
{
// if it greater than swap
if (inventory[j].ID > inventory[j + 1].ID)
{
string tauthor, ttitle;
int tid;
bool tcb;
// swapping all the data
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 ;
}
}
}
}
// adds the book to the inventory
bool addBook(const std::string & author, const std::string & title, const int ID, Book inventory[], int & numBooks)
{
//inserting data into the inventory
inventory[numBooks].author = author;
inventory[numBooks].title = title;
inventory[numBooks].ID = ID;
inventory[numBooks].checkedOut = false;
numBooks++;
return true;
}
// reads data from user to add the new book
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;
// adding the book to inventory
addBook(author, title, currID, inventory, numBooks);
// sorting the inventory
sort(inventory, numBooks);
}
// removes the book from the inventory
void removeBook(Book inventory[], int & numBooks)
{
// reads book id from the user
int ID;
cout << "Enter book ID u want to find" << endl;
cin >> ID;
// calls removeBook to remove from the inventory
removeBook(ID, inventory, numBooks);
}
bool removeBook(const int ID, Book inventory[], int & numBooks)
{
// finds book from the inventory
int ind = findBook(ID, inventory, numBooks);
// if not exist return false
if (ind == -1)
return false;
// if exist shifting all books to its right
for (int i = ind ; i < numBooks+1; i++)
inventory[i] = inventory[i + 1];
// decreasing num of books
numBooks--;
return true;
}
// this is used to load the books from the file
bool loadBooks(const std::string & fileName, Book inventory[], int & numBooks, int & lastID)
{
// connecting with file
std::ifstream file(fileName);
std::string line;
std::getline(file, line);
//cout << fileName << endl;
// reading line by line
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;
// if book already exists not adding to the inventory
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;
}
// saves the books to the file
bool saveBooks(const std::string & fileName, const Book inventory[], const int numBooks, const int lastID)
{
// connecting with the file
ofstream myfile(fileName);
myfile << numBooks << " ";
// looping through the book to write into file
for (int i = 0; i < numBooks; i++)
{
myfile << inventory[i].author << " " << inventory[i].title << " " << inventory[i].ID << " ";
if (inventory[i].checkedOut)
myfile << "t ";
else
myfile << "f ";
}
myfile.close();
return true;
}
// finds the book in inventory based on given ID using binary search
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.