include iostream include string include stdlib using namespace std cla

  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
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class failure
{public:
virtual void view()=0;
virtual void Repair()=0;
virtual bool Check()=0;
virtual string GetStatus()=0;
};
class EngineFailed:public failure
{private:
string status;
public:
virtual bool Check(){return status.compare("Engine is OK!")==0;}
virtual string GetStatus(){return status;}
virtual void Repair(){if (rand()%3) status="Engine is OK!";};
virtual void view(){cout<<status<<endl;}
EngineFailed()
{if (rand()%2) status="Engine is OK!";
else status="Engine failed";
};
};
class LockFailed:public failure
{private:
string status;
public:
virtual bool Check(){return status.compare("Lock is OK!")==0;}
virtual string GetStatus(){return status;}
virtual void Repair()
{if (rand()%3)
status="Lock is OK!";
};
virtual void view(){cout<<status<<endl;}
LockFailed()
{if (rand()%2) status="Lock is OK!";
else status="Lock failed";
};
};
class WeaponFailed:public failure
{private:
string status;
public:
virtual bool Check(){return status.compare("Weapon is OK!")==0;}
virtual string GetStatus(){return status;}
virtual void Repair(){if (rand()%3) status="Weapon is OK!";};
virtual void view(){cout<<status<<endl;}
WeaponFailed()
{if (rand()%2) status="Weapon is OK!";
else status="Weapon failed";
};
};
class Weapon
{private:
int Power;
// static
public:
failure *f;
void SetPower(int P){Power=P%10;}
void Fire()
{if (f->Check())
{if(Power%5>rand()%10)
cout<<"++ No one survive"<<endl;
else
if(Power%7>rand()%10)
cout<<"+ Destroyed!!!"<<endl;
else
cout<<"- Keep alive..."<<endl;
}
else {cout<<"Weapon is broken"<<endl;}
}
Weapon(){WeaponFailed W;
f=(failure*)malloc(sizeof(WeaponFailed));
f=&W;
Power=rand()%10;
f->view();
}
// Weapon(int w){f=(WeaponFailed*)malloc(10000);Power=w;int r=rand()%2; *f=WeaponFailed(r); f->view();}
~Weapon(){free(f);}
};
class Engine
{private:
int Number;
int Power;
bool started;
// static EngineFailed E;
public:
failure *f;
void SetNumber(int N){Number=N;}
void SetPower(int P){Power=P;}
Engine(){started=false;
EngineFailed E;
f=(failure*)malloc(sizeof(EngineFailed));
f=&E;
Power=rand()%10;
f->view();
}
void Start(){
if (f->Check()){started=true; cout<<"Engine starting"<<endl;}
else {started=false;
cout<<"Engine is broken"<<endl;}
};
void Stop(){ if(started) {started=false; cout<<"Engine stopped"<<endl;}
else cout<<"Engine doesn't started, cannot stop it"<<endl;}
~Engine(){free(f);}
};
class Lock
{private:
int Number;
bool opened;
// static LockFailed L;
public:
failure *f;
void SetNumber(int N){Number=N;}
Lock(){LockFailed L;
f=(failure*)malloc(sizeof(LockFailed));
f=&L;
Number=0;
opened=false;
f->view();}
// Lock(int N){f=(LockFailed*)malloc(1024); Number=N; int r=rand()%2; opened=r==0; *f=LockFailed(r); f->view();}
void Open(){
if (f->Check()) {opened=true; cout<<"Lock openning"<<endl;}
else {opened=false; cout<<"Lock is broken"<<endl;}
};
void Close(){
if (opened) {opened=false; cout<<"Lock closed"<<endl;}
else {cout<<"Lock doesn't open"<<endl;}
};
~Lock(){free(f);}
};
class space_ship
{public:
Engine e[4];
Weapon w[3];
Lock l[2];
space_ship();
};
space_ship::space_ship()
{for(int i=0;i<4;i++)
{e[i].SetNumber(i); e[i].SetPower(rand()%10);}
for(int i=0;i<3;i++)
{w[i].SetPower(rand()%10);}
for(int i=0;i<4;i++)
{l[i].SetNumber(i);}
}
int main()
{space_ship SHIP;
int i=1,j,k;
while (i!=0)
{cout<<"1-select engines"<<"\n"<<"2-select weapon"<<"\n"<<"3-select locks"<<"\n"<<"0- exit"<<"\n"<<"type your choise"<<endl;
cin>>i;
switch (i)
{case 1:
cout<<"select engine's number"<<endl;
cin>>j;
cout<<"1- engine's status"<<"\n"<<"2- start engine"<<"\n"<<"3- stop engine"<<"\n"<<"4- repair engine"<<"\n"<<"0- return"<<endl;
cin>>k;
switch (k)
{case 1:SHIP.e[j].f->view();break;
case 2:SHIP.e[j].Start();break;
case 3:SHIP.e[j].Stop();break;
case 4:SHIP.e[j].f->Repair();break;
default: break;
}
break;
case 2:
cout<<"select weapon's number"<<endl;
cin>>j;
cout<<"1- weapon's status"<<"\n"<<"2- fire"<<"\n"<<"3- repair engine"<<"\n"<<"0- return"<<endl;
cin>>k;
switch (k)
{case 1:SHIP.w[j].f->view();break;
case 2:SHIP.w[j].Fire();break;
case 3:SHIP.w[j].f->Repair();break;
default: break;
}
break;
case 3:
cout<<"select lock's number"<<endl;
cin>>j;
cout<<"1- lock's status"<<"\n"<<"2- open lock"<<"\n"<<"3- close lock"<<"\n"<<"4- repair lock"<<"\n"<<"0- return"<<endl;
cin>>k;
switch (k)
{case 1:SHIP.l[j].f->view();break;
case 2:SHIP.l[j].Open();break;
case 3:SHIP.l[j].Close();break;
case 4:SHIP.l[j].f->Repair();break;
}
break;
}
}
return 0;
};