Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- class Recipe {
- private:
- int numIngredients;
- char name[100];
- char *contents;
- public:
- Recipe() {
- numIngredients = 0;
- strcpy(name, "");
- contents = new char[0];
- }
- Recipe(int n, char s[], char *c) {
- numIngredients = n;
- strcpy(name, s);
- contents = new char[strlen(c) + 1];
- strcpy(contents, c);
- }
- Recipe &operator++() {
- numIngredients++;
- return *this;
- }
- friend ostream &operator<<(ostream &out, const Recipe &r) {
- out << r.numIngredients << endl << r.name << endl << r.contents << endl;
- return out;
- }
- friend inline bool operator==(const Recipe &a, const Recipe &b) {
- return (strcmp(a.name, b.name) == 0);
- }
- const char *getName() {
- return name;
- }
- const int getIngredients() {
- return numIngredients;
- }
- const char *getContents() {
- return contents;
- }
- void setIngredients(const int n) {
- numIngredients = n;
- }
- void setName(const char n[]) {
- strcpy(name, n);
- }
- void setContents(const char c[]) {
- contents = new char[strlen(c) + 1];
- strcpy(contents, c);
- }
- Recipe &operator=(const Recipe &from) {
- if (this != &from) {
- numIngredients = from.numIngredients;
- strcpy(name, from.name);
- strncpy(contents, from.contents, strlen(from.contents));
- contents[strlen(from.contents) + 1] = '\0';
- }
- return *this;
- }
- char operator[](int i) { return contents[i]; }
- const char operator[](int i) const { return contents[i]; }
- };
- class RecipesBook {
- private:
- char name[100];
- Recipe *recipes;
- int len;
- public:
- RecipesBook() {
- len = 0;
- strcpy(name, "");
- recipes = new Recipe[len];
- }
- RecipesBook(const char n[]) {
- len = 0;
- strcpy(name, n);
- recipes = new Recipe[len];
- }
- Recipe &operator[](int i) { return recipes[i]; }
- const Recipe &operator[](int i) const { return recipes[i]; }
- void recipeCopy(Recipe dest[], const Recipe source[], const int len) {
- for (int i = 0; i < len; ++i) {
- dest[i] = source[i];
- }
- }
- friend ostream &operator<<(ostream &out, const RecipesBook &b) {
- out << b.name << endl;
- for (int i = 0; i < b.len; ++i) {
- out << b.recipes[i] << endl;
- }
- return out;
- }
- RecipesBook newBook(Recipe &r) {
- RecipesBook book(name);
- for (int i = 0; i < len; ++i) {
- if (recipes[i].getIngredients() < r.getIngredients()) {
- book.recipes[book.len] = recipes[i];
- book.recipes[book.len].setIngredients(recipes[i].getIngredients() + 1);
- book.len++;
- }
- }
- return book;
- }
- RecipesBook &operator+=(const Recipe &r) {
- bool found = false;
- for (int i = 0; i < len && !found; ++i) {
- found = (recipes[i] == r);
- }
- if (!found) {
- Recipe *temp = new Recipe[len + 1];
- recipeCopy(temp, recipes, len);
- temp[len++] = r;
- delete[] recipes;
- recipes = temp;
- }
- return *this;
- }
- RecipesBook &operator=(const RecipesBook &r) {
- if (this != &r) {
- len = r.len;
- strcpy(name, r.name);
- recipes = new Recipe[r.len];
- recipeCopy(recipes, r.recipes, r.len);
- }
- return *this;
- }
- RecipesBook(const RecipesBook &r) {
- len = r.len;
- strcpy(name, r.name);
- recipes = new Recipe[r.len];
- recipeCopy(recipes, r.recipes, r.len);
- }
- };
- int main() {
- Recipe rec;
- int n;
- char name[100], contents[200];
- int num_ing;
- cin >> name >> n;
- RecipesBook b1(name);
- Recipe last;
- for (int i = 0; i < n; i++) {
- cin >> num_ing >> name >> contents;
- Recipe r(num_ing, name, contents);
- b1 += r;
- last = r;
- }
- cout << b1;
- cin >> num_ing >> name >> contents;
- rec.setIngredients(num_ing);
- rec.setName(name);
- rec.setContents(contents);
- b1 += rec;
- cout << b1;
- RecipesBook b2 = b1.newBook(rec);
- cout << b2;
- // testing copy constructor
- cout << "b2 copy" << endl;
- RecipesBook rb = b2;
- last.setName("changed-name");
- rb += last;
- cout << rb;
- cout << "original" << endl;
- cout << b2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement