1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
class Book
{
public:
//constructors
//default construcor (no parameters)
Book(); //sets book's default values
Book(string new_title, int new_year, string new_author); //reads in new values for the book's title, year, and author
Book(string all_data); //splits all_data string into separate title, year, and author
//getters
string get_title(); //function to get the book's title
int get_year(); //function to get the book's year
string get_author(); //function to get the book's author
//setters
void set_title(string new_title);
void set_year(int new_year);
void set_author(string new_author);
void print_book();
bool match_title(string target_title);
//bool match_year();
bool match_author(string target_author);
private:
string title;
int year;
string author;
string convert_title(string title);
string convert_author(string author);
};
int main()
{
Book b; //default constructor called
Book b2("Catcher In the Rye", 1951, "J. D. Salinger");
b.set_title("The Hitchhiker's Guide to the Galaxy");
b.set_year(1979);
b.set_author("Douglas Adams");
string all_data;
string target_title;
cout << "Enter all_data text: " << endl;
cin >> all_data;
Book b3(all_data);
cout << "Enter title to search: " << endl;
cin >> target_title;
b3.match_title(target_title);
b.print_book();
b2.print_book();
b3.print_book();
return 0;
}
Book::Book()
{
title = "***";
year = 0;
author = "***";
}
Book::Book(string new_title, int new_year, string new_author)
{
title = new_title;
year = new_year;
author = new_author;
}
Book::Book(string all_data)
{
int year2;
year = year2;
string s_year = to_string(year2);
int length = all_data.length();
int index = all_data.find('|', 0);
title = all_data.substr(0, index);
int index2 = all_data.find('|', index + 1);
s_year = all_data.substr(index + 1, 4);
author = all_data.substr(index2 + 1, length - index2);
int i_year = stoi(s_year);
year = i_year;
}
string Book::get_title()
{
return title;
}
int Book::get_year()
{
return year;
}
string Book::get_author()
{
return author;
}
void Book::set_title(string new_title)
{
title = new_title;
}
void Book::set_year(int new_year)
{
year = new_year;
}
void Book::set_author(string new_author)
{
author = new_author;
}
void Book::print_book()
{
cout << "Title: " << title << endl;
cout << "Year Published: " << year << endl;
cout << "Author: " << author << endl;
}
bool Book::match_title(string target_title)
{
if(title.find(target_title) != string::npos)
{
cout << "true" << endl;
return true;
}
else
{
cout << "false" << endl;
return false;
}
}
bool Book::match_author(string target_author)
{
if(author.find(target_author) != string::npos)
{
return true;
}
else
{
return false;
}
}
string Book::convert_title(string title)
{
string low_title;
low_title = title;
for(int i = 0; i < low_title.length(); i++)
{
low_title[i] = tolower(low_title[i]);
}
return(low_title);
}
string Book::convert_author(string author)
{
string low_author;
author = low_author;
for(int i = 0; i < low_author.length(); i++)
{
if(low_author[i] != ' ')
{
low_author[i] = tolower(low_author[i]);
}
}
return (low_author);
}
|