13

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

class Zichara {
    char *location;
    int price;
public:
    Zichara(const char *_location = "", int _price = 0) {
        price = _price;
        if (_location!=nullptr) {
            location = new char[strlen(_location)+1];
            strcpy(location,_location);
        }
        else
            location = nullptr;
    }
    ~Zichara() {
        delete[] location;
    }
    Zichara(const Zichara &z) {
        location = nullptr;
        price = z.price;
        if (z.location!=nullptr) {
            location = new char[strlen(z.location)+1];
            strcpy(location,z.location);
        }
    }
    Zichara &operator=(const Zichara &z) {
        if (this!=&z){
            delete[] location;
            price = z.price;
            if (z.location!=nullptr) {
                location = new char[strlen(z.location)+1];
                strcpy(location,z.location);
            }
        }
        return *this;
    }
    int getPrice(){return price;}
};
class PlaninarskiDom {
    char name[15];
    int price[2];
    char _class;
    bool hasLift;
    Zichara *z;
public:
    PlaninarskiDom() {
        name[0] ='\0';
        for (int i = 0; i<2; i++) price[i] = 0;
        _class = '\0';
        hasLift = false;
        z = nullptr;
    }
    PlaninarskiDom(const char *_name, int *price, char __class ) {
        strcpy(name,_name);
        for (int i = 0; i<2; i++) this->price[i] = price[i];
        _class = __class;
        hasLift = false;
        z = nullptr;
    }
    ~PlaninarskiDom(){delete z;}

    PlaninarskiDom(const PlaninarskiDom &p) {
        strcpy(name,p.name);
        for (int i = 0; i<2; i++) price[i] = p.price[i];
        _class = p._class;
        hasLift = p.hasLift;
        if (p.z)
            z = new Zichara(*p.z);
        else
            z = nullptr;
    }
    PlaninarskiDom &operator=(const PlaninarskiDom &p) {
        if (this!=&p){
            strcpy(name,p.name);
            for (int i = 0; i<2; i++) price[i] = p.price[i];
            _class = p._class;
            hasLift = p.hasLift;
            if (p.z)
                z = new Zichara(*p.z);
            else
                z = nullptr;
        }
        return *this;
    }
    PlaninarskiDom &operator--() {

        if (_class >= 'E')
            _class = 'F';
        else
            _class += 1;

        return *this;
    }
    friend ostream &operator <<(ostream &os, const PlaninarskiDom &p);

    bool operator<=(char _class) {
        return this->_class>=_class;
    }

    void setZichara(const Zichara &z) {
        hasLift = true;
        delete this->z;
        this->z = new Zichara(z);
    }

    void presmetajDnevenPrestoj(int day, int month, int &price_z) {
        if (day < 1 || day > 31 || month < 1 || month > 12) {
            throw -1; // Throw an int error code
        }

        // Letna sezona
        if (month >= 4 && month <= 8)
            price_z = price[0];
        else
            price_z = price[1];

        if (hasLift && z) {
            // Ensure z is not nullptr
            price_z += (*z).getPrice();
        }
    }

};
ostream &operator <<(ostream &os, const PlaninarskiDom &p) {
    os << p.name << " klasa:"<<p._class;
    if (p.hasLift) cout<<" so Zichara"<<endl;
    else cout<<endl;
    return os;
}
int main() {
    PlaninarskiDom p; //креирање на нов објект од класата планинарски дом

    //во следниот дел се вчитуваат информации за планинарскиот дом
    char imePlaninarskiDom[15],mestoZichara[30],klasa;
    int ceni[12];
    int dnevnakartaZichara;
    bool daliZichara;
    cin>>imePlaninarskiDom;
    for (int i=0;i<2;i++) cin>>ceni[i];
    cin>>klasa;
    cin>>daliZichara;

    //во следниот дел се внесуваат информации и за жичарата ако постои
    if (daliZichara) {
        cin>>mestoZichara>>dnevnakartaZichara;
        PlaninarskiDom pom(imePlaninarskiDom,ceni,klasa);
        Zichara r(mestoZichara,dnevnakartaZichara);
        pom.setZichara(r);
        p=pom;
    }
    else{
        PlaninarskiDom *pok=new PlaninarskiDom(imePlaninarskiDom,ceni,klasa);
        p=*pok;
    }

    //се намалува класата на планинарскиот дом за 2
    --p;
    --p;

    int cena;
    int den,mesec;
    cin>>den>>mesec;
    try{
        p.presmetajDnevenPrestoj(den,mesec,cena); //тука се користи функцијата presmetajDnevenPrestoj
        cout<<"Informacii za PlaninarskiDomot:"<<endl;
        cout<<p;
        if (p<='D')
            cout<<"Planinarskiot dom za koj se vneseni informaciite ima klasa poniska ili ista so D\n";

        cout<<"Cenata za "<<den<<"."<<mesec<<" e "<<cena; //се печати цената за дадениот ден и месец
    }
    catch (int){
        cout<<"Mesecot ili denot e greshno vnesen!";
    }
}
Editor is loading...
Leave a Comment