chasnasestra

Сладолед

Mar 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class IceCream {
  6. private:
  7. char *ime;
  8. char sostav[100];
  9. float cena;
  10. int popust;
  11.  
  12. public:
  13. IceCream(const char *ime="", const char *sostav="", float cena=0.0){
  14. this->ime = new char[strlen(ime)+1];
  15. strcpy(this->ime, ime);
  16. strcpy(this->sostav, sostav);
  17. this->cena = cena;
  18. this->popust = 0;
  19. }
  20.  
  21. IceCream &operator=(const IceCream &rhs){
  22. this->ime = new char[strlen(rhs.ime)+1];
  23. strcpy(this->ime, rhs.ime);
  24. strcpy(this->sostav, rhs.sostav);
  25. this->cena = rhs.cena;
  26. this->popust = rhs.popust;
  27. return *this;
  28. }
  29.  
  30. IceCream ic(const IceCream &rhs){
  31. this->ime = new char[strlen(rhs.ime)+1];
  32. strcpy(this->ime, rhs.ime);
  33. strcpy(this->sostav, rhs.sostav);
  34. this->cena = rhs.cena;
  35. this->popust = rhs.popust;
  36. }
  37.  
  38. int setDiscount(int popust){
  39. this->popust = popust;
  40. return this->popust;
  41. }
  42.  
  43. void setName(const char* n) {
  44. ime = new char[strlen(n) + 1];
  45. strcpy(ime, n);
  46. }
  47.  
  48. ~IceCream(){ }
  49.  
  50. friend ostream &operator << (ostream &o, IceCream &rhs){
  51. if(rhs.popust>0)
  52. o << rhs.ime << ": " << rhs.sostav << " " << rhs.cena << " (" << (float)rhs.cena-(rhs.cena*rhs.popust*0.01) << ")" << " ";
  53. else o << rhs.ime << ": " << rhs.sostav << " " << rhs.cena << " ";
  54. return o;
  55. }
  56.  
  57. IceCream &operator++(){
  58. this->popust+=5;
  59. return *this;
  60. }
  61.  
  62. IceCream &operator+(const char string[]){
  63. strcat(this->ime, " + ");
  64. strcat(this->ime, string);
  65. this->cena+=10;
  66. return *this;
  67. }
  68. };
  69.  
  70. class IceCreamShop {
  71.  
  72. private:
  73. char ime[50];
  74. IceCream *ic;
  75. int brSladoledi;
  76.  
  77. public:
  78. IceCreamShop(const char *ime=""){
  79. strcpy(this->ime, ime);
  80. brSladoledi = 0;
  81. }
  82.  
  83. ~IceCreamShop(){}
  84.  
  85. IceCreamShop &operator+=(const IceCream &rhs){
  86. IceCream *temp = new IceCream[brSladoledi+1];
  87. for(int i=0; i<brSladoledi; i++)
  88. temp[i] = ic[i];
  89.  
  90. temp[brSladoledi]=rhs;
  91. ic = temp;
  92. brSladoledi++; return *this;
  93. }
  94.  
  95. friend ostream &operator << (ostream &o, IceCreamShop &rhs){
  96. o << rhs.ime << endl;
  97. for(int i=0; i<rhs.brSladoledi; i++)
  98. cout << rhs.ic[i] << endl;
  99. return o;
  100. }
  101. };
  102.  
  103. int main() {
  104. char name[100];
  105. char ingr[100];
  106. float price;
  107. int discount;
  108.  
  109. int testCase;
  110.  
  111. cin >> testCase;
  112. cin.get();
  113. if(testCase == 1) {
  114. cout << "====== TESTING IceCream CLASS ======" << endl;
  115. cin.getline(name,100);
  116. cin.getline(ingr,100);
  117. cin >> price;
  118. cin >> discount;
  119. cout << "CONSTRUCTOR" << endl;
  120. IceCream ic1(name, ingr, price);
  121. ic1.setDiscount(discount);
  122. cin.get();
  123. cin.getline(name,100);
  124. cin.getline(ingr,100);
  125. cin >> price;
  126. cin >> discount;
  127. IceCream ic2(name, ingr, price);
  128. ic2.setDiscount(discount);
  129. cout << "OPERATOR <<" << endl;
  130. cout << ic1 << endl;
  131. cout << ic2 << endl;
  132. cout << "OPERATOR ++" << endl;
  133. ++ic1;
  134. cout << ic1 << endl;
  135. cout << "OPERATOR +" << endl;
  136. IceCream ic3 = ic2 + "chocolate";
  137. cout << ic3 << endl;
  138. } else if(testCase == 2) {
  139. cout << "====== TESTING IceCream CONSTRUCTORS ======" << endl;
  140. cin.getline(name,100);
  141. cin.getline(ingr,100);
  142. cin >> price;
  143. //cin >> discount;
  144. cout << "CONSTRUCTOR" << endl;
  145. IceCream ic1(name, ingr, price);
  146. cout << ic1 << endl;
  147. cout << "COPY CONSTRUCTOR" << endl;
  148. IceCream ic2(ic1);
  149. cin.get();
  150. cin.getline(name,100);
  151. ic2.setName(name);
  152. cout << ic1 << endl;
  153. cout << ic2 << endl;
  154. cout << "OPERATOR =" << endl;
  155. ic1 = ic2;
  156. cin.getline(name,100);
  157. ic2.setName(name);
  158. cout << ic1 << endl;
  159. cout << ic2 << endl;
  160.  
  161. cin >> discount;
  162. ic1.setDiscount(discount);
  163.  
  164.  
  165. } else if(testCase == 3) {
  166. cout << "====== TESTING IceCreamShop ======" << endl;
  167. char icsName[50];
  168. cin.getline(icsName,100);
  169. cout << "CONSTRUCTOR" << endl;
  170. IceCreamShop ics(icsName);
  171. int n;
  172. cin >> n;
  173. cout << "OPERATOR +=" << endl;
  174. for(int i = 0; i < n; ++i) {
  175. cin.get();
  176. cin.getline(name,100);
  177. cin.getline(ingr,100);
  178. cin >> price;
  179. IceCream ic(name, ingr, price);
  180. ics += ic;
  181. }
  182. cout << ics;
  183. } else if(testCase == 4) {
  184. cout << "====== TESTING IceCreamShop CONSTRUCTORS ======" << endl;
  185. char icsName[50];
  186. cin.getline(icsName,100);
  187. IceCreamShop ics(icsName);
  188. int n;
  189. cin >> n;
  190. for(int i = 0; i < n; ++i) {
  191. cin.get();
  192. cin.getline(name,100);
  193. cin.getline(ingr,100);
  194. cin >> price;
  195. IceCream ic(name, ingr, price);
  196. ics += ic;
  197. }
  198. IceCream x("FINKI fruits", "strawberry ice cream, raspberry ice cream, blueberry ice cream", 60);
  199. IceCreamShop icp = ics;
  200. ics+=x;
  201. cout << ics << endl;
  202. cout << icp << endl;
  203. }
  204.  
  205.  
  206. return 0;
  207. }
Add Comment
Please, Sign In to add comment