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

Could you write algorithm and comment for the fllowing C++ code. The form of alg

ID: 3699948 • Letter: C

Question

Could you write algorithm and comment for the fllowing C++ code. The form of algorithm and comment show in the picture. Thank you very much

#include <iostream>

#include <fstream>

#include <string>

#include <cstdlib>

#include <sstream>

using namespace std;

class Book {

private:

string title;

string author;

public:

string getTitle() {

return title;

}

void setTitle(string title) {

this->title = title;

}

string getAuthor() {

return author;

}

void setAuthor(string author) {

this->author = author;

}

Book(string title, string author) {

this->title = title;

this->author = author;

}

Book() {

title = "NONE";

author = "NONE";

}

};

class User {

private:

string name;

int ratings[100];

int numRatings;

public:

User(string name, int r[], int num) {

this->name = name;

this->numRatings = num;

for (int i = 0; i < num; i++) {

ratings[i] = r[i];

}

}

User() {

name = "NONE";

numRatings = 0;

for (int i = 0; i < 100; i++) {

ratings[i] = 0;

}

}

int getNumRatings() {

return numRatings;

}

void setNumRatings(int numRatings) {

this->numRatings = numRatings;

}

string getName() {

return name;

}

void setName(string name) {

this->name = name;

}

int getRatingAt(int index) {

if (index >= numRatings)

return -1000;

return ratings[index];

}

int setRatingAt(int index, int rating) {

if (index >= numRatings) {

return -1000;

}

if (rating != -5 && rating != -3 && rating != 0

&& rating != 1 && rating != 3 && rating != 5) {

cout << "Invalid Input!" << endl;

return -1;

}

ratings[index] = rating;

cout << "Success!" << endl;

return 0;

}

};

class Library {

private:

Book books[100];

User users[100];

int numOfUsers;

int numOfBooks;

int loginUser;

string toLower(string s) {

for (int i = 0;i < s.length();i++) {

if (s[i] >= 'A'&&s[i] <= 'Z') {

s[i] += 32;

}

}

return s;

}

int findUser(string name) {

for (int i = 0; i < numOfUsers;i++) {

if (users[i].getName() == name) {

return i;

}

}

return -1;

}

int findBook(string name) {

for (int i = 0; i < numOfBooks;i++) {

int len = books[i].getTitle().length();

if (books[i].getTitle().substr(0,len-1) == name) {

return i;

}

}

return -1;

}

int ssd(int i, int j) {

int sum = 0;

for (int k = 0;k < numOfBooks;k++) {

sum += ((users[i].getRatingAt(k) - users[j].getRatingAt(k)) * (users[i].getRatingAt(k) - users[j].getRatingAt(k)));

}

return sum;

}

int mostSimilar() {

int minssd;

int min;

if (loginUser == 0) {

minssd = ssd(loginUser, 1);

min = 1;

}

else {

minssd = ssd(loginUser, 0);

min = 0;

}

for (int i = 0;i < numOfUsers;i++) {

if (i == loginUser)

continue;

if (ssd(loginUser, i) < minssd) {

minssd = ssd(loginUser, i);

min = i;

}

}

return min;

}

public:

Library() :numOfUsers(0), numOfBooks(0) { }

void loadData() {

ifstream bookfile("books.txt");

ifstream userfile("ratings.txt");

if (!bookfile.is_open() || !userfile.is_open()) {

cout << "Launch unsuccessful" << endl;

cout << "Error! books or ratings file could not be found." << endl;

exit(1);

}

string line;

while (!bookfile.eof()) {

if (bookfile.fail()) {

break;

}

getline(bookfile, line);

if (line.empty()) {

continue;

}

line = toLower(line);

int comma = line.find(',');

books[numOfBooks].setAuthor(line.substr(0, comma));

books[numOfBooks].setTitle(line.substr(comma + 1));

numOfBooks++;

}

bookfile.close();

while (!userfile.eof()) {

if (userfile.fail()) {

break;

}

getline(userfile, line);

if (line.empty()) {

continue;

}

line = toLower(line);

int comma = line.find(',');

stringstream ss;

int ratings[100];

ss.str(line.substr(comma + 1));

for (int i = 0; i < numOfBooks;i++) {

int rating;

ss >> rating;

ratings[i] = rating;

}

users[numOfUsers++] = User(line.substr(0, comma), ratings, numOfBooks);

}

userfile.close();

cout << "Data Loaded successfully!" << endl;

}

void login() {

cout << "Welcome to the Library, What is your name?:" << endl;

string name;

getline(cin, name);

while (name.empty()) {

cout << "You provided an empty user name, Please provide a valid user name to login or register:" << endl;

cout << "Enter your name again:" << endl;

getline(cin, name);

}

string lowername = toLower(name);

int res = findUser(lowername);

if (res != -1) {

cout << "Welcome back, " << name << endl;

loginUser = res;

}

else {

cout << "Welcome to the Library, " << name << endl;

User newUser;

newUser.setName(toLower(name));

newUser.setNumRatings(numOfBooks);

loginUser = numOfUsers;

users[numOfUsers] = newUser;

numOfUsers++;

}

}

char menu() {

string input;

while (true) {

cout << "Would you like to (v)iew your ratings, (r)ate a book, (g)et recommendations, or (q)uit?:" << endl;

getline(cin, input);

if (input.length() == 1

&& (input[0] == 'v' || input[0] == 'V'

|| input[0] == 'r' || input[0] == 'R'

|| input[0] == 'g' || input[0] == 'G'

|| input[0] == 'q' || input[0] == 'Q')) {

return input[0];

}

else {

cout << "Please input a valid choice" << endl;

}

}

}

void viewRatings() {

bool hasRated = false;

for (int i = 0; i < numOfBooks;i++) {

if (users[loginUser].getRatingAt(i) != 0) {

hasRated = true;

break;

}

}

if (!hasRated) {

cout << "You have not rated any books as yet: " << endl;

return;

}

cout << "Here are the books that you have rated: " << endl;

for (int i = 0;i < numOfBooks;i++) {

if (users[loginUser].getRatingAt(i) != 0) {

string s1 = books[i].getTitle();

int ans = users[loginUser].getRatingAt(i);

int len = s1.length();

s1 = s1.substr(0,len-1);

cout << "Title : " << s1 << endl;

cout << "Rating : " << ans << endl;

cout << "------------------------------" << endl;

}

}

}

void rateBook() {

while (true) {

cout << "Enter the name of the book that you would like to rate:" << endl;

string title;

string str;

getline(cin, title);

str = title;

string lowerTitle = toLower(title);

cout << "Enter your rating of the book:" << endl;

int rating;

cin >> rating;

getchar();

int index = findBook(lowerTitle);

if (index == -1) {

cout << "The book you requested does not exist in the database" << endl;

}

else {

users[loginUser].setRatingAt(index, rating);

cout<<"Thank-you. The rating for the book titled "<<str<<" has been updated to "<<rating<<endl;

return;

}

}

}

void recommend() {

int similar = mostSimilar();

bool hasRecommend = false;

for (int i = 0; i < numOfBooks;i++) {

if (users[similar].getRatingAt(i) >= 3 && users[loginUser].getRatingAt(i) == 0) {

hasRecommend = true;

break;

}

}

if (!hasRecommend) {

cout << "There are no recommendations for you at present" << endl;

return;

}

cout << "Here are some of the books that we think you would like" << endl;

int recommendBooks = 0;

for (int i = 0; i < numOfBooks && recommendBooks < 10;i++) {

if (users[similar].getRatingAt(i) >= 3 && users[loginUser].getRatingAt(i) == 0) {

int l1 = books[i].getTitle().length();

int l2 = books[i].getAuthor().length();

cout << books[i].getTitle().substr(0,l1-1) << " by " << books[i].getAuthor()<< endl;

recommendBooks++;

}

}

}

void quit() {

ofstream out("ratings_new.txt");

for (int i = 0;i < numOfUsers;i++) {

out << users[i].getName() << ",";

for (int j = 0;j < numOfBooks;j++) {

out << users[i].getRatingAt(j) << " ";

}

out << endl;

}

out.close();

cout << "Data successfully saved. Goodbye!" << endl;

}

};

int main()

{

Library library;

library.loadData();

library.login();

char opt;

while (true) {

opt = library.menu();

switch (opt) {

case 'v':

case 'V':

library.viewRatings();

break;

case 'r':

case 'R':

library.rateBook();

break;

case 'g':

case 'G':

library.recommend();

break;

case 'q':

case 'Q':

library.quit();

return 0;

}

}

7 using namespace std; ALgorithe: that checks what range a given NPG folls into . 1. Take the mpg value passed to the function. 11 12 13 14 15 16 Comments (10 points) 2 Check f tt is greater than se If yes, then print "Nice Job . If not, then check f t ts greater than 25 Alorithm If yes, then print "Not great, but oay 4. If not, then print "So bad, so very, very bad (10 points) Input parometers: iles per gollon (Float type 18Output:dfferent string bosed on three cotegortes of G:50, 25-49, and Less than 25 Returns: nothing 21 23 void checkPG(Float mpg) 50) check f the Input volue ts greater than 5e 25 26 27 28 29 30 31 32 cout ee "Nice job e endl; 7 output message lse if(ng25) 7/ff not, check f is greater than 25 cout

Explanation / Answer

Hi Student,

Please find the comments and algos.

//include the headers

#include <iostream>

#include <fstream>

#include <string>

#include <cstdlib>

#include <sstream>

//namespace

using namespace std;

/*

* Class Book has two data members

* a. title - keeps the title of the book

* b. author - keeps the author of the book

*  

*/

class Book {

private:

string title; // data member title

string author; // data member author

public:

/* getter for private data member title */

string getTitle() {

return title;

}

/* setter for private data member title */

void setTitle(string title) {

this->title = title;

}

/* getter for private data member author */

string getAuthor() {

return author;

}

/* setter for private data member author */

void setAuthor(string author) {

this->author = author;

}

/* constructor with title and author */

Book(string title, string author) {

this->title = title;

this->author = author;

}

/* default constructor */

Book() {

title = "NONE";

author = "NONE";

}

};

/*

* Class User

*

*

*

*/

class User {

private:

string name;

int ratings[100];

int numRatings;

public:

/* constructor to set name and ratings and number ratings */

User(string name, int r[], int num) {

this->name = name;

this->numRatings = num;

for (int i = 0; i < num; i++) {

ratings[i] = r[i];

}

}

/* default constructor */

User() {

name = "NONE";

numRatings = 0;

/* initialize the ratings with initial value of 0 */

for (int i = 0; i < 100; i++) {

ratings[i] = 0;

}

}

/* getter for private data member numRatings */

int getNumRatings() {

return numRatings;

}

/* setter for private data member numRatings */

void setNumRatings(int numRatings) {

this->numRatings = numRatings;

}

/* getter for private data member name */

string getName() {

return name;

}

/* setter for private data member name */

void setName(string name) {

this->name = name;

}

/*

* Algorithm to get the ratings at a particular index

* 1. If index is greater than the total ratings

* returns -1000

* Else

* returns the rating at corresponding index

*/

int getRatingAt(int index) {

if (index >= numRatings) // condition check if index provided is greater than the total ratings  

return -1000;

return ratings[index];

}

/*

* Algorithm to set the ratings at a particular index

* 1. If index is greater than the total ratings

* returns -1

* 2.If index is any of -5,-3,0,3,5

* Sucessfully sets the rating to the index

* Prints Success message to console

* returns 0

* Else

* returns -1 for other ratings

*/

int setRatingAt(int index, int rating) {

if (index >= numRatings) {

return -1000;

}

if (rating != -5 && rating != -3 && rating != 0

&& rating != 1 && rating != 3 && rating != 5) {

cout << "Invalid Input!" << endl;

return -1;

}

ratings[index] = rating;

cout << "Success!" << endl;

return 0;

}

};

class Library {

private:

Book books[100]; // data member to store books - max 100

User users[100]; // data member to store users - max 100

int numOfUsers; // data member to keep count of num of users

int numOfBooks; // data members to keep count of num of books

int loginUser; // data member to keep login status

/*Algorithm toLower()

* Input : Takes a string

* Output : Returns the string after converting to lower case

*

*/

string toLower(string s) {

for (int i = 0;i < s.length();i++) {

if (s[i] >= 'A'&&s[i] <= 'Z') {

s[i] += 32;

}

}

return s;

}

/* Algorthm findUser()

* 1. Takes a name as input

* 2. Check is its already an user

* If Yes returns the position

* Else

* reurns -1

*   

* Input : String name

* Output : Returns the user num or -1 if not found

*

*/

int findUser(string name) {

for (int i = 0; i < numOfUsers;i++) {

if (users[i].getName() == name) {

return i;

}

}

return -1;

}

/* Algorthm findBook

* 1. Takes a name of book as input

* 2. Check is its already present

* If Yes returns the position

* Else

* reurns -1

*   

* Input : String name

* Output : Returns the user num or -1 if not found

*

*/

int findBook(string name) {

for (int i = 0; i < numOfBooks;i++) {

int len = books[i].getTitle().length(); // take the length of the title

if (books[i].getTitle().substr(0,len-1) == name) { // check if book is present

return i;

}

}

return -1;

}

int ssd(int i, int j) {

int sum = 0;

for (int k = 0;k < numOfBooks;k++) {

sum += ((users[i].getRatingAt(k) - users[j].getRatingAt(k)) * (users[i].getRatingAt(k) - users[j].getRatingAt(k)));

}

return sum;

}

int mostSimilar() {

int minssd;

int min;

if (loginUser == 0) {

minssd = ssd(loginUser, 1);

min = 1;

}

else {

minssd = ssd(loginUser, 0);

min = 0;

}

for (int i = 0;i < numOfUsers;i++) {

if (i == loginUser)

continue;

if (ssd(loginUser, i) < minssd) {

minssd = ssd(loginUser, i);

min = i;

}

}

return min;

}

public:

Library() :numOfUsers(0), numOfBooks(0) { }

/* ALgorithm loadData()

* Loads the books from books.txt

* Loads the ratings for books from ratings.txt

*

* Input : void

* Return : coid

*/

void loadData() {

ifstream bookfile("books.txt");

ifstream userfile("ratings.txt");

if (!bookfile.is_open() || !userfile.is_open()) {

cout << "Launch unsuccessful" << endl;

cout << "Error! books or ratings file could not be found." << endl;

exit(1);

}

string line;

while (!bookfile.eof()) {

if (bookfile.fail()) {

break;

}

getline(bookfile, line);

if (line.empty()) {

continue;

}

line = toLower(line);

int comma = line.find(',');

books[numOfBooks].setAuthor(line.substr(0, comma));

books[numOfBooks].setTitle(line.substr(comma + 1));

numOfBooks++;

}

bookfile.close();

while (!userfile.eof()) {

if (userfile.fail()) {

break;

}

getline(userfile, line);

if (line.empty()) {

continue;

}

line = toLower(line);

int comma = line.find(',');

stringstream ss;

int ratings[100];

ss.str(line.substr(comma + 1));

for (int i = 0; i < numOfBooks;i++) {

int rating;

ss >> rating;

ratings[i] = rating;

}

users[numOfUsers++] = User(line.substr(0, comma), ratings, numOfBooks);

}

userfile.close();

cout << "Data Loaded successfully!" << endl;

}

/* Algorithm login

* Takes username

* If exising user then print a welcome messge

* Else register the user and print a welcome message

*

* Input : void

* Outptut : void

*/

void login() {

cout << "Welcome to the Library, What is your name?:" << endl;

string name;

getline(cin, name);

while (name.empty()) {

cout << "You provided an empty user name, Please provide a valid user name to login or register:" << endl;

cout << "Enter your name again:" << endl;

getline(cin, name);

}

string lowername = toLower(name);

int res = findUser(lowername);

if (res != -1) {

cout << "Welcome back, " << name << endl;

loginUser = res;

}

else {

cout << "Welcome to the Library, " << name << endl;

User newUser;

newUser.setName(toLower(name));

newUser.setNumRatings(numOfBooks);

loginUser = numOfUsers;

users[numOfUsers] = newUser;

numOfUsers++;

}

}

char menu() {

string input;

while (true) {

cout << "Would you like to (v)iew your ratings, (r)ate a book, (g)et recommendations, or (q)uit?:" << endl;

getline(cin, input);

if (input.length() == 1

&& (input[0] == 'v' || input[0] == 'V'

|| input[0] == 'r' || input[0] == 'R'

|| input[0] == 'g' || input[0] == 'G'

|| input[0] == 'q' || input[0] == 'Q')) {

return input[0];

}

else {

cout << "Please input a valid choice" << endl;

}

}

}

void viewRatings() {

bool hasRated = false;

for (int i = 0; i < numOfBooks;i++) {

if (users[loginUser].getRatingAt(i) != 0) {

hasRated = true;

break;

}

}

if (!hasRated) {

cout << "You have not rated any books as yet: " << endl;

return;

}

cout << "Here are the books that you have rated: " << endl;

for (int i = 0;i < numOfBooks;i++) {

if (users[loginUser].getRatingAt(i) != 0) {

string s1 = books[i].getTitle();

int ans = users[loginUser].getRatingAt(i);

int len = s1.length();

s1 = s1.substr(0,len-1);

cout << "Title : " << s1 << endl;

cout << "Rating : " << ans << endl;

cout << "------------------------------" << endl;

}

}

}

void rateBook() {

while (true) {

cout << "Enter the name of the book that you would like to rate:" << endl;

string title;

string str;

getline(cin, title);

str = title;

string lowerTitle = toLower(title);

cout << "Enter your rating of the book:" << endl;

int rating;

cin >> rating;

getchar();

int index = findBook(lowerTitle);

if (index == -1) {

cout << "The book you requested does not exist in the database" << endl;

}

else {

users[loginUser].setRatingAt(index, rating);

cout<<"Thank-you. The rating for the book titled "<<str<<" has been updated to "<<rating<<endl;

return;

}

}

}

/* Algorithm recommend

*

*

*

*/

void recommend() {

int similar = mostSimilar();

bool hasRecommend = false;

for (int i = 0; i < numOfBooks;i++) {

if (users[similar].getRatingAt(i) >= 3 && users[loginUser].getRatingAt(i) == 0) {

hasRecommend = true;

break;

}

}

if (!hasRecommend) {

cout << "There are no recommendations for you at present" << endl;

return;

}

cout << "Here are some of the books that we think you would like" << endl;

int recommendBooks = 0;

for (int i = 0; i < numOfBooks && recommendBooks < 10;i++) {

if (users[similar].getRatingAt(i) >= 3 && users[loginUser].getRatingAt(i) == 0) {

int l1 = books[i].getTitle().length();

int l2 = books[i].getAuthor().length();

cout << books[i].getTitle().substr(0,l1-1) << " by " << books[i].getAuthor()<< endl;

recommendBooks++;

}

}

}

/*Algorithm Quit

* 1. Creates an ofstream to save the data

* 2. For all the users

* Print the name and ratings for all books

* 3. Save the data

* 4. Close the file

*

*/

void quit() {

ofstream out("ratings_new.txt");

for (int i = 0;i < numOfUsers;i++) {

out << users[i].getName() << ",";

for (int j = 0;j < numOfBooks;j++) {

out << users[i].getRatingAt(j) << " ";

}

out << endl;

}

out.close();

cout << "Data successfully saved. Goodbye!" << endl;

}

};

int main()

{

Library library; //initialize the library object

library.loadData(); // call data member

library.login(); // check for login

char opt;

while (true) {

opt = library.menu(); // display the menu

switch (opt) { //

case 'v':

case 'V':

library.viewRatings(); // show ratings

break;

case 'r':

case 'R':

library.rateBook(); // rate Book

break;

case 'g':

case 'G':

library.recommend(); // show recommendation

break;

case 'q':

case 'Q':

library.quit(); // quit the program

return 0;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote