-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpa_lab_5_3_10_(4)-C.cpp
112 lines (93 loc) · 2.11 KB
/
cpa_lab_5_3_10_(4)-C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <sstream>
using namespace std;
class FarmAnimal
{
public:
FarmAnimal(double water_consumption);
double getWaterConsumption();
private:
double water_consumption;
};
FarmAnimal::FarmAnimal(double water_consumption)
{
this->water_consumption = water_consumption;
}
double FarmAnimal::getWaterConsumption()
{
return water_consumption;
}
class ConsumptionAccumulator
{
public:
ConsumptionAccumulator();
double getTotalConsumption();
void addConsumption(FarmAnimal animal);
private:
double total_consumption;
};
ConsumptionAccumulator::ConsumptionAccumulator() : total_consumption(0){}
double ConsumptionAccumulator::getTotalConsumption()
{
return total_consumption;
}
void ConsumptionAccumulator::addConsumption(FarmAnimal animal)
{
total_consumption += animal.getWaterConsumption();
}
class Cow : public FarmAnimal
{
public:
Cow(int weight);
private:
int weight;
};
Cow::Cow(int weight) : FarmAnimal(8.6 * (weight / 100))
{
this->weight = weight;
}
class Sheep : public FarmAnimal
{
public:
Sheep(int weight);
private:
int weight;
};
Sheep::Sheep(int weight) : FarmAnimal(1.1 * (weight / 10))
{
this->weight = weight;
}
class Horse : public FarmAnimal
{
public:
Horse(int weight);
private:
int weight;
};
Horse::Horse(int weight) : FarmAnimal(6.8 * (weight / 100))
{
this->weight = weight;
}
int main()
{
ConsumptionAccumulator accumulator;
string str, animalType;
int animalWeight;
getline(cin, str);
istringstream iss;
while(!str.empty())
{
iss.str(str);
iss >> animalType >> animalWeight;
if(animalType == "cow")
accumulator.addConsumption(Cow(animalWeight));
else if(animalType == "sheep")
accumulator.addConsumption(Sheep(animalWeight));
else if(animalType == "horse")
accumulator.addConsumption(Horse(animalWeight));
iss.clear();
getline(cin, str);
}
cout << accumulator.getTotalConsumption();
return 0;
}