Advertisement
Guest User

riki zamara

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