josiftepe

Untitled

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