Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- using namespace std;
- class PrognozaMeteo
- {
- string vremea;
- int nr_zile = 0;
- const int id;
- float* ploaie = NULL;
- public:
- PrognozaMeteo() :id(0)
- {
- string vremea = "Frumusel";
- int nr_zile = 0;
- ploaie = NULL;
- }
- PrognozaMeteo(string vremea, int nr_zile, int id, float* ploaie) : id(id)
- {
- this->vremea = vremea;
- this->nr_zile = nr_zile;
- this->ploaie = new float[nr_zile];
- for (int i = 0; i < nr_zile; i++)
- {
- this->ploaie[i] = ploaie[i];
- }
- }
- PrognozaMeteo(const PrognozaMeteo& p) : id(p.id)
- {
- this->vremea = p.vremea;
- this->nr_zile = p.nr_zile;
- if (p.ploaie)
- {
- this->ploaie = new float[p.nr_zile];
- for (int i = 0; i < p.nr_zile; i++)
- {
- this->ploaie[i] = p.ploaie[i];
- }
- }
- }
- PrognozaMeteo& operator=(const PrognozaMeteo& p)
- {
- if (this->ploaie)
- {
- delete[] this->ploaie;
- }
- this->vremea = p.vremea;
- this->nr_zile = p.nr_zile;
- if (p.ploaie)
- {
- this->ploaie = new float[p.nr_zile];
- for (int i = 0; i < p.nr_zile; i++)
- {
- this->ploaie[i] = p.ploaie[i];
- }
- }
- return*this;
- }
- ~PrognozaMeteo()
- {
- if (this->ploaie)
- delete[] ploaie;
- }
- string getVremea()
- {
- if (this->vremea.length() != 0)
- return this->vremea;
- }
- int getNrZile()
- {
- if (this->nr_zile)
- return this->nr_zile;
- }
- float* getPloaie()
- {
- float* aux = new float[this->nr_zile];
- if (this->ploaie)
- {
- for (int i = 0; i < this->nr_zile; i++)
- {
- aux[i] = ploaie[i];
- }
- }
- return aux;
- }
- void setVremea(string vreme)
- {
- if (this->vremea.length() != 0)
- this->vremea = vreme;
- }
- void setNrZile(int zile)
- {
- if (this->nr_zile)
- this->nr_zile = zile;
- }
- void setPloaie(int zile, float* ploaia)
- {
- if (this->ploaie)
- delete[] ploaie;
- if (ploaia)
- {
- this->ploaie = new float[zile];
- for (int i = 0; i < zile; i++)
- {
- ploaie[i] = ploaia[i];
- }
- }
- }
- float sum()
- {
- float sum = 0;
- for (int i = 0; i < this->nr_zile; i++)
- {
- sum += this->ploaie[i];
- }
- return sum;
- }
- float medie()
- {
- float medie;
- medie = sum() / nr_zile;
- return medie;
- }
- explicit operator int()
- {
- return this->getNrZile();
- }
- friend ostream& operator<<(ostream&, PrognozaMeteo& p);
- friend istream& operator>>(istream&, PrognozaMeteo& p);
- friend ifstream& operator>>(ifstream&, PrognozaMeteo& p);
- };
- ostream& operator<<(ostream& out, PrognozaMeteo& p)
- {
- out << "----------------------" << endl;
- out << "Prognoza Meteo pentru " << p.nr_zile << " de zile";
- out << " este " << p.vremea;
- out << " cu cantitatile de ploaie: ";
- for (int i = 0; i < p.nr_zile; i++)
- {
- out << p.ploaie[i] << ", ";
- }
- out <<endl<<"-----------------------"<< endl;
- return out;
- }
- istream& operator>>(istream& in, PrognozaMeteo& p)
- {
- char prognoza[31];
- cout << "Prognoza " << endl;
- in.getline(prognoza, 30);
- try
- {
- strlen(prognoza) > 0;
- p.vremea = prognoza;
- }
- catch (char prognoza)
- {
- throw "MUIE MA";
- }
- int zile;
- in >> zile;
- p.nr_zile = zile;
- float* ploaia = new float[zile];
- for (int i = 0; i < zile; i++)
- {
- cout << "Precipitatii pentru ziua: " << (i + 1) << endl;
- in >> ploaia[i];
- }
- delete[] p.ploaie;
- p.ploaie = new float[zile];
- for (int i = 0; i < zile; i++)
- {
- p.ploaie[i] = ploaia[i];
- }
- delete[] ploaia;
- in.clear();
- in.ignore(INT_MAX, '\n');
- return in;
- }
- ifstream& operator>>(ifstream& m, PrognozaMeteo& p)
- {
- string prediction;
- m >> prediction;
- p.vremea = prediction;
- int nr_z;
- m >> nr_z;
- p.nr_zile = nr_z;
- if (nr_z > 0)
- {
- float* ploaia = new float[nr_z];
- for (int i = 0; i < nr_z; i++)
- {
- m >> ploaia[i];
- }
- p.setPloaie(nr_z, ploaia);
- delete[] ploaia;
- }
- m.get();
- return m;
- }
- bool operator<(PrognozaMeteo p, PrognozaMeteo m)
- {
- if (p.sum() < m.sum())
- return true;
- else
- return false;
- }
- int main()
- {
- //for (;;)
- float ploaie[4] = { 55.5f, 33.3f, 444.4f, 124.5f };
- PrognozaMeteo p1;
- PrognozaMeteo p2("Ploios", 4, 1, ploaie);
- PrognozaMeteo p3 = p2;
- PrognozaMeteo p5;
- float ploaie2[3] = { 0.1f, 0.2f, 0.3f };
- PrognozaMeteo p4("Frumusel", 3, 2, ploaie2);
- p5 = p4;
- cout << "Suma cantitatii ploilor: " << p3.sum() << endl;
- cout << "In medie pe zi: " << p3.medie() << endl;
- cout << p5;
- if (p5 < p3)
- cout << "p5 < p3";
- else
- cout << "p3 < p5";
- int flaut = (int)p5;
- cout << endl << "Nr de zile pentru flautul cautuat: " << flaut << endl;
- PrognozaMeteo p6;
- cin >> p6;
- cout << p6;
- ifstream laba;
- laba.open("MUIE.txt", ios::in);
- int n;
- laba >> n;
- cout <<endl<<"Sunt atatea date:"<< n<<endl;
- PrognozaMeteo* vector = new PrognozaMeteo[n];
- for (int i = 0; i < n; i++)
- {
- laba >> vector[i];
- }
- laba.close();
- for (int i = 0; i < 3; i++)
- {
- cout << vector[i];
- }
- delete[] vector;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement