Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Recipe
- {
- int number;
- char name[100];
- char* contents;
- public:
- Recipe() //Default constructor
- {
- contents=new char[0];
- }
- Recipe(int n, char* na, char* c) //Constructor with parameters set
- {
- number = n;
- contents = new char [strlen(c)+1]; //Allocating enough memory to copy the contents, +1 in order to make place for '\0'
- strcpy(name,na);
- strcpy(contents,c);
- }
- Recipe &operator++() //prefix operator. Incrementing and returning
- {
- ++number;
- return *this;
- }
- Recipe operator++(int) //postfix operator. Returning non-incremented value
- {
- Recipe temp(number,name,contents); //Remembering the current value
- ++(*this); //using prefix operator on the current value
- return temp; //Returning the value we remembered
- }
- friend ostream &operator<<(ostream &output, const Recipe &rhs) //Output operator
- {
- output<<rhs.number<<endl<<rhs.name<<endl<<rhs.contents<<endl;
- return output;
- }
- Recipe &operator=(const Recipe &rhs) //Operator=, you just need to copy everything from rhs to this.
- {
- this->number=rhs.number;
- delete[] contents;
- this->contents = new char[strlen(rhs.contents)+1];
- strcpy(this->name,rhs.name);
- strcpy(this->contents,rhs.contents);
- return *this;
- }
- bool operator==(const Recipe &rhs) //We need to compare if two recipes are equal
- {
- 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
- }
- bool operator>(const Recipe &rhs) //Comparing two recipes by ingredients (needed for newBook)
- {
- return this->number>rhs.number;
- }
- void set_num_ing(int n)
- {
- number=n;
- }
- void set_name(char *n)
- {
- strcpy(name,n);
- }
- void set_contents(char *c)
- {
- delete[] contents;
- contents = new char[strlen(c)+1];
- strcpy(contents,c);
- }
- ~Recipe() //Destructor
- {
- delete[] contents;
- }
- };
- class RecipesBook
- {
- char name[100];
- Recipe* recipes;
- int number;
- public:
- RecipesBook() //Default constructor
- {
- recipes=new Recipe[0];
- }
- RecipesBook(char *n) //Constructor that only takes name as argument
- {
- recipes=new Recipe[0];
- strcpy(name,n);
- number=0; //We will use this later to know if we should call delete or not
- }
- RecipesBook &operator+=(const Recipe &rhs) //Operator used to add a recipe to the book if it's not present
- {
- bool isPresent=false; //This will be true if the recipe is already present in the book
- for(int i=0;i<number;i++) //For all recipes i from 0 to number in the book
- if(recipes[i]==rhs) //If there's an equal recipe, then isPresent is true
- isPresent=true;
- if(isPresent) //If the recipe is already in, we don't have to add anything and can return
- return *this;
- Recipe* temp = new Recipe[number]; //A temporary array of recipes
- for(int i=0;i<number;i++) //We copy all recipes from our current array to the temp array
- temp[i]=recipes[i];
- delete[] recipes;
- 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)
- for(int i=0;i<number;i++) //Copying all the recipes from temp back into our book
- recipes[i]=temp[i];
- recipes[number]=rhs; //Adding the last one at the end of the book
- number++; //We increase the recipe counter
- return *this; //Return the result
- }
- friend ostream &operator<<(ostream &output, RecipesBook &rhs) //Operator to print the book
- {
- output<<rhs.name<<endl; //First we print the name of the book
- for(int i=0;i<rhs.number;i++) //Then we print all recipes
- output<<rhs.recipes[i]<<endl;
- return output;
- }
- RecipesBook novaBrosura(Recipe &r) //The newBook function
- {
- RecipesBook result(this->name); //We create a new book with the name of the original one
- for(int i=0;i<this->number;i++) //We check all recipes in the original
- if(r>this->recipes[i]) //If recipe r has more ingredients than it recipe i
- result+=recipes[i]; //Add recipe i to the new book
- for(int i=0;i<result.number;i++) //We also need to increment all recipes by 1
- result.recipes[i]++;
- return result;
- }
- RecipesBook(const RecipesBook &rhs)
- {
- this->number=rhs.number; //We copy the number
- this->recipes=new Recipe[this->number]; //Allocate memory for recipes
- strcpy(this->name,rhs.name); //Copy the name
- for(int i=0;i<rhs.number;i++) //We copy all recipes one by one
- this->recipes[i]=rhs.recipes[i];
- }
- ~RecipesBook() //Destructor
- {
- 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.set_num_ing(num_ing);
- rec.set_name(name);
- rec.set_contents(contents);
- b1+=rec;
- cout << b1;
- RecipesBook b2 = b1.novaBrosura(rec);
- cout << b2;
- // testing copy constructor
- cout << "b2 copy" << endl;
- RecipesBook rb = b2;
- last.set_name("changed-name");
- rb += last;
- cout << rb;
- cout << "original" << endl;
- cout << b2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement