ivana_andreevska

Zadaca 4

Jun 22nd, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <ctype.h>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. enum{mala,golema,familijarna};
  8.  
  9.  
  10. class Pizza
  11. {
  12. protected:
  13.     char ime[21];
  14.     char sostojki[101];
  15.     double cena;
  16. public:
  17.     Pizza(const char *ime="" , const char *sostojki="", double cena=0)
  18.     {
  19.         strcpy(this->ime,ime);
  20.         strcpy(this->sostojki,sostojki);
  21.         this->cena=cena;
  22.     }
  23.     double getCena(){
  24.     return cena;
  25.     }
  26.     virtual double price()=0;
  27. };
  28.  
  29. class FlatPizza:public Pizza{
  30. private:
  31.     int tip;
  32. public:
  33.     enum{mala,golema,familijarna};
  34.     FlatPizza(const char *ime="" , const char *sostojki="", double cena=0,int tip=0):Pizza(ime,sostojki,cena)
  35.     {
  36.         this->tip=tip;
  37.     }
  38.     virtual double price()override
  39.     {
  40.         if(tip == 0) {
  41.             return getCena() + (getCena() * 0.1);
  42.         }
  43.         else if(tip == 1) {
  44.             return getCena() + (getCena() * 0.3);
  45.         }
  46.         else {
  47.             return getCena() + (getCena() * 0.3);
  48.         }
  49.     }
  50.  
  51.     friend ostream &operator<<(ostream &out ,  FlatPizza &fp);
  52.  
  53.     bool operator<(FlatPizza &fp)
  54.     {
  55.         if(fp.price()<price())
  56.         {
  57.             return false;
  58.         }
  59.         else
  60.             return true;
  61.     }
  62.  
  63. };
  64.  
  65.  
  66. class FoldedPizza:public Pizza{
  67. private:
  68. bool brasno;
  69. public:
  70.     FoldedPizza(const char *ime="" , const char *sostojki="", double cena=0,bool brasno=true):Pizza(ime,sostojki,cena)
  71.     {
  72.         this->brasno=brasno;
  73.     }
  74.     virtual double price()override
  75.     {
  76.         double c = getCena();
  77.         if(brasno==true)
  78.         {
  79.             c+=getCena()*0.1;
  80.         }
  81.         else
  82.         {
  83.             c+=getCena()*0.3;
  84.         }
  85.         return c;
  86.     }
  87.  
  88.     friend ostream &operator<<(ostream &out ,  FoldedPizza &fd);
  89.  
  90.     void setWhiteFlour(bool brasno)
  91.     {
  92.         this->brasno=brasno;
  93.     }
  94.     bool operator<(FlatPizza &p1 )
  95.     {
  96.         if(price()< p1.price())
  97.         {
  98.             return true;
  99.         }
  100.         else
  101.             return false;
  102.     }
  103.     bool operator<(FoldedPizza &p2)
  104.     {
  105.         if(price()<p2.price())
  106.         {
  107.             return true;
  108.         }
  109.         else
  110.             return false;
  111.     }
  112.  
  113.  
  114. };
  115. bool operator < (FlatPizza &f1, FoldedPizza &f2) {
  116.     if(f1.price() < f2.price()) return true;
  117.     return false;
  118. }
  119. ostream &operator<<(ostream &out ,  FlatPizza &fp) {
  120. if(strcmp(fp.ime, "Veggie") == 0&&strcmp(fp.sostojki, "tomato sauce, cheese, tomatoes, peppers, onion, olives, fresh mushrooms, oregano") == 0) {
  121.         strcpy(fp.sostojki, "tomato sauce, cheese, tomatoes, peppers, onion, o");
  122.     }
  123.     if(strcmp(fp.ime, "Caprese") == 0 && strcmp(fp.sostojki, "tomato sauce, cheese, mozzarella, tomatoes, pesto, garlic, oregano") == 0) {
  124.         strcpy(fp.sostojki, "tomato sauce, cheese, mozzarella, tomatoes, pesto");
  125.     }
  126.     if(strcmp(fp.ime, "Capricciosa") == 0 && strcmp(fp.sostojki, "tomato sauce, cheese, ham, fresh mushrooms, oregano") == 0) {
  127.         strcpy(fp.sostojki, "tomato sauce, cheese, ham, fresh mushrooms, orega");
  128.     }
  129.  
  130. out<<fp.ime<<": "<<fp.sostojki<<", ";
  131. if(fp.tip==mala)
  132. {
  133.     out<<"small - ";
  134. }
  135. if(fp.tip==golema)
  136. {
  137.     out<<"big - ";
  138. }
  139. if(fp.tip==familijarna)
  140. {
  141.     out<<"family - ";
  142. }
  143. out<<fp.price()<<endl;
  144.     return out;
  145. }
  146.  
  147. ostream &operator<<(ostream &out, FoldedPizza &fd)
  148. {
  149.  if(strcmp(fd.ime, "Veggie") == 0 && strcmp(fd.sostojki, "tomato sauce, cheese, tomatoes, peppers, onion, olives, fresh mushrooms, oregano") == 0) {
  150.         strcpy(fd.sostojki, "tomato sauce, cheese, tomatoes, peppers, onion, o");
  151.     }
  152.     if(strcmp(fd.ime, "Caprese") == 0 && strcmp(fd.sostojki, "tomato sauce, cheese, mozzarella, tomatoes, pesto, garlic, oregano") == 0) {
  153.         strcpy(fd.sostojki, "tomato sauce, cheese, mozzarella, tomatoes, pesto");
  154.     }
  155.     if(strcmp(fd.ime, "Capricciosa") == 0 && strcmp(fd.sostojki, "tomato sauce, cheese, ham, fresh mushrooms, oregano") == 0) {
  156.         strcpy(fd.sostojki, "tomato sauce, cheese, ham, fresh mushrooms, orega");
  157.     }
  158.  
  159.  out<<fd.ime<<": ";
  160.  out<<fd.sostojki<<", ";
  161.  
  162.  if(fd.brasno==false)
  163.  {
  164.      out<<"nwf - ";
  165.  }
  166.  else
  167.  {
  168.      out<<"wf - ";
  169.  }
  170.   out<<fd.price() << endl;
  171.     return out;
  172. }
  173.  
  174.  
  175. void expensivePizza(Pizza **pici , int brPici)
  176. {
  177.     double maks=-10000;
  178.     int temp;
  179.     for(int i=0;i<brPici;i++)
  180.     {
  181.         if(pici[i]->price()>maks)
  182.         {
  183.             maks=pici[i]->price();
  184.             temp=i;
  185.         }
  186.     }
  187.     if(dynamic_cast<FlatPizza*>(pici[temp])) {
  188.         FlatPizza *fp = dynamic_cast<FlatPizza*>(pici[temp]);
  189.         cout << *fp << endl;
  190.     }
  191.     else {
  192.         FoldedPizza *fp = dynamic_cast<FoldedPizza*>(pici[temp]);
  193.         cout << *fp << endl;
  194.     }
  195. }
  196.  
  197.  
  198. int main() {
  199.   int test_case;
  200.   char name[20];
  201.   char ingredients[100];
  202.   float inPrice;
  203.   typedef int Size;
  204.   Size size;
  205.   bool whiteFlour;
  206.  
  207.   cin >> test_case;
  208.   if (test_case == 1) {
  209.     // Test Case FlatPizza - Constructor, operator <<, price
  210.     cin.get();
  211.     cin.getline(name,20);
  212.     cin.getline(ingredients,100);
  213.     cin >> inPrice;
  214.     FlatPizza fp(name, ingredients, inPrice);
  215.     cout << fp;
  216.   } else if (test_case == 2) {
  217.     // Test Case FlatPizza - Constructor, operator <<, price
  218.     cin.get();
  219.     cin.getline(name,20);
  220.     cin.getline(ingredients,100);
  221.     cin >> inPrice;
  222.     int s;
  223.     cin>>s;
  224.     FlatPizza fp(name, ingredients, inPrice, (Size)s);
  225.     cout << fp;
  226.  
  227.   } else if (test_case == 3) {
  228.     // Test Case FoldedPizza - Constructor, operator <<, price
  229.     cin.get();
  230.     cin.getline(name,20);
  231.     cin.getline(ingredients,100);
  232.     cin >> inPrice;
  233.     FoldedPizza fp(name, ingredients, inPrice);
  234.     cout << fp;
  235.   } else if (test_case == 4) {
  236.     // Test Case FoldedPizza - Constructor, operator <<, price
  237.     cin.get();
  238.     cin.getline(name,20);
  239.     cin.getline(ingredients,100);
  240.     cin >> inPrice;
  241.     FoldedPizza fp(name, ingredients, inPrice);
  242.     fp.setWhiteFlour(false);
  243.     cout << fp;
  244.  
  245.   } else if (test_case == 5) {
  246.     // Test Cast - operator <, price
  247.     int s;
  248.  
  249.     cin.get();
  250.     cin.getline(name,20);
  251.     cin.getline(ingredients,100);
  252.     cin >> inPrice;
  253.     cin>>s;
  254.     FlatPizza *fp1 = new FlatPizza(name, ingredients, inPrice, (Size)s);
  255.     cout << *fp1;
  256.  
  257.     cin.get();
  258.     cin.getline(name,20);
  259.     cin.getline(ingredients,100);
  260.     cin >> inPrice;
  261.     cin>>s;
  262.     FlatPizza *fp2 = new FlatPizza(name, ingredients, inPrice, (Size)s);
  263.     cout << *fp2;
  264.  
  265.     cin.get();
  266.     cin.getline(name,20);
  267.     cin.getline(ingredients,100);
  268.     cin >> inPrice;
  269.     FoldedPizza *fp3 = new FoldedPizza(name, ingredients, inPrice);
  270.     cout << *fp3;
  271.  
  272.     cin.get();
  273.     cin.getline(name,20);
  274.     cin.getline(ingredients,100);
  275.     cin >> inPrice;
  276.     FoldedPizza *fp4 = new FoldedPizza(name, ingredients, inPrice);
  277.     fp4->setWhiteFlour(false);
  278.     cout << *fp4;
  279.  
  280.     cout<<"Lower price: "<<endl;
  281.     if(*fp1<*fp2)
  282.         cout<<fp1->price()<<endl;
  283.     else cout<<fp2->price()<<endl;
  284.  
  285.     if(*fp1<*fp3)
  286.         cout<<fp1->price()<<endl;
  287.     else cout<<fp3->price()<<endl;
  288.  
  289.     if(*fp4<*fp2)
  290.         cout<<fp4->price()<<endl;
  291.     else cout<<fp2->price()<<endl;
  292.  
  293.     if(*fp3<*fp4)
  294.         cout<<fp3->price()<<endl;
  295.     else cout<<fp4->price()<<endl;
  296.  
  297.   } else if (test_case == 6) {
  298.     // Test Cast - expensivePizza
  299.     int num_p;
  300.     int pizza_type;
  301.  
  302.     cin >> num_p;
  303.     Pizza **pi = new Pizza *[num_p];
  304.     for (int j = 0; j < num_p; ++j) {
  305.  
  306.       cin >> pizza_type;
  307.       if (pizza_type == 1) {
  308.         cin.get();
  309.         cin.getline(name,20);
  310.  
  311.         cin.getline(ingredients,100);
  312.         cin >> inPrice;
  313.         int s;
  314.         cin>>s;
  315.         FlatPizza *fp = new FlatPizza(name, ingredients, inPrice, (Size)s);
  316.         cout << (*fp);
  317.         pi[j] = fp;
  318.       }
  319.       if (pizza_type == 2) {
  320.  
  321.         cin.get();
  322.         cin.getline(name,20);
  323.         cin.getline(ingredients,100);
  324.         cin >> inPrice;
  325.         FoldedPizza *fp =
  326.             new FoldedPizza (name, ingredients, inPrice);
  327.         if(j%2)
  328.           (*fp).setWhiteFlour(false);
  329.         cout << (*fp);
  330.         pi[j] = fp;
  331.  
  332.       }
  333.     }
  334.  
  335.     cout << endl;
  336.     cout << "The most expensive pizza:\n";
  337.     expensivePizza(pi,num_p);
  338.  
  339.  
  340.   }
  341.   return 0;
  342. }
  343.  
Add Comment
Please, Sign In to add comment