Advertisement
Tucancitto

Pb14. Linie poligonală

Sep 3rd, 2021
1,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. template<class T>
  8. class Punct
  9. {
  10. public:
  11.     T x, y;
  12.  
  13.     Punct() : x(0), y(0) {}
  14.     Punct(T x, T y) : x(x), y(y) {}
  15.     Punct(const Punct& P)
  16.     {
  17.         x = P.x;
  18.         y = P.y;
  19.     }
  20.  
  21.     Punct operator=(const Punct& P)
  22.     {
  23.         if (this != &P)
  24.         {
  25.             x = P.x;
  26.             y = P.y;
  27.         }
  28.  
  29.         return *this;
  30.     }
  31.  
  32.     friend istream& operator>>(istream& in, Punct& P)
  33.     {
  34.         in >> P.x >> P.y;
  35.         return in;
  36.     }
  37.  
  38.     friend ostream& operator<<(ostream& out, Punct P)
  39.     {
  40.         out << "(" << P.x << ' ' << P.y << ") ";
  41.         return out;
  42.     }
  43. };
  44.  
  45. template<class T>
  46. class LiniePoligonala
  47. {
  48.     using Punct = Punct<T>;
  49.  
  50. private:
  51.     int nrPuncte;
  52.     Punct* vectPuncte;
  53.  
  54. public:
  55.     Punct* alocare(int nrPuncte)
  56.     {
  57.         Punct* vectPuncte = new Punct[nrPuncte]();
  58.         return vectPuncte;
  59.     }
  60.  
  61.     LiniePoligonala() : nrPuncte(0), vectPuncte(nullptr) {}
  62.     LiniePoligonala(int nrPuncte) : nrPuncte(nrPuncte)
  63.     {
  64.         vectPuncte = alocare(nrPuncte);
  65.     }
  66.     LiniePoligonala(const LiniePoligonala& L)
  67.     {
  68.         nrPuncte = L.nrPuncte;
  69.         vectPuncte = alocare(nrPuncte);
  70.  
  71.         for (int i = 0; i < nrPuncte; ++i)
  72.             vectPuncte[i] = L.vectPuncte[i];
  73.     }
  74.  
  75.     ~LiniePoligonala()
  76.     {
  77.         delete[] vectPuncte;
  78.     }
  79.  
  80.     friend istream& operator>>(istream& in, LiniePoligonala& L)
  81.     {
  82.         in >> L.nrPuncte;
  83.         L.vectPuncte = L.alocare(L.nrPuncte);
  84.  
  85.         for (int i = 0; i < L.nrPuncte; ++i)
  86.             in >> L[i];
  87.  
  88.         return in;
  89.     }
  90.  
  91.     friend ostream& operator<<(ostream& out, LiniePoligonala L)
  92.     {
  93.         for (int i = 0; i < L.nrPuncte; ++i)
  94.             out << L[i];
  95.  
  96.         return out;
  97.     }
  98.  
  99.     Punct& operator[](int i) throw (out_of_range)
  100.     {
  101.         if (i < 0 or i >= nrPuncte)
  102.             throw out_of_range("Indice in afara limitelor. ");
  103.  
  104.         return vectPuncte[i];
  105.     }
  106.  
  107.     LiniePoligonala operator=(const LiniePoligonala& L)
  108.     {
  109.         if (this != &L)
  110.         {
  111.             nrPuncte = L.nrPuncte;
  112.             vectPuncte = alocare(nrPuncte);
  113.  
  114.             for (int i = 0; i < nrPuncte; ++i)
  115.                 vectPuncte[i] = L.vectPuncte[i];
  116.         }
  117.  
  118.         return *this;
  119.     }
  120.  
  121.     friend LiniePoligonala operator+(LiniePoligonala L1, LiniePoligonala L2)
  122.     {
  123.         LiniePoligonala S(L1.nrPuncte + L2.nrPuncte);
  124.  
  125.         for (int i = 0; i < L1.nrPuncte; ++i)
  126.             S[i] = L1[i];
  127.  
  128.  
  129.         for (int i = L1.nrPuncte; i < L1.nrPuncte + L2.nrPuncte; ++i)
  130.             S[i] = L2[i - L1.nrPuncte];
  131.  
  132.         return S;
  133.     }
  134.  
  135.     friend LiniePoligonala operator+(Punct P, LiniePoligonala L)
  136.     {
  137.         LiniePoligonala S(L.nrPuncte + 1);
  138.  
  139.         S[0] = P;
  140.         for (int i = 1; i < L.nrPuncte + 1; ++i)
  141.             S[i] = L[i - 1];
  142.  
  143.         return S;
  144.     }
  145.  
  146.     friend LiniePoligonala operator+(LiniePoligonala L, Punct P)
  147.     {
  148.         LiniePoligonala S(L.nrPuncte + 1);
  149.  
  150.         S[L.nrPuncte] = P;
  151.         for (int i = 0; i < L.nrPuncte; ++i)
  152.             S[i] = L[i];
  153.  
  154.         return S;
  155.     }
  156.  
  157.     float operator!()
  158.     {
  159.         float lungime = 0;
  160.         for (int i = 0; i < nrPuncte - 1; ++i)
  161.         {
  162.             float X = vectPuncte[i + 1].x - vectPuncte[i].x;
  163.             float Y = vectPuncte[i + 1].y - vectPuncte[i].y;
  164.  
  165.             lungime += sqrt(pow(X, 2) + pow(Y, 2));
  166.         }
  167.  
  168.         return lungime;
  169.     }
  170.  
  171.     friend bool operator<(LiniePoligonala L1, LiniePoligonala L2)
  172.     {
  173.         return !L1 < !L2;
  174.     }
  175.  
  176.     friend bool operator>(LiniePoligonala L1, LiniePoligonala L2)
  177.     {
  178.         return !L1 > !L2;
  179.     }
  180.  
  181. };
  182.  
  183. void citire(LiniePoligonala<int>& L, string fis)
  184. {
  185.     ifstream fin(fis);
  186.     fin >> L;
  187. }
  188.  
  189. int main()
  190. {
  191.     LiniePoligonala<int> L1, L2;
  192.  
  193.     citire(L1, "Linie1.txt");
  194.     citire(L2, "Linie2.txt");
  195.  
  196.     cout << "L1: " << L1 << endl;
  197.     cout << "L2: " << L2 << endl;
  198.  
  199.     cout << endl << "Lungimea L1: " << !L1;
  200.     cout << endl << "Lungimea L2: " << !L2 << endl;
  201.  
  202.     if (L1 > L2)
  203.         cout << "L1 > L2" << endl << L1 + L2 << endl;
  204.     else
  205.         cout << "L2 > L1" << endl << L2 + L1 << endl;
  206.  
  207.     Punct<int> P = { 0, 5 };
  208.     cout << endl << P + L2;
  209.     cout << endl << L2 + P << endl;
  210.     return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement