Advertisement
kokokozhina

Untitled

Dec 10th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <map>
  4. #include <set>
  5. #include <vector>
  6. #include <string>
  7. #include <stdio.h>
  8.  
  9. using namespace std;
  10.  
  11. class Electrical_facility //электрическая установка
  12. {
  13. protected:
  14. string type;
  15. double r;
  16. public:
  17. Electrical_facility(string newtype, double newr)
  18. {
  19. type = newtype;
  20. r = newr;
  21. }
  22.  
  23. string get_type()
  24. {
  25. return type;
  26. }
  27.  
  28. //void set_type(string newtype)
  29. //{
  30. // this->type = newtype;
  31. //}
  32.  
  33. double get_r()
  34. {
  35. return r;
  36. }
  37.  
  38. void set_r(double newr)
  39. {
  40. this->r = newr;
  41. }
  42.  
  43. virtual void print()
  44. {
  45. cout << this->get_type() << ": " << this->get_r() << " Om" << endl;
  46. }
  47. };
  48.  
  49. class Lamp : public Electrical_facility
  50. {
  51. protected:
  52. bool light;
  53. public:
  54. Lamp(double newr) : Electrical_facility("LMP", newr)
  55. {
  56. light = false;
  57. }
  58.  
  59. bool get_light()
  60. {
  61. return light;
  62. }
  63.  
  64. void set_light(bool newlight)
  65. {
  66. this->light = newlight;
  67. }
  68.  
  69. void print()
  70. {
  71. cout << this->get_type() << ": " << this->get_r() << " Om ";
  72. if(this->get_light())
  73. cout << "light: on\n";
  74. else
  75. cout << "light: off\n";
  76. }
  77.  
  78. };
  79.  
  80. class Resistor : public Electrical_facility
  81. {
  82. public:
  83. Resistor(double newr) : Electrical_facility("RES", newr) {}
  84.  
  85. void print()
  86. {
  87. cout << this->get_type() << ": " << this->get_r() << " Om" << endl;
  88. }
  89. };
  90.  
  91. class Appliance : public Electrical_facility
  92. {
  93. protected:
  94. double req_u, req_i, du, di;
  95. bool is_working;
  96.  
  97. public:
  98. Appliance(double newr, double newrequ, double newreqi,
  99. double newdu, double newdi) : Electrical_facility("APP", newr)
  100. {
  101. req_u = newrequ;
  102. du = newdu;
  103. req_i = newreqi;
  104. di = newdi;
  105. is_working = false;
  106. }
  107.  
  108. double get_req_u()
  109. {
  110. return req_u;
  111. }
  112.  
  113. void set_req_u(double newreq_u)
  114. {
  115. this->req_u = newreq_u;
  116. }
  117.  
  118. double get_req_i()
  119. {
  120. return req_i;
  121. }
  122.  
  123. void set_req_i(double newreq_i)
  124. {
  125. this->req_i = newreq_i;
  126. }
  127.  
  128. double get_du()
  129. {
  130. return du;
  131. }
  132.  
  133. void set_du(double newdu)
  134. {
  135. this->du = newdu;
  136. }
  137.  
  138. double get_di()
  139. {
  140. return di;
  141. }
  142.  
  143. void set_di(double newdi)
  144. {
  145. this->di = newdi;
  146. }
  147.  
  148. void set_is_working(bool state)
  149. {
  150. this->is_working = state;
  151. }
  152.  
  153. bool get_is_working()
  154. {
  155. return is_working;
  156. }
  157.  
  158. void print()
  159. {
  160. cout << this->get_type() << ": " << this->get_r() << " Om ";
  161. if(this->get_is_working())
  162. cout << "work: on\n";
  163. else
  164. cout << "work: off\n";
  165. cout << " required: " << this->get_req_u() << "V " << this->get_req_i() << "A \n";
  166. cout << " variation: " << this->get_du() << "V " << this->get_di() << "A \n";
  167. }
  168. };
  169.  
  170. class CCS : public Electrical_facility
  171. {
  172. protected:
  173. double e;
  174.  
  175. public:
  176. CCS(double newr, double newe) : Electrical_facility("CCS", newr)
  177. {
  178. e = newe;
  179. }
  180.  
  181. double get_e()
  182. {
  183. return e;
  184. }
  185.  
  186. void set_e(double newe)
  187. {
  188. this->e = newe;
  189. }
  190.  
  191. void print()
  192. {
  193. cout << this->get_type() << ": " << this->get_r() << " Om " << this->get_e() << " V" << endl;
  194. }
  195. };
  196.  
  197. class Key : public Electrical_facility
  198. {
  199. protected:
  200. bool state;
  201.  
  202. public:
  203. Key(bool newstate) : Electrical_facility("KEY", 0)
  204. {
  205. state = newstate;
  206. }
  207.  
  208. bool get_state()
  209. {
  210. return state;
  211. }
  212.  
  213. void set_state(bool newstate)
  214. {
  215. this->state = newstate;
  216. }
  217.  
  218. void print()
  219. {
  220. cout << this->get_type() << ": ";
  221. if(this->get_state())
  222. cout << "locked \n";
  223. else
  224. cout << "unlocked \n";
  225. }
  226. };
  227.  
  228. class Branch
  229. {
  230. protected:
  231. vector<Electrical_facility*> branch;
  232. public:
  233. Branch() {}
  234. void set_el_f()
  235. {
  236. cout << "If you want to set a new lamp, please type 1" << endl;
  237. cout << "If you want to set a new resistor, please type 2" << endl;
  238. cout << "If you want to set a new appliance, please type 3" << endl;
  239. cout << "If you want to set a new CCS, please type 4" << endl;
  240. cout << "If you want to set a new key, please type 5" << endl;
  241. int type, place; cin >> type;
  242. cout << "Now please type the number of position, where you want to put it: ";
  243. cin >> place;
  244.  
  245. Electrical_facility* new_obj = new Electrical_facility("error", -100500);
  246. double r, ru, ri, du, di, e;
  247. bool f = true;
  248. switch (type)
  249. {
  250. case 1:
  251. cout << "Please enter the lamp resistance: ";
  252. cin >> r;
  253. new_obj = new Lamp(r);
  254. break;
  255. case 2:
  256. cout << "Please enter the resistor resistance: ";
  257. cin >> r;
  258. new_obj = new Resistor(r);
  259. break;
  260. case 3:
  261. cout << "Please enter the appliance resistance, then required voltage and amperage, ";
  262. cout << "then variation of voltage and amperage: ";
  263. cin >> r >> ru >> ri >> du >> di;
  264. new_obj = new Appliance(r, ru, ri, du, di);
  265. break;
  266. case 4:
  267. cout << "Please enter the CCS resistance and electromotive force: ";
  268.  
  269. cin >> r >> e;
  270. new_obj = new CCS(r, e);
  271. break;
  272. case 5:
  273. cout << "Please enter the key state: ";
  274. bool st;
  275. cin >> st;
  276. new_obj = new Key(st);
  277. break;
  278. default:
  279. f = false;
  280. cout << "Something went wrong, please try again\n";
  281. break;
  282. }
  283.  
  284. place--;
  285. if(f)
  286. if(place < 0 || place > branch.size())
  287. cout << "Something went wrong, please try again\n";
  288. else
  289. {
  290. branch.insert(branch.begin() + place, new_obj);
  291. }
  292. }
  293.  
  294. //bool is_empty(){
  295. // return branch.empty();
  296. //}
  297.  
  298. void remove_el_f()
  299. {
  300. cout << "Please enter the number of the facility you want to remove: ";
  301. int n; cin >> n; n--;
  302. if(n >= branch.size() || n < 0)
  303. cout << "Something went wrong, please try again\n";
  304. else
  305. branch.erase(branch.begin() + n);
  306. }
  307.  
  308. bool is_locked()
  309. {
  310. bool res = true;
  311. for(int i = 0; i < branch.size(); i++)
  312. {
  313. if(branch[i]->get_type() == "KEY")
  314. {
  315. Key key = *(dynamic_cast<Key*>(branch[i]));
  316. if(!key.get_state())
  317. {
  318. res = false;
  319. break;
  320. }
  321. }
  322. }
  323. return res;
  324. }
  325.  
  326. double get_r_from_to(int from, int to)
  327. {
  328. double res = 0;
  329. from--, to--;
  330. if(to >= 0 && to < branch.size() && from >=0 && from < branch.size()
  331. && to >= from && this->is_locked())
  332. for(int i = from; i <= to; i++)
  333. res += branch[i]->get_r();
  334. return res;
  335. }
  336.  
  337. void change_parameters()
  338. {
  339. cout << "There are " << branch.size() << " elements in this branch\n";
  340. cout << "Enter the number of element which parameters you want to change: ";
  341. int i; cin >> i; i--;
  342. if(i >= branch.size() || i < 0)
  343. cout << "Incorrect number of element, please try again\n";
  344. else
  345. {
  346. string type = branch[i]->get_type();
  347. cout << "Availiable changes: \n";
  348. if(type == "LMP")
  349. {
  350. Lamp* lmp = (dynamic_cast<Lamp*>(branch[i]));
  351. cout << "Internal resistance: now" << lmp->get_r() << " Om\n";
  352. cout << "Type 1 for changing the internal resistance, \n";
  353. cout << " 0 for exit: ";
  354. int answer; cin >> answer;
  355. switch (answer)
  356. {
  357. case 1:
  358. cout << "Enter new value of internal resistance: ";
  359. double r; cin >> r;
  360. lmp->set_r(r);
  361. break;
  362. case 0:
  363. break;
  364. default:
  365. cout << "Incorrect command\n";
  366. break;
  367. }
  368. }
  369. else if(type == "RES")
  370. {
  371. Resistor* res = (dynamic_cast<Resistor*>(branch[i]));
  372. cout << "Resistance: now" << res->get_r() << " Om\n";
  373. cout << "Type 1 for changing the resistance, \n";
  374. cout << " 0 for exit: ";
  375. int answer; cin >> answer;
  376. switch (answer)
  377. {
  378. case 1:
  379. cout << "Enter new value of resistance: ";
  380. double r; cin >> r;
  381. res->set_r(r);
  382. break;
  383. case 0:
  384. break;
  385. default:
  386. cout << "Incorrect command\n";
  387. break;
  388. }
  389. }
  390. else if(type == "APP")
  391. {
  392. Appliance* app = (dynamic_cast<Appliance*>(branch[i]));
  393. cout << "Internal resistance: now" << app->get_r() << " Om\n";
  394. cout << "Required voltage: now " << app->get_req_u() << " V\n";
  395. cout << "Required amperage: now " << app->get_req_i() << " A\n";
  396. cout << "Variation of voltage: now " << app->get_du() << " V\n";
  397. cout << "Variation of amperage: now " << app->get_di() << " A\n";
  398. cout << "Type 1 for changing the internal resistance, \n";
  399. cout << " 2 for changing the required voltage, \n";
  400. cout << " 3 for changing the required amperage, \n";
  401. cout << " 4 for changing the variation of voltage, \n";
  402. cout << " 5 for changing the variation of amperage, \n";
  403. cout << " 0 for exit: ";
  404. int answer; cin >> answer;
  405. double u, i, r;
  406. switch (answer)
  407. {
  408. case 1:
  409. cout << "Enter new value of internal resistance: ";
  410. cin >> r;
  411. app->set_r(r);
  412. break;
  413. case 2:
  414. cout << "Enter new value of required voltage: ";
  415. cin >> u;
  416. app->set_req_u(u);
  417. break;
  418. case 3:
  419. cout << "Enter new value of required amperage: ";
  420. cin >> i;
  421. app->set_req_i(i);
  422. break;
  423. case 4:
  424. cout << "Enter new value of variation of voltage: ";
  425. cin >> u;
  426. app->set_du(u);
  427. break;
  428. case 5:
  429. cout << "Enter new value of variation of amperage: ";
  430. cin >> i;
  431. app->set_di(i);
  432. break;
  433. case 0:
  434. break;
  435. default:
  436. cout << "Incorrect command\n";
  437. break;
  438. }
  439.  
  440. }
  441. else if(type == "CCS")
  442. {
  443. CCS* ccs = (dynamic_cast<CCS*>(branch[i]));
  444. cout << "Internal resistance: now" << ccs->get_r() << " Om\n";
  445. cout << "Electromotive force of the CCS: now " << ccs->get_e() << " V\n";
  446. cout << "Type 1 for changing the internal resistance, \n";
  447. cout << " 2 for changing the electromotive force, \n";
  448. cout << " 0 for exit: ";
  449. int answer; cin >> answer;
  450. switch (answer)
  451. {
  452. case 1:
  453. cout << "Enter new value of internal resistance: ";
  454. double r; cin >> r;
  455. ccs->set_r(r);
  456. break;
  457. case 2:
  458. cout << "Enter new value of electromotive force: ";
  459. double e; cin >> e;
  460. ccs->set_e(e);
  461. break;
  462. case 0:
  463. break;
  464. default:
  465. cout << "Incorrect command\n";
  466. break;
  467. }
  468. }
  469. else if(type == "KEY")
  470. {
  471. Key* key = (dynamic_cast<Key*>(branch[i]));
  472. cout << "State of the key: now ";
  473. if(key->get_state())
  474. cout << "locked\n";
  475. else cout << "unlocked\n";
  476. cout << "Type 1 for changing the key state, 0 for exit: ";
  477. int answer; cin >> answer;
  478.  
  479. switch (answer)
  480. {
  481. case 1:
  482. cout << "Type 1 (or 0) for locking (or unlocking) the key: ";
  483. bool newstate; cin >> newstate;
  484. key->set_state(newstate);
  485. case 0:
  486. break;
  487. default:
  488. cout << "Incorrect command\n";
  489. break;
  490. }
  491. }
  492. else
  493. {
  494. cout << "Coder, you have a great error! Fix it immideately!" << endl;
  495. }
  496. }
  497. }
  498.  
  499. void print(){
  500. for(int i = 0; i < branch.size(); i++)
  501. branch[i]->print();
  502. }
  503. };
  504.  
  505. class Subchain
  506. {
  507. protected:
  508. vector<Branch*> subchain;
  509. public:
  510.  
  511. };
  512.  
  513. int main()
  514. {
  515. //#ifdef _DEBUG
  516. // freopen("in.txt", "r", stdin);
  517. // freopen("out.txt", "w", stdout);
  518. //#endif
  519.  
  520. Branch* my_branch = new Branch();
  521. int n; cin >> n;
  522. while(n != 26)
  523. {
  524. switch(n)
  525. {
  526. case 1:
  527. my_branch->set_el_f();
  528. break;
  529. case 2:
  530. my_branch->remove_el_f();
  531. break;
  532. case 3:
  533. my_branch->print();
  534. break;
  535. case 4:
  536. cout << my_branch->is_locked() << endl;
  537. break;
  538. case 5:
  539. int from, to; cin >> from >> to;
  540. cout << my_branch->get_r_from_to(from, to) << endl;
  541. break;
  542. case 6:
  543. my_branch->change_parameters();
  544. break;
  545. default:
  546. break;
  547. }
  548. cout << "1 for set element\n";
  549. cout << "2 for remove element\n";
  550. cout << "3 for print\n";
  551. cout << "4 for check locked\\unlocked\n";
  552. cout << "5 for get r from to\n";
  553. cout << "6 for change parameters\n";
  554. cout << "26 for exit\n";
  555. cin >> n;
  556. }
  557.  
  558. cout << "compilation completed\n";
  559. system("pause");
  560. return 0;
  561. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement