Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cmath>
- using namespace std;
- template<class T>
- class Punct
- {
- public:
- T x, y;
- Punct() : x(0), y(0) {}
- Punct(T x, T y) : x(x), y(y) {}
- Punct(const Punct& P)
- {
- x = P.x;
- y = P.y;
- }
- Punct operator=(const Punct& P)
- {
- if (this != &P)
- {
- x = P.x;
- y = P.y;
- }
- return *this;
- }
- friend istream& operator>>(istream& in, Punct& P)
- {
- in >> P.x >> P.y;
- return in;
- }
- friend ostream& operator<<(ostream& out, Punct P)
- {
- out << "(" << P.x << ' ' << P.y << ") ";
- return out;
- }
- };
- template<class T>
- class LiniePoligonala
- {
- using Punct = Punct<T>;
- private:
- int nrPuncte;
- Punct* vectPuncte;
- public:
- Punct* alocare(int nrPuncte)
- {
- Punct* vectPuncte = new Punct[nrPuncte]();
- return vectPuncte;
- }
- LiniePoligonala() : nrPuncte(0), vectPuncte(nullptr) {}
- LiniePoligonala(int nrPuncte) : nrPuncte(nrPuncte)
- {
- vectPuncte = alocare(nrPuncte);
- }
- LiniePoligonala(const LiniePoligonala& L)
- {
- nrPuncte = L.nrPuncte;
- vectPuncte = alocare(nrPuncte);
- for (int i = 0; i < nrPuncte; ++i)
- vectPuncte[i] = L.vectPuncte[i];
- }
- ~LiniePoligonala()
- {
- delete[] vectPuncte;
- }
- friend istream& operator>>(istream& in, LiniePoligonala& L)
- {
- in >> L.nrPuncte;
- L.vectPuncte = L.alocare(L.nrPuncte);
- for (int i = 0; i < L.nrPuncte; ++i)
- in >> L[i];
- return in;
- }
- friend ostream& operator<<(ostream& out, LiniePoligonala L)
- {
- for (int i = 0; i < L.nrPuncte; ++i)
- out << L[i];
- return out;
- }
- Punct& operator[](int i) throw (out_of_range)
- {
- if (i < 0 or i >= nrPuncte)
- throw out_of_range("Indice in afara limitelor. ");
- return vectPuncte[i];
- }
- LiniePoligonala operator=(const LiniePoligonala& L)
- {
- if (this != &L)
- {
- nrPuncte = L.nrPuncte;
- vectPuncte = alocare(nrPuncte);
- for (int i = 0; i < nrPuncte; ++i)
- vectPuncte[i] = L.vectPuncte[i];
- }
- return *this;
- }
- friend LiniePoligonala operator+(LiniePoligonala L1, LiniePoligonala L2)
- {
- LiniePoligonala S(L1.nrPuncte + L2.nrPuncte);
- for (int i = 0; i < L1.nrPuncte; ++i)
- S[i] = L1[i];
- for (int i = L1.nrPuncte; i < L1.nrPuncte + L2.nrPuncte; ++i)
- S[i] = L2[i - L1.nrPuncte];
- return S;
- }
- friend LiniePoligonala operator+(Punct P, LiniePoligonala L)
- {
- LiniePoligonala S(L.nrPuncte + 1);
- S[0] = P;
- for (int i = 1; i < L.nrPuncte + 1; ++i)
- S[i] = L[i - 1];
- return S;
- }
- friend LiniePoligonala operator+(LiniePoligonala L, Punct P)
- {
- LiniePoligonala S(L.nrPuncte + 1);
- S[L.nrPuncte] = P;
- for (int i = 0; i < L.nrPuncte; ++i)
- S[i] = L[i];
- return S;
- }
- float operator!()
- {
- float lungime = 0;
- for (int i = 0; i < nrPuncte - 1; ++i)
- {
- float X = vectPuncte[i + 1].x - vectPuncte[i].x;
- float Y = vectPuncte[i + 1].y - vectPuncte[i].y;
- lungime += sqrt(pow(X, 2) + pow(Y, 2));
- }
- return lungime;
- }
- friend bool operator<(LiniePoligonala L1, LiniePoligonala L2)
- {
- return !L1 < !L2;
- }
- friend bool operator>(LiniePoligonala L1, LiniePoligonala L2)
- {
- return !L1 > !L2;
- }
- };
- void citire(LiniePoligonala<int>& L, string fis)
- {
- ifstream fin(fis);
- fin >> L;
- }
- int main()
- {
- LiniePoligonala<int> L1, L2;
- citire(L1, "Linie1.txt");
- citire(L2, "Linie2.txt");
- cout << "L1: " << L1 << endl;
- cout << "L2: " << L2 << endl;
- cout << endl << "Lungimea L1: " << !L1;
- cout << endl << "Lungimea L2: " << !L2 << endl;
- if (L1 > L2)
- cout << "L1 > L2" << endl << L1 + L2 << endl;
- else
- cout << "L2 > L1" << endl << L2 + L1 << endl;
- Punct<int> P = { 0, 5 };
- cout << endl << P + L2;
- cout << endl << L2 + P << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement