Advertisement
add1ctus

2. Recipes

Mar 29th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Recipe
  7. {
  8.     int number;
  9.     char name[100];
  10.     char* contents;
  11. public:
  12.     Recipe()                                //Default constructor
  13.     {
  14.         contents=new char[0];
  15.     }
  16.     Recipe(int n, char* na, char* c)        //Constructor with parameters set
  17.     {
  18.         number = n;
  19.         contents = new char [strlen(c)+1];  //Allocating enough memory to copy the contents, +1 in order to make place for '\0'
  20.         strcpy(name,na);
  21.         strcpy(contents,c);
  22.     }
  23.  
  24.     Recipe &operator++()                    //prefix operator. Incrementing and returning
  25.     {
  26.         ++number;
  27.         return *this;
  28.     }
  29.  
  30.     Recipe operator++(int)                  //postfix operator. Returning non-incremented value
  31.     {
  32.         Recipe temp(number,name,contents);  //Remembering the current value
  33.         ++(*this);                          //using prefix operator on the current value
  34.         return temp;                        //Returning the value we remembered
  35.     }
  36.  
  37.     friend ostream &operator<<(ostream &output, const Recipe &rhs)  //Output operator
  38.     {
  39.         output<<rhs.number<<endl<<rhs.name<<endl<<rhs.contents<<endl;
  40.         return output;
  41.     }
  42.  
  43.     Recipe &operator=(const Recipe &rhs)    //Operator=, you just need to copy everything from rhs to this.
  44.     {
  45.         this->number=rhs.number;
  46.         delete[] contents;
  47.         this->contents = new char[strlen(rhs.contents)+1];
  48.         strcpy(this->name,rhs.name);
  49.         strcpy(this->contents,rhs.contents);
  50.         return *this;
  51.     }
  52.  
  53.     bool operator==(const Recipe &rhs)  //We need to compare if two recipes are equal
  54.     {
  55.         return !strcmp(this->name,rhs.name);    //strcmp will be 0 if strings are equal. Since we want 1 if strings are equal, we negate it
  56.     }
  57.  
  58.     bool operator>(const Recipe &rhs)   //Comparing two recipes by ingredients (needed for newBook)
  59.     {
  60.         return this->number>rhs.number;
  61.     }
  62.  
  63.     void set_num_ing(int n)
  64.     {
  65.         number=n;
  66.     }
  67.  
  68.     void set_name(char *n)
  69.     {
  70.         strcpy(name,n);
  71.     }
  72.  
  73.     void set_contents(char *c)
  74.     {
  75.         delete[] contents;
  76.         contents = new char[strlen(c)+1];
  77.         strcpy(contents,c);
  78.     }
  79.  
  80.     ~Recipe()               //Destructor
  81.     {
  82.         delete[] contents;
  83.     }
  84. };
  85.  
  86. class RecipesBook
  87. {
  88.     char name[100];
  89.     Recipe* recipes;
  90.     int number;
  91. public:
  92.     RecipesBook()              //Default constructor
  93.     {
  94.         recipes=new Recipe[0];
  95.     }
  96.     RecipesBook(char *n)    //Constructor that only takes name as argument
  97.     {
  98.         recipes=new Recipe[0];
  99.         strcpy(name,n);
  100.         number=0;           //We will use this later to know if we should call delete or not
  101.     }
  102.  
  103.     RecipesBook &operator+=(const Recipe &rhs)  //Operator used to add a recipe to the book if it's not present
  104.     {
  105.         bool isPresent=false;                   //This will be true if the recipe is already present in the book
  106.         for(int i=0;i<number;i++)               //For all recipes i from 0 to number in the book
  107.             if(recipes[i]==rhs)                 //If there's an equal recipe, then isPresent is true
  108.                 isPresent=true;
  109.  
  110.         if(isPresent)                           //If the recipe is already in, we don't have to add anything and can return
  111.             return *this;
  112.  
  113.         Recipe* temp = new Recipe[number];      //A temporary array of recipes
  114.         for(int i=0;i<number;i++)               //We copy all recipes from our current array to the temp array
  115.             temp[i]=recipes[i];
  116.  
  117.         delete[] recipes;
  118.  
  119.         recipes = new Recipe[number+1];         //Making the array we deleted again, but this time it can hold 1 more recipe (so we can add a new one)
  120.  
  121.         for(int i=0;i<number;i++)               //Copying all the recipes from temp back into our book
  122.             recipes[i]=temp[i];
  123.  
  124.         recipes[number]=rhs;                    //Adding the last one at the end of the book
  125.         number++;                               //We increase the recipe counter
  126.  
  127.         return *this;                           //Return the result
  128.     }
  129.  
  130.     friend ostream &operator<<(ostream &output, RecipesBook &rhs)   //Operator to print the book
  131.     {
  132.         output<<rhs.name<<endl;                         //First we print the name of the book
  133.         for(int i=0;i<rhs.number;i++)                   //Then we print all recipes
  134.             output<<rhs.recipes[i]<<endl;
  135.         return output;
  136.     }
  137.  
  138.     RecipesBook novaBrosura(Recipe &r)                      //The newBook function
  139.     {
  140.         RecipesBook result(this->name);                 //We create a new book with the name of the original one
  141.         for(int i=0;i<this->number;i++)                 //We check all recipes in the original
  142.             if(r>this->recipes[i])                      //If recipe r has more ingredients than it recipe i
  143.                 result+=recipes[i];                     //Add recipe i to the new book
  144.         for(int i=0;i<result.number;i++)                //We also need to increment all recipes by 1
  145.             result.recipes[i]++;
  146.         return result;
  147.     }
  148.  
  149.     RecipesBook(const RecipesBook &rhs)
  150.     {
  151.         this->number=rhs.number;                        //We copy the number
  152.         this->recipes=new Recipe[this->number];             //Allocate memory for recipes
  153.         strcpy(this->name,rhs.name);                    //Copy the name
  154.         for(int i=0;i<rhs.number;i++)                   //We copy all recipes one by one
  155.             this->recipes[i]=rhs.recipes[i];
  156.     }
  157.  
  158.     ~RecipesBook()      //Destructor
  159.     {
  160.         delete[] recipes;
  161.     }
  162. };
  163.  
  164. int main()
  165. {
  166.     Recipe rec;
  167.     int n;
  168.     char name[100], contents[200];
  169.     int num_ing;
  170.     cin >> name >> n;
  171.     RecipesBook b1(name);
  172.     Recipe last;
  173.     for(int i = 0; i < n; i++){
  174.         cin >> num_ing >> name >> contents;
  175.         Recipe r(num_ing, name, contents);
  176.         b1 += r;
  177.         last = r;
  178.     }
  179.     cout << b1;
  180.     cin >> num_ing >> name >> contents;
  181.     rec.set_num_ing(num_ing);
  182.     rec.set_name(name);
  183.     rec.set_contents(contents);
  184.     b1+=rec;
  185.     cout << b1;
  186.     RecipesBook b2 = b1.novaBrosura(rec);
  187.     cout << b2;
  188.     // testing copy constructor
  189.     cout << "b2 copy" << endl;
  190.     RecipesBook rb = b2;
  191.     last.set_name("changed-name");
  192.     rb += last;
  193.     cout << rb;
  194.     cout << "original" << endl;
  195.     cout << b2;
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement