Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <memory>
- #include <stdexcept>
- #include <cmath>
- #include <fstream>
- class GrafickiElement
- {
- public:
- enum Boje { Crvena, Zelena, Zuta, Plava, Ljubicasta, Crna, Bijela };
- GrafickiElement(Boje boja) : boja(boja) {}
- Boje DajBoju() const { return boja; }
- virtual void Ispisi() const { std::cout << boja; }
- virtual ~GrafickiElement() {}
- virtual double DajPovrsinu() const = 0;
- virtual GrafickiElement *DajKopiju() const = 0;
- private:
- Boje boja;
- };
- class Tacka : public GrafickiElement
- {
- double x, y;
- public:
- Tacka(GrafickiElement::Boje boja, double x, double y) : GrafickiElement(boja), x(x), y(y) {}
- void Ispisi() const { std::cout << "{" << x << "," << y << "}"; }
- double DajX() const { return x; }
- double DajY() const { return y; }
- friend Tacka operator *(const Tacka &t, double k);
- friend Tacka operator *(double k, const Tacka &t) { return t*k; }
- double DajPovrsinu() const override { return 0; }
- Tacka *DajKopiju() const override { return new Tacka(*this); }
- };
- Tacka operator *(const Tacka &t, double k)
- {
- Tacka rez(t);
- rez.x *= k; rez.y *= k;
- return rez;
- }
- class Krug : public GrafickiElement
- {
- double x, y, r;
- public:
- Krug(GrafickiElement::Boje boja, double x, double y, double r) : GrafickiElement(boja), x(x), y(y), r(r) {}
- Krug(GrafickiElement::Boje boja, const Tacka &t, double r) : GrafickiElement(boja), x(t.DajX()), y(t.DajY()), r(r) {}
- void Ispisi() const { std::cout << "{{" << x << "," << y << "}," << r << "}"; }
- double DajPovrsinu() const override { return r*r * 4 * atan(1); }
- Krug *DajKopiju() const override { return new Krug(*this); }
- };
- class Poligon : public GrafickiElement
- {
- int broj_tjemena;
- double *x, *y;
- public:
- Poligon(GrafickiElement::Boje boja, int broj_tjemena);
- ~Poligon() { delete[] x; delete[] y; }
- Poligon(const Poligon &p);
- Poligon(Poligon &&p);
- Poligon &operator =(const Poligon &p);
- Poligon &operator =(Poligon &&p);
- void PostaviTjeme(int redni_broj, double x, double y) { Poligon::x[redni_broj - 1] = x; Poligon::y[redni_broj - 1] = y; }
- void Ispisi() const { std::cout << "{"; for (int i = 0; i < broj_tjemena; i++) { std::cout << "{" << x[i] << "," << y[i] << "}"; if (i != broj_tjemena - 1) std::cout << ",";}std::cout << "}"; }
- void DodajTjeme(int redni_broj, double x, double y);
- void DodajTjeme(int redni_broj, const Tacka &t);
- double DajPovrsinu() const override;
- Tacka operator [](int tjeme) const { return Tacka(DajBoju(), x[tjeme - 1], y[tjeme - 1]); }
- Poligon *DajKopiju() const override { return new Poligon(*this); }
- };
- double Poligon::DajPovrsinu() const
- {
- double p(0);
- for (int i(3); i <= broj_tjemena; i++)
- p += x[1] * (y[i - 1] - y[i]) + x[i - 1] * (y[i] - y[1]) + x[i] * (y[1] - y[i - 1]);
- return p;
- }
- void Poligon::DodajTjeme(int redni_broj, const Tacka &t)
- {
- if (redni_broj > broj_tjemena || redni_broj < 1) throw std::range_error("Redni broj tjemena nije u legalnom opsegu");
- /*Tijelo sad njihovo*/
- }
- void Poligon::DodajTjeme(int redni_broj, double x, double y)
- {
- if (redni_broj > broj_tjemena || redni_broj < 1) throw std::range_error("Redni broj tjemena nije u legalnom opsegu");
- /*Tijelo sad njihovo koje nije u prilogu postavljeno*/
- }
- Poligon::Poligon(GrafickiElement::Boje boja, int broj_tjemena) : GrafickiElement(boja), broj_tjemena(broj_tjemena), x(nullptr), y(nullptr)
- {
- try
- {
- x = new double[broj_tjemena];
- y = new double[broj_tjemena];
- }
- catch (std::bad_alloc)
- {
- delete[] x;
- delete[] y;
- throw;
- }
- }
- Poligon &Poligon::operator =(Poligon &&p)
- {
- if (&p != this)
- {
- delete[] x; delete[] y;
- broj_tjemena = p.broj_tjemena;
- x = std::move(p.x); y = std::move(p.y);
- p.broj_tjemena = 0;
- p.x = nullptr; p.y = nullptr;
- }
- return *this;
- }
- Poligon &Poligon::operator =(const Poligon &p)
- {
- if (&p != this)
- {
- delete[] x; delete[] y;
- broj_tjemena = p.broj_tjemena;
- x = new double[p.broj_tjemena];
- y = new double[p.broj_tjemena];
- for (int i(0); i < p.broj_tjemena; i++)
- {
- x[i] = p.x[i];
- y[i] = p.x[i];
- }
- }
- return *this;
- }
- Poligon::Poligon(const Poligon &p) : GrafickiElement(p)
- {
- broj_tjemena = p.broj_tjemena;
- x = new double[p.broj_tjemena];
- y = new double[p.broj_tjemena];
- for (int i(0); i < p.broj_tjemena; i++)
- {
- x[i] = p.x[i];
- y[i] = p.y[i];
- }
- }
- Poligon::Poligon(Poligon &&p) : GrafickiElement(p)
- {
- broj_tjemena = p.broj_tjemena;
- x = std::move(p.x);
- y = std::move(p.y);
- p.broj_tjemena = 0;
- p.x = nullptr; p.y = nullptr;
- }
- class GrafickiSistem
- {
- std::vector<std::shared_ptr<GrafickiElement>> elementi;
- public:
- GrafickiSistem() {}
- GrafickiSistem(const GrafickiSistem &s);
- GrafickiSistem(GrafickiSistem &&s);
- void RegistrirajElement(GrafickiElement *e) { elementi.push_back(std::shared_ptr<GrafickiElement>(e)); }
- void RegistrirajElement(const GrafickiElement &e) { elementi.emplace_back(e.DajKopiju()); }
- void IspisiSve() const { for (int i(0); i < elementi.size(); i++) { elementi[i]->Ispisi(); std::cout << std::endl; } }
- void Load(std::string ime_datoteke);
- };
- void GrafickiSistem::Load(std::string ime_datoteke)
- {
- std::ifstream ucitaj(ime_datoteke);
- int br_elemenata(0);
- ucitaj >> br_elemenata;
- for (int i(0); i < br_elemenata; i++)
- {
- char znak;
- int boja;
- double x, y;
- ucitaj >> znak;
- ucitaj >> boja;
- if (znak == 'T')
- {
- ucitaj >> x >> y;
- RegistrirajElement(Tacka(GrafickiElement::Boje(boja), x, y));
- }
- else if (znak == 'P')
- {
- int br_tjemena;
- ucitaj >> br_tjemena;
- Poligon *p(new Poligon(GrafickiElement::Boje(boja), br_tjemena));
- for (int j(0); j < br_tjemena; j++)
- {
- ucitaj >> x >> y;
- p->PostaviTjeme(j + 1, x, y);
- }
- RegistrirajElement(p);
- }
- else if (znak == 'K')
- {
- double r;
- ucitaj >> x >> y >> r;
- RegistrirajElement(Krug(GrafickiElement::Boje(boja), x, y, r));
- }
- }
- }
- GrafickiSistem::GrafickiSistem(GrafickiSistem &&s)
- {
- elementi = std::move(s.elementi);
- s.elementi.resize(0);
- }
- GrafickiSistem::GrafickiSistem(const GrafickiSistem &s)
- {
- for (unsigned int i(0); i < s.elementi.size(); i++)
- elementi.emplace_back(s.elementi[i]->DajKopiju());
- }
- int main()
- {
- GrafickiSistem s;
- s.RegistrirajElement(new Tacka(GrafickiElement::Crvena, 30, 20));
- Poligon *p(new Poligon(GrafickiElement::Plava, 4));
- p->PostaviTjeme(1, 10, 10); p->PostaviTjeme(2, 10, 50);
- p->PostaviTjeme(3, 50, 50); p->PostaviTjeme(4, 50, 10);
- s.RegistrirajElement(p);
- s.IspisiSve();
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement