12

 avatar
gorazd
c_cpp
a year ago
5.3 kB
11
Indexable
#include <iostream>
#include <cstring>
#include  <iomanip>
using namespace std;

class Pica {
    char name[15];
    int price;
    char *ingredients;
    int percentOff;
public:
    Pica(const char *_name = "", int _price = 0, const char *_ingredients = nullptr,int _percentOff = 0) {
        strncpy(name, _name,14);
        name[14] = '\0';
        price = _price;
        percentOff = _percentOff;
        if (_ingredients!=nullptr) {
            ingredients = new char[strlen(_ingredients)+1];
            strcpy(ingredients,_ingredients);
        }
        else
            ingredients = nullptr;
    }
    ~Pica() {
        delete[] ingredients;
    }
    Pica(const Pica &p) {
        strcpy(name,p.name);
        price = p.price;
        percentOff = p.percentOff;
        if (p.ingredients != nullptr) {
            ingredients = new char[strlen(p.ingredients)+1];
            strcpy(ingredients, p.ingredients);
        }
        else
            ingredients = nullptr;
    }
    Pica &operator=(const Pica &p) {
        if (this != &p) {
            delete[] ingredients;
            strcpy(name,p.name);
            price = p.price;
            percentOff = p.percentOff;
            if (p.ingredients != nullptr) {
                ingredients = new char[strlen(p.ingredients)+1];
                strcpy(ingredients, p.ingredients);
            }
            else
                ingredients = nullptr;
        }
        return *this;
    }
    void pecati() {
        cout<<name<<" - "<<ingredients<<", "<<price;
    }
    bool istiSe(Pica &p) {
        return (strcmp(ingredients, p.ingredients) == 0);
    }
    int getPercentOff(){return percentOff;}
    char *getName(){return name;}
    char *getIngredients(){return ingredients;}
    int getPrice(){return price;}
};

class Picerija {
    char name[15];
    Pica *pici;
    int numberOfPizzas;
public:
    Picerija(const char *_name = "", Pica *_pici = nullptr, int _numberOfPizzas = 0) {
        strncpy(name,_name,14);
        name[14] = '\0';
        numberOfPizzas = _numberOfPizzas;
        if (_pici!=nullptr and numberOfPizzas>0) {
            pici = new Pica[numberOfPizzas];
            for (int i = 0; i<numberOfPizzas; i++)
                pici[i] = _pici[i];
        }
        else
            pici = nullptr;
    }
    ~Picerija() {
        delete[] pici;
    }
    Picerija(const Picerija &p) {
        strcpy(name,p.name);
        numberOfPizzas = p.numberOfPizzas;
        if (p.pici!=nullptr and p.numberOfPizzas > 0) {
            pici = new Pica[numberOfPizzas];
            for (int i = 0; i<numberOfPizzas; i++)
                pici[i] = p.pici[i];

        }
        else
            pici = nullptr;
    }
    Picerija &operator=(const Picerija &p) {
        delete[] pici;
        strcpy(name,p.name);
        numberOfPizzas = p.numberOfPizzas;
        if (p.pici!=nullptr and p.numberOfPizzas > 0) {
            pici = new Pica[numberOfPizzas];
            for (int i = 0; i<numberOfPizzas; i++)
                pici[i] = p.pici[i];

        }
        else
            pici = nullptr;
    }
    Picerija &operator+=(Pica &p) {
        for (int i = 0; i<numberOfPizzas; i++) {
            if (pici[i].istiSe(p))
                return *this;
        }
        Pica *temp = new Pica[numberOfPizzas+1];
        for (int i = 0; i<numberOfPizzas; i++)
            temp[i] = pici[i];
        temp[numberOfPizzas] = p;
        delete[] pici;
        pici = temp;
        numberOfPizzas++;
        return *this;
    }


    void dodadi(Pica &p) {
        for (int i = 0; i<numberOfPizzas; i++) {
            if (pici[i].istiSe(p))
                return;
        }
        Pica *temp = new Pica[numberOfPizzas+1];
        for (int i = 0; i<numberOfPizzas; i++)
            temp[i] = pici[i];
        temp[numberOfPizzas] = p;
        delete[] pici;
        pici = temp;
        numberOfPizzas++;
    }
    void piciNaPromocija() {
        for (int i = 0; i<numberOfPizzas; i++) {
            if (pici[i].getPercentOff()>0) {
                int newPrice = pici[i].getPrice() - (pici[i].getPercentOff()*pici[i].getPrice()/100);
                pici[i].pecati();
                cout<<" "<<newPrice;
                cout<<endl;

            }
        }
    }
    char *getName(){return name;}
    void setName(const char *_name) {
        strcpy(name,_name);
    }
};

int main() {
    int n;
    char ime[15];
    cin >> ime;
    cin >> n;

    Picerija p1(ime);
    for (int i = 0; i < n; i++){
        char imp[100];
        cin.get();
        cin.getline(imp, 100);
        int cena;
        cin >> cena;
        char sostojki[100];
        cin.get();
        cin.getline(sostojki, 100);
        int popust;
        cin >> popust;
        Pica p(imp, cena, sostojki, popust);
        p1+=p;
    }

    Picerija p2 = p1;
    cin >> ime;
    p2.setName(ime);
    char imp[100];
    cin.get();
    cin.getline(imp, 100);
    int cena;
    cin >> cena;
    char sostojki[100];
    cin.get();
    cin.getline(sostojki, 100);
    int popust;
    cin >> popust;
    Pica p(imp, cena, sostojki, popust);
    p2+=p;

    cout << p1.getName() << endl;
    cout << "Pici na promocija:" << endl;
    p1.piciNaPromocija();

    cout << p2.getName() << endl;
    cout << "Pici na promocija:" << endl;
    p2.piciNaPromocija();

    return 0;
}
Editor is loading...
Leave a Comment