Advertisement
add1ctus

Flowers

Jun 2nd, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Flower
  7. {
  8. protected:
  9.     char name[100];
  10.     int cost;
  11.     char type[100];
  12. public:
  13.     Flower(){}
  14.     Flower(char *n, int p, char *t)
  15.     {
  16.         strcpy(name,n);
  17.         cost=p;
  18.         strcpy(type,t);
  19.     }
  20.     virtual int price()=0;
  21.     char* getType()
  22.     {
  23.         return type;
  24.     }
  25.     char* getName()
  26.     {
  27.         return name;
  28.     }
  29.     char* getKind()
  30.     {
  31.         return type;
  32.     }
  33.     bool operator==(Flower &rhs)
  34.     {
  35.         return !strcmp(this->type,rhs.type);
  36.     }
  37. };
  38.  
  39. class PotFlower:public Flower
  40. {
  41.     int bloomedFlowers;
  42. public:
  43.     PotFlower(){}
  44.     PotFlower(char *n, int p, char* t, int b):Flower(n,p,t)
  45.     {
  46.         bloomedFlowers=b;
  47.     }
  48.     int price()
  49.     {
  50.         return cost+bloomedFlowers;
  51.     }
  52. };
  53.  
  54. class CutFlower:public Flower
  55. {
  56.     int daysSinceHarvested;
  57. public:
  58.     CutFlower(){}
  59.     CutFlower(char *n, int p, char *t, int d):Flower(n,p,t)
  60.     {
  61.         daysSinceHarvested=d;
  62.     }
  63.     int price()
  64.     {
  65.         return cost+((daysSinceHarvested<10)?20:(daysSinceHarvested<=35)?10:0);
  66.     }
  67. };
  68.  
  69. void specialFlowers(Flower **arr, int n, Flower &flower)
  70. {
  71.     int id=-1;
  72.     for(int i=0;i<n;i++)
  73.     {
  74.         if(flower==*arr[i]&&(id==-1 || arr[id]->price()<arr[i]->price()))
  75.             id=i;
  76.     }
  77.     cout<<arr[id]->getName()<<" "<<arr[id]->getKind()<<" "<<arr[id]->price()<<endl;
  78. }
  79.  
  80. int main() {
  81.  
  82.     char name[10], kind[10];
  83.     int type, age, num, price;
  84.     int n;
  85.     cin >> n;
  86.     Flower **flowers;
  87.     flowers = new Flower*[n];
  88.  
  89.     for (int i = 0; i < n; i++) {
  90.  
  91.         cin >> type >> name >> price >> kind;
  92.         if (type == 1) {
  93.             cin >> age;
  94.             flowers[i] = new CutFlower(name, price, kind, age);
  95.  
  96.         }
  97.         else {
  98.             cin >> num;
  99.             flowers[i] = new PotFlower(name, price, kind, num);
  100.         }
  101.  
  102.  
  103.     }
  104.  
  105.     CutFlower nov("opuntia", 90, "cactus", 10);
  106.     specialFlowers(flowers, n, nov);
  107.  
  108.     for (int i = 0; i < n; i++) delete flowers[i];
  109.     delete [] flowers;
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement