Advertisement
EmpireJordan

Sladoled

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