Advertisement
Filip_Markoski

Recipes 17-March Question

Mar 17th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Recipe {
  7. private:
  8.     int numIngredients;
  9.     char name[100];
  10.     char *contents;
  11. public:
  12.     Recipe() {
  13.         numIngredients = 0;
  14.         strcpy(name, "");
  15.         contents = new char[0];
  16.     }
  17.  
  18.     Recipe(int n, char s[], char *c) {
  19.         numIngredients = n;
  20.         strcpy(name, s);
  21.         contents = new char[strlen(c) + 1];
  22.         strcpy(contents, c);
  23.     }
  24.  
  25.     Recipe &operator++() {
  26.         numIngredients++;
  27.         return *this;
  28.     }
  29.  
  30.     friend ostream &operator<<(ostream &out, const Recipe &r) {
  31.         out << r.numIngredients << endl << r.name << endl << r.contents << endl;
  32.         return out;
  33.     }
  34.  
  35.     friend inline bool operator==(const Recipe &a, const Recipe &b) {
  36.         return (strcmp(a.name, b.name) == 0);
  37.     }
  38.  
  39.     const char *getName() {
  40.         return name;
  41.     }
  42.  
  43.     const int getIngredients() {
  44.         return numIngredients;
  45.     }
  46.  
  47.     const char *getContents() {
  48.         return contents;
  49.     }
  50.  
  51.     void setIngredients(const int n) {
  52.         numIngredients = n;
  53.     }
  54.  
  55.     void setName(const char n[]) {
  56.         strcpy(name, n);
  57.     }
  58.  
  59.     void setContents(const char c[]) {
  60.         contents = new char[strlen(c) + 1];
  61.         strcpy(contents, c);
  62.     }
  63.  
  64.     Recipe &operator=(const Recipe &from) {
  65.         if (this != &from) {
  66.             numIngredients = from.numIngredients;
  67.             strcpy(name, from.name);
  68.             strncpy(contents, from.contents, strlen(from.contents));
  69.             contents[strlen(from.contents) + 1] = '\0';
  70.         }
  71.         return *this;
  72.     }
  73.  
  74.  
  75.     char operator[](int i) { return contents[i]; }
  76.     const char operator[](int i) const { return contents[i]; }
  77.  
  78. };
  79.  
  80.  
  81. class RecipesBook {
  82. private:
  83.     char name[100];
  84.     Recipe *recipes;
  85.     int len;
  86. public:
  87.     RecipesBook() {
  88.         len = 0;
  89.         strcpy(name, "");
  90.         recipes = new Recipe[len];
  91.     }
  92.  
  93.     RecipesBook(const char n[]) {
  94.         len = 0;
  95.         strcpy(name, n);
  96.         recipes = new Recipe[len];
  97.     }
  98.  
  99.     Recipe &operator[](int i) { return recipes[i]; }
  100.     const Recipe &operator[](int i) const { return recipes[i]; }
  101.  
  102.     void recipeCopy(Recipe dest[], const Recipe source[], const int len) {
  103.         for (int i = 0; i < len; ++i) {
  104.             dest[i] = source[i];
  105.         }
  106.     }
  107.  
  108.     friend ostream &operator<<(ostream &out, const RecipesBook &b) {
  109.         out << b.name << endl;
  110.         for (int i = 0; i < b.len; ++i) {
  111.             out << b.recipes[i] << endl;
  112.         }
  113.         return out;
  114.     }
  115.  
  116.     RecipesBook newBook(Recipe &r) {
  117.         RecipesBook book(name);
  118.         for (int i = 0; i < len; ++i) {
  119.             if (recipes[i].getIngredients() < r.getIngredients()) {
  120.                 book.recipes[book.len] = recipes[i];
  121.                 book.recipes[book.len].setIngredients(recipes[i].getIngredients() + 1);
  122.                 book.len++;
  123.             }
  124.         }
  125.         return book;
  126.     }
  127.  
  128.     RecipesBook &operator+=(const Recipe &r) {
  129.         bool found = false;
  130.         for (int i = 0; i < len && !found; ++i) {
  131.             found = (recipes[i] == r);
  132.         }
  133.         if (!found) {
  134.             Recipe *temp = new Recipe[len + 1];
  135.             recipeCopy(temp, recipes, len);
  136.             temp[len++] = r;
  137.             delete[] recipes;
  138.             recipes = temp;
  139.         }
  140.         return *this;
  141.     }
  142.  
  143.     RecipesBook &operator=(const RecipesBook &r) {
  144.         if (this != &r) {
  145.             len = r.len;
  146.             strcpy(name, r.name);
  147.             recipes = new Recipe[r.len];
  148.             recipeCopy(recipes, r.recipes, r.len);
  149.         }
  150.         return *this;
  151.     }
  152.  
  153.     RecipesBook(const RecipesBook &r) {
  154.         len = r.len;
  155.         strcpy(name, r.name);
  156.         recipes = new Recipe[r.len];
  157.         recipeCopy(recipes, r.recipes, r.len);
  158.     }
  159. };
  160.  
  161.  
  162. int main() {
  163.     Recipe rec;
  164.     int n;
  165.     char name[100], contents[200];
  166.     int num_ing;
  167.     cin >> name >> n;
  168.     RecipesBook b1(name);
  169.     Recipe last;
  170.     for (int i = 0; i < n; i++) {
  171.         cin >> num_ing >> name >> contents;
  172.         Recipe r(num_ing, name, contents);
  173.         b1 += r;
  174.         last = r;
  175.     }
  176.     cout << b1;
  177.     cin >> num_ing >> name >> contents;
  178.     rec.setIngredients(num_ing);
  179.     rec.setName(name);
  180.     rec.setContents(contents);
  181.     b1 += rec;
  182.     cout << b1;
  183.     RecipesBook b2 = b1.newBook(rec);
  184.     cout << b2;
  185.     // testing copy constructor
  186.     cout << "b2 copy" << endl;
  187.     RecipesBook rb = b2;
  188.     last.setName("changed-name");
  189.     rb += last;
  190.     cout << rb;
  191.     cout << "original" << endl;
  192.     cout << b2;
  193.     return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement