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 number_ingredients;
- char name[100];
- char* contents;
- void copyy(const Recipe &r) {
- number_ingredients = r.number_ingredients;
- strncpy(name, r.name, 99);
- contents = new char[100];
- strncpy(contents, r.contents, 99);
- name[99] = '\0';
- contents[99] = '\0';
- }
- public:
- Recipe(int ni = 0, const char* n = "none", const char* c = "none") {
- number_ingredients = ni;
- strncpy(name, n, 99);
- contents = new char[100];
- strncpy(contents, c, 99);
- name[99] = '\0';
- contents[99] = '\0';
- }
- Recipe(const Recipe &r) {
- copyy(r);
- }
- Recipe& operator= (const Recipe &r) {
- if(this == &r) return *this;
- delete[] contents;
- copyy(r);
- return *this;
- }
- friend ostream& operator<< (ostream &out, Recipe &r) {
- return out << r.number_ingredients << "\n" <<
- r.name << "\n" << r.contents << endl;
- }
- bool operator== (const Recipe &r) {
- return (strcmp(name, r.name) == 0);
- }
- Recipe& operator++ () {
- ++number_ingredients;
- return *this;
- }
- Recipe operator++ (int) {
- Recipe temp = *this;
- number_ingredients++;
- return temp;
- }
- int getNum_Ingredients() {
- return number_ingredients;
- }
- void setIngredients(int number_ingredients) {
- this->number_ingredients = number_ingredients;
- }
- void setName(char* name) {
- strcpy(this->name, name);
- }
- void setContents (char* contents) {
- strcpy(this->contents, contents);
- }
- ~Recipe() {
- delete[] contents;
- }
- };
- class RecipesBook {
- private:
- char name[100];
- Recipe *recipes;
- int number_elements;
- public:
- RecipesBook(const char* name = "none", Recipe *recipes = 0, int number_elements = 0) {
- strncpy(this->name, name, 99);
- this->recipes = new Recipe [number_elements + 1];
- this->number_elements = number_elements;
- }
- RecipesBook(const RecipesBook &rb) {
- strncpy(this->name, rb.name, 99);
- this->number_elements = rb.number_elements;
- this->recipes = new Recipe [rb.number_elements + 1];
- for (int i = 0; i < number_elements; i++) {
- recipes[i] = rb.recipes[i];
- }
- }
- RecipesBook& operator= (const RecipesBook &rb) {
- if(this == &rb) return *this;
- strncpy(name, rb.name, 99);
- number_elements = rb.number_elements;
- delete[] recipes;
- recipes = new Recipe[rb.number_elements + 1];
- for (int i = 0; i < number_elements; i++) {
- recipes[i] = rb.recipes[i];
- }
- return *this;
- }
- RecipesBook& operator+= (const Recipe &r) {
- for (int i = 0; i < number_elements; i++) {
- if (recipes[i] == r)
- return *this;
- }
- Recipe *temp = new Recipe[number_elements + 1];
- for (int i = 0; i < number_elements; i++) {
- temp[i] = recipes[i];
- }
- delete[] recipes;
- temp[number_elements++] = r;
- recipes = temp;
- return *this;
- }
- RecipesBook newBook(Recipe &r) {
- RecipesBook rb ;
- strncpy(rb.name, name, 99);
- for (int i = 0; i < number_elements; i++) {
- if (recipes[i].getNum_Ingredients() < r.getNum_Ingredients())
- rb += recipes[i];
- }
- for (int i = 0; i < rb.number_elements; i++)
- rb.recipes[i]++;
- return rb;
- }
- friend ostream& operator<< (ostream &out, RecipesBook &rb) {
- out << rb.name << endl;
- for (int i = 0; i < rb.number_elements; i++)
- out << rb.recipes[i] << endl;
- return out;
- }
- ~RecipesBook() {
- delete[] recipes;
- }
- };
- 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 << "TESTtaaa" << endl;
- cout << b1;
- //cout << "TESTttttttttt" << endl;
- RecipesBook b2 = b1.newBook(rec);
- //cout << "TEST0" << endl;
- cout << b2;
- //cout << "TEST1" << endl;
- // testing copy constructor
- cout << "b2 copy" << endl;
- //cout << "TEST3" << endl;
- RecipesBook rb = b2;
- //cout << "TEST4" << endl;
- last.setName("changed-name");
- //cout << "TEST5" << endl;
- rb += last;
- //cout << "TEST6" << endl;
- cout << rb;
- //cout << "TEST7" << endl;
- cout << "original" << endl;
- //cout << "TEST8" << endl;
- cout << b2;
- //cout << "TEST9" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement