Курсач ООП(оранжерея)

  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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
using namespace std;
class Tree
{
protected:
int health;//состояние здоровья дерева
double growth;//рост в метрах
virtual void death(){}
public:
int number_of_tree;
virtual int Normal_Climate(double p1, double p2, double p3){return true;}//этот метод проверяет, подходят ли данные условия к условиям дерева
int GetHealth(){return health;}//получить здоровье
void Health_up(){health+=30;}
void Health_down(){health-=10;}
double GetGrowth(){return growth;}//получить рост
void growth_up(){growth+=0.5;}//подрости
public:
virtual ~Tree(){};
};
class Tropical_Tree: public Tree
{
static int number;
public:
Tropical_Tree()
{
number_of_tree = number;//дали порядковый номер дереву
health = 50 + rand()%50;//здоровье
growth = 0.5 + rand()%5;//начальный рост
number++;
cout<<"Tropical tree "<<number_of_tree<<" was planted"<<endl;
}
int Normal_Climate(double temperature, double humidity, double salinity)
{
if ((temperature > 15)&&(temperature < 25)&&(humidity > 79)&&(humidity<96)&&(salinity>20))
return 0;
if (temperature<16) return 1;
if (temperature>24) return 2;
if (humidity<80) return 3;
if (humidity>95) return 4;
if (salinity < 21) return 5;
}
void death()
{
cout<<"Tropical tree "<<number_of_tree<<" has been killed"<<endl;
}
~Tropical_Tree(){death();};
};
int Tropical_Tree::number = 1;
class Sub_Tropical_Tree: public Tree
{
static int number;
public:
Sub_Tropical_Tree()
{
number_of_tree = number;//дали порядковый номер дереву
health = 50 + rand()%50;//здоровье
growth = 0.3+rand()%4;//начальный рост
number++;
cout<<"Subtropical tree "<<number_of_tree<<" was planted"<<endl;
}
int Normal_Climate(double temperature, double humidity, double salinity)
{
if ((temperature > 7)&&(temperature < 21)&&(humidity > 69)&&(humidity<86)&&(salinity>20))
return 0;
if (temperature<8) return 1;
if (temperature>20) return 2;
if (humidity<70) return 3;
if (humidity>85) return 4;
if (salinity < 21) return 5;
}
void death()
{
cout<<"Subtropical tree "<<number_of_tree<<" has been killed"<<endl;
}
~Sub_Tropical_Tree(){death();};
};
int Sub_Tropical_Tree::number = 1;
class Central_Tree: public Tree
{
static int number;
public:
Central_Tree()
{
number_of_tree = number;//дали порядковый номер дереву
health = 50 + rand()%50;//здоровье
growth = 0.2+rand()%1;//начальный рост
number++;
cout<<"tree of Central "<<number_of_tree<<" was planted"<<endl;
}
int Normal_Climate(double temperature, double humidity, double salinity)
{
if ((temperature > -25)&&(temperature < 50)&&(humidity > 69)&&(humidity<86)&&(salinity>20))
return 0;
if (temperature<-26) return 1;
if (temperature>49) return 2;
if (humidity<70) return 3;
if (humidity>85) return 4;
if (salinity < 21) return 5;
}
void death()
{
cout<<"tree of Central"<<number_of_tree<<" has been killed"<<endl;
}
~Central_Tree(){death();};
};
int Central_Tree::number = 1;
class Soil
{
double humidity;
double salinity;
public:
double GetHumidity(){return humidity;}
double GetSalinity(){return salinity;}
void SetHumidity(double humid){humidity = humid;}
void SetSalinity(double salin){salinity = salin;}
Soil(){humidity = 50 + rand() % 30; salinity = 70 + rand() % 10;}
};
class Air
{
double temperature;
public:
double GetTemperature(){return temperature;}
void SetTemperature(double temp){temperature = temp;}
Air(){temperature = 15 + rand()%10;}
};
class adjustment_systems
{
public:
Soil obj1;
Air obj2;
adjustment_systems (){}
void Pour_the_soil()//полить почву
{
double humid = obj1.GetHumidity();
obj1.SetHumidity(humid+10);
cout<<"the soil was watered"<<endl;
}
void fertilize_the_soil()//удобрить почву
{
double salin = obj1.GetSalinity();
obj1.SetSalinity(salin+5);
cout<<"the soil was fertilized"<<endl;
}
void warm_air()//подогреть воздух
{
double temp = obj2.GetTemperature();
obj2.SetTemperature(temp+30);
cout<<"the air was preheated"<<endl;
}
void cool_air()//охладить воздух
{
double temp = obj2.GetTemperature();
obj2.SetTemperature(temp-10);
cout<<"was cooled air"<<endl;
}
};
class Factory
{
public:
Tree * generator()
{
switch(rand()%3)
{
case 0: return new Tropical_Tree;
case 1: return new Sub_Tropical_Tree;
case 2:return new Central_Tree;
}
}
};
class Agronomist
{
Factory f;
public:
adjustment_systems sys;
vector <Tree*> v;//вектор деревьев
Agronomist()
{
for (int i = 0; i < 20; i++)
{
Tree * t = 0;
t = f.generator();
v.push_back(t);
}
}
void plant_a_tree()//посадить дерево
{
Tree * t = 0;
t = f.generator();
v.push_back(t);
}
void remove_tree(int n)//убрать дерево
{
v[n]->~Tree();
v.erase(v.begin() + n);
}
void treated(int n)//вылечить дерево
{
v[n]->Health_up();
cout<<"Tree "<<v[n]->number_of_tree<<" was cured"<<endl;
}
double Monitor_Health(int n){cout<<"Health = "<<v[n]->GetHealth()<<endl; return v[n]->GetHealth();}
double Monitor_Growth(int n){cout<<v[n]->GetGrowth()<<endl; return v[n]->GetGrowth();}
void Pour_the_soil(){sys.Pour_the_soil();}
void fertilize_the_soil(){sys.fertilize_the_soil();}
void warm_air(){sys.warm_air();}
void cool_air(){sys.cool_air();}
int Normal_Climate(int n)
{
return v[n]->Normal_Climate(sys.obj2.GetTemperature(), sys.obj1.GetHumidity(), sys.obj1.GetSalinity());
}
};
class Monitor
{
Agronomist a;
public:
void start()
{
while(1)
{
for (int i = 0; i < 20; i++)
{
switch(a.Normal_Climate(i))
{
case 0: break;
case 1: a.warm_air();
a.v[i]->Health_down();
break;
case 2: a.cool_air();
a.v[i]->Health_down();
break;
case 3: a.Pour_the_soil();
a.v[i]->Health_down();
break;
case 4: break;
case 5: a.fertilize_the_soil();
a.v[i]->Health_down();
break;
}
Sleep(2000);
a.v[i]->growth_up();
a.sys.obj1.SetSalinity(a.sys.obj1.GetSalinity()-0.1);
a.sys.obj1.SetHumidity(a.sys.obj1.GetHumidity()-0.25);
a.sys.obj2.SetTemperature(a.sys.obj2.GetTemperature()-0.35);
if (a.v[i]->GetHealth()==0) //если дерево погибло, то посадить новое
{
a.remove_tree(i);//убрать погибшее дерево
a.plant_a_tree();//посадить новое
}
if (a.v[i]->GetHealth() < 20) a.treated(i);
}
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Monitor a;
a.start();
return 0;
}