Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. class PrognozaMeteo
  8. {
  9. string vremea;
  10. int nr_zile = 0;
  11. const int id;
  12. float* ploaie = NULL;
  13. public:
  14. PrognozaMeteo() :id(0)
  15. {
  16. string vremea = "Frumusel";
  17. int nr_zile = 0;
  18. ploaie = NULL;
  19. }
  20. PrognozaMeteo(string vremea, int nr_zile, int id, float* ploaie) : id(id)
  21. {
  22. this->vremea = vremea;
  23. this->nr_zile = nr_zile;
  24.  
  25. this->ploaie = new float[nr_zile];
  26. for (int i = 0; i < nr_zile; i++)
  27. {
  28. this->ploaie[i] = ploaie[i];
  29. }
  30. }
  31. PrognozaMeteo(const PrognozaMeteo& p) : id(p.id)
  32. {
  33. this->vremea = p.vremea;
  34. this->nr_zile = p.nr_zile;
  35.  
  36. if (p.ploaie)
  37. {
  38. this->ploaie = new float[p.nr_zile];
  39. for (int i = 0; i < p.nr_zile; i++)
  40. {
  41. this->ploaie[i] = p.ploaie[i];
  42. }
  43. }
  44. }
  45. PrognozaMeteo& operator=(const PrognozaMeteo& p)
  46. {
  47. if (this->ploaie)
  48. {
  49. delete[] this->ploaie;
  50. }
  51.  
  52. this->vremea = p.vremea;
  53. this->nr_zile = p.nr_zile;
  54.  
  55. if (p.ploaie)
  56. {
  57. this->ploaie = new float[p.nr_zile];
  58. for (int i = 0; i < p.nr_zile; i++)
  59. {
  60. this->ploaie[i] = p.ploaie[i];
  61. }
  62. }
  63. return*this;
  64. }
  65. ~PrognozaMeteo()
  66. {
  67. if (this->ploaie)
  68. delete[] ploaie;
  69. }
  70. string getVremea()
  71. {
  72. if (this->vremea.length() != 0)
  73. return this->vremea;
  74. }
  75. int getNrZile()
  76. {
  77. if (this->nr_zile)
  78. return this->nr_zile;
  79. }
  80. float* getPloaie()
  81. {
  82. float* aux = new float[this->nr_zile];
  83. if (this->ploaie)
  84. {
  85. for (int i = 0; i < this->nr_zile; i++)
  86. {
  87. aux[i] = ploaie[i];
  88. }
  89. }
  90. return aux;
  91. }
  92. void setVremea(string vreme)
  93. {
  94. if (this->vremea.length() != 0)
  95. this->vremea = vreme;
  96. }
  97. void setNrZile(int zile)
  98. {
  99. if (this->nr_zile)
  100. this->nr_zile = zile;
  101. }
  102. void setPloaie(int zile, float* ploaia)
  103. {
  104. if (this->ploaie)
  105. delete[] ploaie;
  106. if (ploaia)
  107. {
  108. this->ploaie = new float[zile];
  109. for (int i = 0; i < zile; i++)
  110. {
  111. ploaie[i] = ploaia[i];
  112. }
  113. }
  114. }
  115. float sum()
  116. {
  117. float sum = 0;
  118. for (int i = 0; i < this->nr_zile; i++)
  119. {
  120. sum += this->ploaie[i];
  121. }
  122. return sum;
  123. }
  124. float medie()
  125. {
  126. float medie;
  127. medie = sum() / nr_zile;
  128. return medie;
  129. }
  130. explicit operator int()
  131. {
  132. return this->getNrZile();
  133. }
  134. friend ostream& operator<<(ostream&, PrognozaMeteo& p);
  135. friend istream& operator>>(istream&, PrognozaMeteo& p);
  136. friend ifstream& operator>>(ifstream&, PrognozaMeteo& p);
  137.  
  138. };
  139. ostream& operator<<(ostream& out, PrognozaMeteo& p)
  140. {
  141. out << "----------------------" << endl;
  142. out << "Prognoza Meteo pentru " << p.nr_zile << " de zile";
  143. out << " este " << p.vremea;
  144. out << " cu cantitatile de ploaie: ";
  145. for (int i = 0; i < p.nr_zile; i++)
  146. {
  147. out << p.ploaie[i] << ", ";
  148. }
  149. out <<endl<<"-----------------------"<< endl;
  150.  
  151. return out;
  152. }
  153. istream& operator>>(istream& in, PrognozaMeteo& p)
  154. {
  155. char prognoza[31];
  156. cout << "Prognoza " << endl;
  157. in.getline(prognoza, 30);
  158. try
  159. {
  160. strlen(prognoza) > 0;
  161. p.vremea = prognoza;
  162. }
  163. catch (char prognoza)
  164. {
  165. throw "MUIE MA";
  166. }
  167. int zile;
  168. in >> zile;
  169. p.nr_zile = zile;
  170. float* ploaia = new float[zile];
  171. for (int i = 0; i < zile; i++)
  172. {
  173. cout << "Precipitatii pentru ziua: " << (i + 1) << endl;
  174. in >> ploaia[i];
  175. }
  176. delete[] p.ploaie;
  177. p.ploaie = new float[zile];
  178. for (int i = 0; i < zile; i++)
  179. {
  180. p.ploaie[i] = ploaia[i];
  181. }
  182. delete[] ploaia;
  183. in.clear();
  184. in.ignore(INT_MAX, '\n');
  185. return in;
  186. }
  187.  
  188. ifstream& operator>>(ifstream& m, PrognozaMeteo& p)
  189. {
  190. string prediction;
  191. m >> prediction;
  192. p.vremea = prediction;
  193. int nr_z;
  194. m >> nr_z;
  195. p.nr_zile = nr_z;
  196. if (nr_z > 0)
  197. {
  198. float* ploaia = new float[nr_z];
  199. for (int i = 0; i < nr_z; i++)
  200. {
  201. m >> ploaia[i];
  202. }
  203. p.setPloaie(nr_z, ploaia);
  204. delete[] ploaia;
  205. }
  206. m.get();
  207. return m;
  208. }
  209.  
  210. bool operator<(PrognozaMeteo p, PrognozaMeteo m)
  211. {
  212. if (p.sum() < m.sum())
  213. return true;
  214. else
  215. return false;
  216. }
  217.  
  218. int main()
  219. {
  220. //for (;;)
  221. float ploaie[4] = { 55.5f, 33.3f, 444.4f, 124.5f };
  222. PrognozaMeteo p1;
  223. PrognozaMeteo p2("Ploios", 4, 1, ploaie);
  224. PrognozaMeteo p3 = p2;
  225. PrognozaMeteo p5;
  226. float ploaie2[3] = { 0.1f, 0.2f, 0.3f };
  227. PrognozaMeteo p4("Frumusel", 3, 2, ploaie2);
  228. p5 = p4;
  229. cout << "Suma cantitatii ploilor: " << p3.sum() << endl;
  230. cout << "In medie pe zi: " << p3.medie() << endl;
  231. cout << p5;
  232. if (p5 < p3)
  233. cout << "p5 < p3";
  234. else
  235. cout << "p3 < p5";
  236.  
  237. int flaut = (int)p5;
  238. cout << endl << "Nr de zile pentru flautul cautuat: " << flaut << endl;
  239. PrognozaMeteo p6;
  240. cin >> p6;
  241. cout << p6;
  242. ifstream laba;
  243. laba.open("MUIE.txt", ios::in);
  244. int n;
  245. laba >> n;
  246. cout <<endl<<"Sunt atatea date:"<< n<<endl;
  247. PrognozaMeteo* vector = new PrognozaMeteo[n];
  248. for (int i = 0; i < n; i++)
  249. {
  250. laba >> vector[i];
  251. }
  252. laba.close();
  253. for (int i = 0; i < 3; i++)
  254. {
  255. cout << vector[i];
  256. }
  257. delete[] vector;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement