Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. main.cpp
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5.  
  6. #include "product.h"
  7.  
  8. int main()
  9. {
  10. ProductArray pa;
  11.  
  12. pa.add(Product("balalayka", 803, 100000));
  13. pa.add(Product("doshirak", 2005, 10000000));
  14. pa.add(Product("hren", 1994, 100000));
  15. pa.add(Product("hren 1", 1993, 99999));
  16. pa.add(Product("kolbasa", 1989, 100000));
  17. pa.add(Product("", 1233, 100000));
  18. pa.add(Product("hren", 1994, 100000));
  19.  
  20. pa.printMostExp();
  21. system("pause");
  22.  
  23. return 0;
  24. }
  25.  
  26.  
  27. product.h
  28.  
  29. //#ifndef Product_h
  30. #define Product_h
  31.  
  32. // #define _CRT_SECURE_NO_WARNINGS
  33.  
  34. #include <vector>
  35.  
  36. #define NAME_LEN 32
  37. #define ARR_MAX 32
  38.  
  39. class Product
  40. {
  41. private:
  42. char *name;
  43. int year;
  44. int cost;
  45.  
  46. public:
  47. Product();
  48. ~Product();
  49.  
  50. Product(char const * nameNew, int yearNew, int costNew);
  51. Product(const Product &prod);
  52.  
  53. Product &operator=(const Product &prod);
  54.  
  55. void setName(char const *nameNew);
  56. void setYear(int yearNew);
  57. void setCost(int costNew);
  58.  
  59. int getYear() const;
  60. int getCost() const;
  61. char const * getName() const;
  62.  
  63. void print();
  64. };
  65.  
  66. class ProductArray
  67. {
  68. private:
  69. std::vector<Product> arr;
  70.  
  71. int yearMax;
  72. public:
  73.  
  74. ProductArray();
  75. void add(Product const & prod);
  76. void printMostExp();
  77. };
  78.  
  79.  
  80. product.cpp
  81.  
  82. #define _CRT_SECURE_NO_WARNINGS
  83. #include <cstdlib>
  84. #include <cstring>
  85. #include <iostream>
  86.  
  87. #include "product.h"
  88.  
  89.  
  90.  
  91. Product::Product() : name(NULL)
  92. {
  93. ;
  94. }
  95.  
  96. Product::Product(char const *nameNew, int yearNew, int costNew) : name(NULL)
  97. {
  98. setName(nameNew);
  99. setYear(yearNew);
  100. setCost(costNew);
  101. }
  102.  
  103. Product::Product(const Product &prod)
  104. {
  105. setName(prod.getName());
  106. setYear(prod.getYear());
  107. setCost(prod.getCost());
  108. }
  109.  
  110. Product::~Product()
  111. {
  112. delete[] name;
  113. }
  114.  
  115. Product &Product::operator=(const Product &prod)
  116. {
  117. setName(prod.getName());
  118. setYear(prod.getYear());
  119. setCost(prod.getCost());
  120. return *this;
  121. }
  122.  
  123. ProductArray::ProductArray() : yearMax(1995)
  124. {
  125. }
  126.  
  127. char const * Product::getName() const
  128. {
  129. return name;
  130. }
  131.  
  132. void Product::setName(char const *nameNew)
  133. {
  134. if (name != NULL)
  135. free(NULL);
  136.  
  137. int len = strlen(nameNew);
  138. name = new char[len + 1];
  139. strcpy(name, nameNew);
  140.  
  141. //strcpy_s(name, len, nameNew);
  142. }
  143.  
  144. void Product::setYear(int yearNew)
  145. {
  146. year = yearNew;
  147. }
  148.  
  149. void Product::setCost(int costNew)
  150. {
  151. cost = costNew;
  152. }
  153.  
  154. void Product::print()
  155. {
  156. std::cout << name << " " << year << " " << cost << " ";
  157. }
  158.  
  159. int Product::getYear() const
  160. {
  161. return year;
  162. }
  163.  
  164. int Product::getCost() const
  165. {
  166. return cost;
  167. }
  168.  
  169. void ProductArray::add(Product const &prod)
  170. {
  171. arr.push_back(prod);
  172. }
  173.  
  174. void ProductArray::printMostExp()
  175. {
  176. bool isFound = false;
  177. Product *maxProd = NULL;
  178.  
  179. /* search minimal product */
  180. for (size_t i = 0; i < arr.size(); ++i)
  181. {
  182. if (arr[i].getYear() <= yearMax)
  183. {
  184. if (!isFound || (arr[i].getCost() > maxProd->getCost()))
  185. {
  186. maxProd = &arr[i];
  187. isFound = true;
  188. }
  189. }
  190. }
  191.  
  192. for (size_t i = 0; i < arr.size(); ++i)
  193. {
  194. if (arr[i].getYear() <= yearMax)
  195. {
  196. if (arr[i].getCost() == maxProd->getCost())
  197. {
  198. arr[i].print();
  199. std::cout << std::endl;
  200. }
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement