Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Сладолед Problem 5 (1 / 30)
- #include <iostream>
- #include <cstring>
- using namespace std;
- class IceCream{
- private:
- char* name;
- char ingr[100];
- float price;
- int discount;
- void copy(const IceCream &ic) {
- name = new char[strlen(ic.name) + 1];
- strcpy(name, ic.name);
- strncpy(this->ingr, ic.ingr, 99);
- this->ingr[99] = '\0';
- this->price = ic.price;
- this->discount = ic.discount;
- }
- public:
- IceCream(char* name="", char* ingr="", float price=0){
- this->name = new char[strlen(name)+1];
- strcpy(this->name, name);
- strncpy(this->ingr, ingr, 99);
- this->ingr[99] = '\0';
- this->price = price;
- this->discount = 0;
- }
- IceCream(const IceCream &ic) {
- copy(ic);
- }
- void setDiscount(int discount){
- if(discount >=0&&discount <=100)
- this->discount = discount;
- }
- void setName(const char* name){
- delete[] this->name;
- this->name = new char[strlen(name) + 1];
- strcpy(this->name, name);
- }
- void setPrice(float price){this->price = price;}
- IceCream& operator=(const IceCream &ic) {
- if (this == &ic) return *this;
- delete [] name;
- copy(ic);
- return *this;
- }
- friend ostream& operator << (ostream& outStream, const IceCream& iceCream){
- outStream << iceCream.name << ": ";
- outStream << iceCream.ingr << " ";
- outStream << iceCream.price << " ";
- if(iceCream.discount != 0)
- outStream << "(" << iceCream.price * (100 - iceCream.discount) / 100 << ")";
- return outStream;
- }
- IceCream& operator ++ (){
- setDiscount(this->discount + 5);
- return *this;
- }
- IceCream operator + (char* other){
- char* newName = new char[strlen(this->name)+strlen(other) + 4];
- strcat(newName, this->name);
- strcat(newName, " + ");
- strcat(newName, other);
- IceCream newIceCream(newName, this->ingr, this->price + 10);
- newIceCream.setDiscount(this->discount);
- return newIceCream;
- }
- ~IceCream(){delete[] name;}
- };
- class IceCreamShop{
- private:
- char name[50];
- IceCream* IceCreams;
- int numIceCreams;
- void copy(const IceCreamShop &ics){
- strcpy(this->name, ics.name);
- this->numIceCreams = ics.numIceCreams;
- IceCreams = new IceCream[numIceCreams];
- for(int i = 0; i < numIceCreams; i++){
- this->IceCreams[i] = ics.IceCreams[i];
- }
- }
- public:
- IceCreamShop(char* name){
- strcpy(this->name, name);
- IceCreams = NULL;
- numIceCreams = 0;
- }
- IceCreamShop(const IceCreamShop& ics){copy(ics);}
- IceCreamShop& operator=(const IceCreamShop &ics){
- if(this == &ics) return *this;
- delete[] IceCreams;
- copy(ics);
- return *this;
- }
- IceCreamShop& operator += (const IceCream& other){
- IceCream *tmp = IceCreams;
- IceCreams = new IceCream[this->numIceCreams + 1];
- for(int i = 0; i < this->numIceCreams; i++){
- IceCreams[i] = tmp[i];
- }
- IceCreams[this->numIceCreams] = other;
- this->numIceCreams++;
- delete[] tmp;
- return *this;
- }
- friend ostream& operator << (ostream& outStream, const IceCreamShop& shop){
- outStream << shop.name << endl;
- for(int i = 0; i < shop.numIceCreams; i++){
- outStream << shop.IceCreams[i] << endl;;
- }
- return outStream;
- }
- ~IceCreamShop(){delete[] IceCreams;}
- };
- // vashiot kod ovde
- // zabraneto e menuvanje na main funkcijata
- int main() {
- char name[100];
- char ingr[100];
- float price;
- int discount;
- int testCase;
- cin >> testCase;
- cin.get();
- if(testCase == 1) {
- cout << "====== TESTING IceCream CLASS ======" << endl;
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- cin >> discount;
- cout << "CONSTRUCTOR" << endl;
- IceCream ic1(name, ingr, price);
- ic1.setDiscount(discount);
- cin.get();
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- cin >> discount;
- IceCream ic2(name, ingr, price);
- ic2.setDiscount(discount);
- cout << "OPERATOR <<" << endl;
- cout << ic1 << endl;
- cout << ic2 << endl;
- cout << "OPERATOR ++" << endl;
- ++ic1;
- cout << ic1 << endl;
- cout << "OPERATOR +" << endl;
- IceCream ic3 = ic2 + "chocolate";
- cout << ic3 << endl;
- } else if(testCase == 2) {
- cout << "====== TESTING IceCream CONSTRUCTORS ======" << endl;
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- cout << "CONSTRUCTOR" << endl;
- IceCream ic1(name, ingr, price);
- cout << ic1 << endl;
- cout << "COPY CONSTRUCTOR" << endl;
- IceCream ic2(ic1);
- cin.get();
- cin.getline(name,100);
- ic2.setName(name);
- cout << ic1 << endl;
- cout << ic2 << endl;
- cout << "OPERATOR =" << endl;
- ic1 = ic2;
- cin.getline(name,100);
- ic2.setName(name);
- cout << ic1 << endl;
- cout << ic2 << endl;
- cin >> discount;
- ic1.setDiscount(discount);
- } else if(testCase == 3) {
- cout << "====== TESTING IceCreamShop ======" << endl;
- char icsName[50];
- cin.getline(icsName,100);
- cout << "CONSTRUCTOR" << endl;
- IceCreamShop ics(icsName);
- int n;
- cin >> n;
- cout << "OPERATOR +=" << endl;
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- IceCream ic(name, ingr, price);
- ics += ic;
- }
- cout << ics;
- } else if(testCase == 4) {
- cout << "====== TESTING IceCreamShop CONSTRUCTORS ======" << endl;
- char icsName[50];
- cin.getline(icsName,100);
- IceCreamShop ics(icsName);
- int n;
- cin >> n;
- for(int i = 0; i < n; ++i) {
- cin.get();
- cin.getline(name,100);
- cin.getline(ingr,100);
- cin >> price;
- IceCream ic(name, ingr, price);
- ics += ic;
- }
- IceCream x("FINKI fruits", "strawberry ice cream, raspberry ice cream, blueberry ice cream", 60);
- IceCreamShop icp = ics;
- ics+=x;
- cout << ics << endl;
- cout << icp << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment