Advertisement
amermo

ROK TP II parcijala popravni

Jun 19th, 2015
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. #include <stdexcept>
  5. #include <cmath>
  6. #include <fstream>
  7.  
  8. class GrafickiElement
  9. {
  10. public:
  11.     enum Boje { Crvena, Zelena, Zuta, Plava, Ljubicasta, Crna, Bijela };
  12.     GrafickiElement(Boje boja) : boja(boja) {}
  13.     Boje DajBoju() const { return boja; }
  14.     virtual void Ispisi() const { std::cout << boja; }
  15.     virtual ~GrafickiElement() {}
  16.     virtual double DajPovrsinu() const = 0;
  17.     virtual GrafickiElement *DajKopiju() const = 0;
  18. private:
  19.     Boje boja;
  20. };
  21.  
  22. class Tacka : public GrafickiElement
  23. {
  24.     double x, y;
  25. public:
  26.     Tacka(GrafickiElement::Boje boja, double x, double y) : GrafickiElement(boja), x(x), y(y) {}
  27.     void Ispisi() const { std::cout << "{" << x << "," << y << "}"; }
  28.     double DajX() const { return x; }
  29.     double DajY() const { return y; }
  30.     friend Tacka operator *(const Tacka &t, double k);
  31.     friend Tacka operator *(double k, const Tacka &t) { return t*k; }
  32.     double DajPovrsinu() const override { return 0; }
  33.     Tacka *DajKopiju() const override { return new Tacka(*this); }
  34. };
  35.  
  36. Tacka operator *(const Tacka &t, double k)
  37. {
  38.     Tacka rez(t);
  39.     rez.x *= k; rez.y *= k;
  40.     return rez;
  41. }
  42.  
  43. class Krug : public GrafickiElement
  44. {
  45.     double x, y, r;
  46. public:
  47.     Krug(GrafickiElement::Boje boja, double x, double y, double r) : GrafickiElement(boja), x(x), y(y), r(r) {}
  48.     Krug(GrafickiElement::Boje boja, const Tacka &t, double r) : GrafickiElement(boja), x(t.DajX()), y(t.DajY()), r(r) {}
  49.     void Ispisi() const { std::cout << "{{" << x << "," << y << "}," << r << "}"; }
  50.     double DajPovrsinu() const override { return r*r * 4 * atan(1); }
  51.     Krug *DajKopiju() const override { return new Krug(*this); }
  52. };
  53.  
  54. class Poligon : public GrafickiElement
  55. {
  56.     int broj_tjemena;
  57.     double *x, *y;
  58. public:
  59.     Poligon(GrafickiElement::Boje boja, int broj_tjemena);
  60.     ~Poligon() { delete[] x; delete[] y; }
  61.     Poligon(const Poligon &p);
  62.     Poligon(Poligon &&p);
  63.     Poligon &operator =(const Poligon &p);
  64.     Poligon &operator =(Poligon &&p);
  65.     void PostaviTjeme(int redni_broj, double x, double y) { Poligon::x[redni_broj - 1] = x; Poligon::y[redni_broj - 1] = y; }
  66.     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 << "}"; }
  67.     void DodajTjeme(int redni_broj, double x, double y);
  68.     void DodajTjeme(int redni_broj, const Tacka &t);
  69.     double DajPovrsinu() const override;
  70.     Tacka operator [](int tjeme) const { return Tacka(DajBoju(), x[tjeme - 1], y[tjeme - 1]); }
  71.     Poligon *DajKopiju() const override { return new Poligon(*this); }
  72. };
  73.  
  74.  
  75. double Poligon::DajPovrsinu() const
  76. {
  77.     double p(0);
  78.     for (int i(3); i <= broj_tjemena; i++)
  79.         p += x[1] * (y[i - 1] - y[i]) + x[i - 1] * (y[i] - y[1]) + x[i] * (y[1] - y[i - 1]);
  80.     return p;
  81. }
  82.  
  83.  
  84. void Poligon::DodajTjeme(int redni_broj, const Tacka &t)
  85. {
  86.     if (redni_broj > broj_tjemena || redni_broj < 1) throw std::range_error("Redni broj tjemena nije u legalnom opsegu");
  87.     /*Tijelo sad njihovo*/
  88. }
  89.  
  90. void Poligon::DodajTjeme(int redni_broj, double x, double y)
  91. {
  92.     if (redni_broj > broj_tjemena || redni_broj < 1) throw std::range_error("Redni broj tjemena nije u legalnom opsegu");
  93.     /*Tijelo sad njihovo koje nije u prilogu postavljeno*/
  94. }
  95.  
  96.  
  97. Poligon::Poligon(GrafickiElement::Boje boja, int broj_tjemena) : GrafickiElement(boja), broj_tjemena(broj_tjemena), x(nullptr), y(nullptr)
  98. {
  99.     try
  100.     {
  101.         x = new double[broj_tjemena];
  102.         y = new double[broj_tjemena];
  103.     }
  104.     catch (std::bad_alloc)
  105.     {
  106.         delete[] x;
  107.         delete[] y;
  108.         throw;
  109.     }
  110. }
  111.  
  112. Poligon &Poligon::operator =(Poligon &&p)
  113. {
  114.     if (&p != this)
  115.     {
  116.         delete[] x; delete[] y;
  117.         broj_tjemena = p.broj_tjemena;
  118.         x = std::move(p.x); y = std::move(p.y);
  119.         p.broj_tjemena = 0;
  120.         p.x = nullptr; p.y = nullptr;
  121.     }
  122.     return *this;
  123. }
  124.  
  125. Poligon &Poligon::operator =(const Poligon &p)
  126. {
  127.     if (&p != this)
  128.     {
  129.         delete[] x; delete[] y;
  130.         broj_tjemena = p.broj_tjemena;
  131.         x = new double[p.broj_tjemena];
  132.         y = new double[p.broj_tjemena];
  133.         for (int i(0); i < p.broj_tjemena; i++)
  134.         {
  135.             x[i] = p.x[i];
  136.             y[i] = p.x[i];
  137.         }
  138.     }
  139.     return *this;
  140. }
  141.  
  142. Poligon::Poligon(const Poligon &p) : GrafickiElement(p)
  143. {
  144.     broj_tjemena = p.broj_tjemena;
  145.     x = new double[p.broj_tjemena];
  146.     y = new double[p.broj_tjemena];
  147.     for (int i(0); i < p.broj_tjemena; i++)
  148.     {
  149.         x[i] = p.x[i];
  150.         y[i] = p.y[i];
  151.     }
  152. }
  153.  
  154. Poligon::Poligon(Poligon &&p) : GrafickiElement(p)
  155. {
  156.     broj_tjemena = p.broj_tjemena;
  157.     x = std::move(p.x);
  158.     y = std::move(p.y);
  159.     p.broj_tjemena = 0;
  160.     p.x = nullptr; p.y = nullptr;
  161. }
  162.  
  163. class GrafickiSistem
  164. {
  165.     std::vector<std::shared_ptr<GrafickiElement>> elementi;
  166. public:
  167.     GrafickiSistem() {}
  168.     GrafickiSistem(const GrafickiSistem &s);
  169.     GrafickiSistem(GrafickiSistem &&s);
  170.     void RegistrirajElement(GrafickiElement *e) { elementi.push_back(std::shared_ptr<GrafickiElement>(e)); }
  171.     void RegistrirajElement(const GrafickiElement &e) { elementi.emplace_back(e.DajKopiju()); }
  172.     void IspisiSve() const { for (int i(0); i < elementi.size(); i++) { elementi[i]->Ispisi(); std::cout << std::endl; } }
  173.     void Load(std::string ime_datoteke);
  174. };
  175.  
  176. void GrafickiSistem::Load(std::string ime_datoteke)
  177. {
  178.     std::ifstream ucitaj(ime_datoteke);
  179.     int br_elemenata(0);
  180.     ucitaj >> br_elemenata;
  181.     for (int i(0); i < br_elemenata; i++)
  182.     {
  183.         char znak;
  184.         int boja;
  185.         double x, y;
  186.         ucitaj >> znak;
  187.         ucitaj >> boja;
  188.         if (znak == 'T')
  189.         {
  190.             ucitaj >> x >> y;
  191.             RegistrirajElement(Tacka(GrafickiElement::Boje(boja), x, y));
  192.         }
  193.         else if (znak == 'P')
  194.         {
  195.             int br_tjemena;
  196.             ucitaj >> br_tjemena;
  197.             Poligon *p(new Poligon(GrafickiElement::Boje(boja), br_tjemena));
  198.             for (int j(0); j < br_tjemena; j++)
  199.             {
  200.                 ucitaj >> x >> y;
  201.                 p->PostaviTjeme(j + 1, x, y);
  202.             }
  203.             RegistrirajElement(p);
  204.         }
  205.         else if (znak == 'K')
  206.         {
  207.             double r;
  208.             ucitaj >> x >> y >> r;
  209.             RegistrirajElement(Krug(GrafickiElement::Boje(boja), x, y, r));
  210.         }
  211.     }
  212. }
  213.  
  214.  
  215. GrafickiSistem::GrafickiSistem(GrafickiSistem &&s)
  216. {
  217.     elementi = std::move(s.elementi);
  218.     s.elementi.resize(0);
  219. }
  220.  
  221. GrafickiSistem::GrafickiSistem(const GrafickiSistem &s)
  222. {
  223.     for (unsigned int i(0); i < s.elementi.size(); i++)
  224.         elementi.emplace_back(s.elementi[i]->DajKopiju());
  225. }
  226.  
  227. int main()
  228. {
  229.     GrafickiSistem s;
  230.     s.RegistrirajElement(new Tacka(GrafickiElement::Crvena, 30, 20));
  231.     Poligon *p(new Poligon(GrafickiElement::Plava, 4));
  232.     p->PostaviTjeme(1, 10, 10); p->PostaviTjeme(2, 10, 50);
  233.     p->PostaviTjeme(3, 50, 50); p->PostaviTjeme(4, 50, 10);
  234.     s.RegistrirajElement(p);
  235.     s.IspisiSve();
  236.     system("PAUSE");
  237.     return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement