Advertisement
Guest User

Popravena

a guest
Jun 20th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include<string.h>
  4. #include<string>
  5. using namespace std;
  6.  
  7. class AlcoholicDrink
  8. {
  9. protected:
  10.     char name[100];
  11.     char country[100];
  12.     float percent;
  13.     float baseprice;
  14. public:
  15.     static int discount;
  16.     virtual float computePrice();
  17.     AlcoholicDrink(){}
  18.     AlcoholicDrink(float percent, char *name, char *country,  float baseprice)
  19.     {
  20.         strcpy(this->name,name);
  21.         strcpy(this->country, country);
  22.         this->percent=percent;
  23.         this->baseprice=baseprice;
  24.     }
  25.     ~AlcoholicDrink(){}
  26.    static void total(AlcoholicDrink **ad, int n)
  27. {
  28.     int totalp=0;
  29.     int totald=0;
  30.    /* for(int i=0;i<n;i++)
  31.     {
  32.         if(discount==0)
  33.         {
  34.             totalp+=ad[i]->computePrice();
  35.         }
  36.         else
  37.             totald+=ad[i]->computePrice();
  38.     }*/
  39.        
  40. }
  41.     static void changeDiscount(int d)
  42. {
  43.     discount=d;
  44. }
  45. };
  46. int discount=5;
  47.  
  48.  
  49. class Beer : public AlcoholicDrink
  50. {
  51. private:
  52.      bool ingridient;//BARLEY, WHEAT
  53. public:
  54.     Beer(){}
  55.     Beer(float percent, char *name, char *country, float baseprice, bool ingridient): AlcoholicDrink(percent,name,country, baseprice)
  56.     {
  57.  
  58.         this->ingridient=ingridient;
  59.     }
  60.     ~Beer()
  61.     {}
  62.     friend ostream &operator<<(ostream &out, Beer &b)
  63.     {
  64.         out<<b.name<<" "<<b.country<<" "<<b.computePrice()<<endl;
  65.         return out;
  66.     }
  67.    float computePrice()
  68.     {
  69.         float price=0;
  70.         if(strcmp(country,"Germany")==0)
  71.         {
  72.             price+=baseprice*5/100;
  73.             return price;
  74.         }
  75.         else
  76.             return baseprice;
  77.  
  78.     }
  79. };
  80. class Wine: public AlcoholicDrink
  81. {
  82. private:
  83.     int year;
  84.     char gtype[20];
  85. public:
  86.     Wine(float percent,char *name, char *country, float baseprice, int year, char *gtype)
  87.     {
  88.         this->year=year;
  89.         strcpy(this->gtype,gtype);
  90.     }
  91.     ~Wine(){}
  92.     float computePrice()
  93.     {
  94.         float price=0;
  95.         if(strcmp(country,"Italy")==0)
  96.         {
  97.             price+=baseprice*5/100;
  98.         }
  99.         else if(year<2005)
  100.         {
  101.             price+=baseprice*15/100;
  102.         }
  103.         return price;
  104.     }
  105.     friend ostream &operator<<(ostream &out, Wine &w)
  106.     {
  107.         out<<w.name<<" "<<w.country<<" "<<w.computePrice()<<endl;
  108.         return out;
  109.     }
  110. };
  111.  
  112. void lowestPrice(AlcoholicDrink **a, int n)
  113. {
  114.     int minn=a[0]->computePrice();
  115.     int index;
  116.     for(int i=0;i<n;i++)
  117.     {
  118.         if(a[i]->computePrice()<minn)
  119.         {
  120.             minn=a[i]->computePrice();
  121.             index=i;
  122.         }
  123.         // cout<<a[index]->AlcoholicDrink::name;
  124.         cout<<minn;
  125.     }
  126. }
  127.  
  128. int main() {
  129.     int testCase;
  130.     cin >> testCase;
  131.     float p;
  132.     char name[100];
  133.     char country[100];
  134.     float price;
  135.     bool mainI;
  136.     int year;
  137.     char grape [20];
  138.     if(testCase == 1) {
  139.         cout << "===== TESTING CONSTRUCTORS ======" << endl;
  140.         cin >> p;
  141.         cin >> name;
  142.         cin >> country;
  143.         cin >> price;
  144.         cin >> mainI;
  145.         Beer b(p, name, country, price, mainI);
  146.         cout << b << endl;
  147.         cin >> p;
  148.         cin >> name;
  149.         cin >> country;
  150.         cin >> price;
  151.         cin >> year;
  152.         cin >> grape;
  153.         Wine w(p, name, country, price, year, grape);
  154.         cout << w << endl;
  155.  
  156.     } else if(testCase == 2) {
  157.         cout << "===== TESTING LOWEST PRICE ======" << endl;
  158.         int n;
  159.         cin >> n;
  160.         AlcoholicDrink** ad = new AlcoholicDrink*[n];
  161.         for(int i = 0; i < n; ++i) {
  162.             cin >> p;
  163.             cin >> name;
  164.             cin >> country;
  165.             cin >> price;
  166.  
  167.             if(i % 2 == 1){
  168.                 cin >> mainI;
  169.                 ad[i] = new Beer(p, name, country, price, mainI);
  170.             }
  171.             else {
  172.                 cin >> year;
  173.                 cin >> grape;
  174.                 ad[i] = new Wine(p, name, country, price, year, grape);
  175.             }
  176.         }
  177.  
  178.         lowestPrice(ad, n);
  179.         for(int i = 0; i < n; ++i) {
  180.             delete ad[i];
  181.         }
  182.         delete [] ad;
  183.     } else if(testCase == 3) {
  184.         cout << "===== TESTING DISCOUNT STATIC ======" << endl;
  185.         int n;
  186.         cin >> n;
  187.         AlcoholicDrink** ad = new AlcoholicDrink*[n];
  188.         for(int i = 0; i < n; ++i) {
  189.             cin >> p;
  190.             cin >> name;
  191.             cin >> country;
  192.             cin >> price;
  193.             if(i % 2 == 1){
  194.                 cin >> mainI;
  195.                 ad[i] = new Beer(p, name, country, price, mainI);
  196.             }
  197.             else {
  198.                 cin >> year;
  199.                 cin >> grape;
  200.                 ad[i] = new Wine(p, name, country, price, year, grape);
  201.             }
  202.         }
  203.         AlcoholicDrink::total(ad, n);
  204.         int d;
  205.         cin >> d;
  206.         AlcoholicDrink::changeDiscount(d);
  207.         AlcoholicDrink::total(ad, n);
  208.         for(int i = 0; i < n; ++i) {
  209.             delete ad[i];
  210.         }
  211.         delete [] ad;
  212.     }
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement