Advertisement
JStefan

[Vezbi] Sladoled

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