Advertisement
Filip_Markoski

Recipes (Working) - 29 March

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