prometheus800

ООП: Сладолед

Apr 6th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. // Сладолед Problem 5 (1 / 30)
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. class IceCream{
  8. private:
  9. char* name;
  10. char ingr[100];
  11. float price;
  12. int discount;
  13. void copy(const IceCream &ic) {
  14. name = new char[strlen(ic.name) + 1];
  15. strcpy(name, ic.name);
  16. strncpy(this->ingr, ic.ingr, 99);
  17. this->ingr[99] = '\0';
  18. this->price = ic.price;
  19. this->discount = ic.discount;
  20. }
  21. public:
  22. IceCream(char* name="", char* ingr="", float price=0){
  23. this->name = new char[strlen(name)+1];
  24. strcpy(this->name, name);
  25. strncpy(this->ingr, ingr, 99);
  26. this->ingr[99] = '\0';
  27. this->price = price;
  28. this->discount = 0;
  29. }
  30.  
  31. IceCream(const IceCream &ic) {
  32. copy(ic);
  33. }
  34.  
  35. void setDiscount(int discount){
  36. if(discount >=0&&discount <=100)
  37. this->discount = discount;
  38. }
  39.  
  40. void setName(const char* name){
  41. delete[] this->name;
  42. this->name = new char[strlen(name) + 1];
  43. strcpy(this->name, name);
  44. }
  45.  
  46. void setPrice(float price){this->price = price;}
  47.  
  48. IceCream& operator=(const IceCream &ic) {
  49. if (this == &ic) return *this;
  50. delete [] name;
  51. copy(ic);
  52. return *this;
  53. }
  54.  
  55. friend ostream& operator << (ostream& outStream, const IceCream& iceCream){
  56. outStream << iceCream.name << ": ";
  57. outStream << iceCream.ingr << " ";
  58. outStream << iceCream.price << " ";
  59. if(iceCream.discount != 0)
  60. outStream << "(" << iceCream.price * (100 - iceCream.discount) / 100 << ")";
  61. return outStream;
  62. }
  63.  
  64. IceCream& operator ++ (){
  65. setDiscount(this->discount + 5);
  66. return *this;
  67. }
  68.  
  69. IceCream operator + (char* other){
  70. char* newName = new char[strlen(this->name)+strlen(other) + 4];
  71. strcat(newName, this->name);
  72. strcat(newName, " + ");
  73. strcat(newName, other);
  74. IceCream newIceCream(newName, this->ingr, this->price + 10);
  75. newIceCream.setDiscount(this->discount);
  76. return newIceCream;
  77. }
  78.  
  79. ~IceCream(){delete[] name;}
  80. };
  81.  
  82. class IceCreamShop{
  83. private:
  84. char name[50];
  85. IceCream* IceCreams;
  86. int numIceCreams;
  87. void copy(const IceCreamShop &ics){
  88. strcpy(this->name, ics.name);
  89. this->numIceCreams = ics.numIceCreams;
  90. IceCreams = new IceCream[numIceCreams];
  91. for(int i = 0; i < numIceCreams; i++){
  92. this->IceCreams[i] = ics.IceCreams[i];
  93. }
  94. }
  95. public:
  96. IceCreamShop(char* name){
  97. strcpy(this->name, name);
  98. IceCreams = NULL;
  99. numIceCreams = 0;
  100. }
  101.  
  102. IceCreamShop(const IceCreamShop& ics){copy(ics);}
  103.  
  104. IceCreamShop& operator=(const IceCreamShop &ics){
  105. if(this == &ics) return *this;
  106. delete[] IceCreams;
  107. copy(ics);
  108. return *this;
  109. }
  110.  
  111. IceCreamShop& operator += (const IceCream& other){
  112. IceCream *tmp = IceCreams;
  113. IceCreams = new IceCream[this->numIceCreams + 1];
  114. for(int i = 0; i < this->numIceCreams; i++){
  115. IceCreams[i] = tmp[i];
  116. }
  117. IceCreams[this->numIceCreams] = other;
  118. this->numIceCreams++;
  119. delete[] tmp;
  120. return *this;
  121. }
  122.  
  123. friend ostream& operator << (ostream& outStream, const IceCreamShop& shop){
  124. outStream << shop.name << endl;
  125. for(int i = 0; i < shop.numIceCreams; i++){
  126. outStream << shop.IceCreams[i] << endl;;
  127. }
  128. return outStream;
  129. }
  130.  
  131. ~IceCreamShop(){delete[] IceCreams;}
  132. };
  133.  
  134.  
  135.  
  136. // vashiot kod ovde
  137.  
  138. // zabraneto e menuvanje na main funkcijata
  139.  
  140. int main() {
  141. char name[100];
  142. char ingr[100];
  143. float price;
  144. int discount;
  145.  
  146. int testCase;
  147.  
  148. cin >> testCase;
  149. cin.get();
  150. if(testCase == 1) {
  151. cout << "====== TESTING IceCream CLASS ======" << endl;
  152. cin.getline(name,100);
  153. cin.getline(ingr,100);
  154. cin >> price;
  155. cin >> discount;
  156. cout << "CONSTRUCTOR" << endl;
  157. IceCream ic1(name, ingr, price);
  158. ic1.setDiscount(discount);
  159. cin.get();
  160. cin.getline(name,100);
  161. cin.getline(ingr,100);
  162. cin >> price;
  163. cin >> discount;
  164. IceCream ic2(name, ingr, price);
  165. ic2.setDiscount(discount);
  166. cout << "OPERATOR <<" << endl;
  167. cout << ic1 << endl;
  168. cout << ic2 << endl;
  169. cout << "OPERATOR ++" << endl;
  170. ++ic1;
  171. cout << ic1 << endl;
  172. cout << "OPERATOR +" << endl;
  173. IceCream ic3 = ic2 + "chocolate";
  174. cout << ic3 << endl;
  175. } else if(testCase == 2) {
  176. cout << "====== TESTING IceCream CONSTRUCTORS ======" << endl;
  177. cin.getline(name,100);
  178. cin.getline(ingr,100);
  179. cin >> price;
  180. cout << "CONSTRUCTOR" << endl;
  181. IceCream ic1(name, ingr, price);
  182. cout << ic1 << endl;
  183. cout << "COPY CONSTRUCTOR" << endl;
  184. IceCream ic2(ic1);
  185. cin.get();
  186. cin.getline(name,100);
  187. ic2.setName(name);
  188. cout << ic1 << endl;
  189. cout << ic2 << endl;
  190. cout << "OPERATOR =" << endl;
  191. ic1 = ic2;
  192. cin.getline(name,100);
  193. ic2.setName(name);
  194. cout << ic1 << endl;
  195. cout << ic2 << endl;
  196.  
  197. cin >> discount;
  198. ic1.setDiscount(discount);
  199.  
  200.  
  201. } else if(testCase == 3) {
  202. cout << "====== TESTING IceCreamShop ======" << endl;
  203. char icsName[50];
  204. cin.getline(icsName,100);
  205. cout << "CONSTRUCTOR" << endl;
  206. IceCreamShop ics(icsName);
  207. int n;
  208. cin >> n;
  209. cout << "OPERATOR +=" << endl;
  210. for(int i = 0; i < n; ++i) {
  211. cin.get();
  212. cin.getline(name,100);
  213. cin.getline(ingr,100);
  214. cin >> price;
  215. IceCream ic(name, ingr, price);
  216. ics += ic;
  217. }
  218. cout << ics;
  219. } else if(testCase == 4) {
  220. cout << "====== TESTING IceCreamShop CONSTRUCTORS ======" << endl;
  221. char icsName[50];
  222. cin.getline(icsName,100);
  223. IceCreamShop ics(icsName);
  224. int n;
  225. cin >> n;
  226. for(int i = 0; i < n; ++i) {
  227. cin.get();
  228. cin.getline(name,100);
  229. cin.getline(ingr,100);
  230. cin >> price;
  231. IceCream ic(name, ingr, price);
  232. ics += ic;
  233. }
  234. IceCream x("FINKI fruits", "strawberry ice cream, raspberry ice cream, blueberry ice cream", 60);
  235. IceCreamShop icp = ics;
  236. ics+=x;
  237. cout << ics << endl;
  238. cout << icp << endl;
  239. }
  240.  
  241.  
  242. return 0;
  243. }
  244.  
Add Comment
Please, Sign In to add comment