Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.07 KB | None | 0 0
  1. #ifndef INFO_H
  2. #define INFO_H
  3. #include <iostream>
  4. using std::string;
  5.  
  6. struct place_size
  7. {
  8. int x;
  9. int y;
  10. };
  11.  
  12. class Info
  13. {
  14. public:
  15. Info();
  16. Info(const Info &otherInfo);
  17. virtual ~Info();
  18.  
  19. void addName(string name);
  20. void addPlace(place_size place);
  21.  
  22. string getName() const;
  23. place_size getPlace() const;
  24.  
  25. virtual int defineChild(){return 0;}
  26.  
  27. protected:
  28. string _name;
  29. place_size _place;
  30.  
  31. };
  32.  
  33. #endif // INFO_H
  34.  
  35. ___________________________________________________
  36.  
  37. #ifndef CAR_H
  38. #define CAR_H
  39. #include <iostream>
  40. #include "info.h"
  41. #include "math.h"
  42. using std::string;
  43.  
  44. class Car : public Info
  45. {
  46. public:
  47. Car() : Info()
  48. {
  49. _name = "Default String";
  50. _place.x = 0;
  51. _place.y = 0;
  52. _size.x = 1;
  53. _size.y = 1;
  54. _angle = 0;
  55. }
  56. Car(string name, place_size place, place_size size, double angle) : Info(name,,angle)
  57. {
  58. _name = "Default String";
  59. _place.x = 0;
  60. _place.y = 0;
  61. _size.x = 1;
  62. _size.y = 1;
  63. _angle = 0;
  64. addName(name);
  65. addPlace(place);
  66. addSize(size);
  67. addAngle(angle);
  68. }
  69. Car(const Car& otherCar) : Info(otherCar)
  70. {
  71. _name = otherCar._name;
  72. _place.x = otherCar._place.x;
  73. _place.y = otherCar._place.y;
  74. _size.x = otherCar._size.x;
  75. _size.y = otherCar._size.y;
  76. _angle = otherCar._angle;
  77. }
  78.  
  79. inline void addSize(place_size size)
  80. {
  81. if (size.x > 0 && size.y > 0)
  82. {
  83. _size.x = size.x;
  84. _size.y = size.y;
  85. }
  86. }
  87.  
  88. inline void addAngle(double angle)
  89. {
  90. if (angle >= 0 && angle < M_PI*2) // pi*2
  91. _angle = angle;
  92. }
  93.  
  94. place_size getSize() const;
  95. double getAngle() const;
  96.  
  97. bool check(const Car& otherCar) const;
  98.  
  99. virtual ~Car();
  100. virtual int defineChild(){return 1;}
  101.  
  102. private:
  103. double _angle;
  104. place_size _size;
  105. };
  106.  
  107. #endif // CAR_H
  108. _________________________________________________
  109. #ifndef PODIUM_H
  110. #define PODIUM_H
  111. #include "info.h"
  112.  
  113. class Podium : public Info
  114. {
  115. public:
  116. Podium() : Info()
  117. {
  118. _name = "Default String";
  119. _place.x = 0;
  120. _place.y = 0;
  121. _radius = 1;
  122. }
  123. Podium(string name, place_size place, int radius) : Info(name,place,radius)
  124. {
  125. _name = "Default String";
  126. _place.x = 0;
  127. _place.y = 0;
  128. _radius = 1;
  129.  
  130. addName(name);
  131. addPlace(place);
  132. addRadius(radius);
  133. }
  134. Podium(const Podium& otherPodium) : Info(otherPodium)
  135. {
  136. _name = otherPodium._name;
  137. _place.x = otherPodium._place.x;
  138. _place.y = otherPodium._place.y;
  139. _radius = otherPodium._radius;
  140. }
  141.  
  142. void addRadius(int radius);
  143.  
  144. int getRadius() const;
  145.  
  146. virtual ~Podium();
  147. virtual int defineChild(){return 2;}
  148.  
  149. private:
  150. int _radius;
  151. };
  152.  
  153. #endif // PODIUM_H
  154. ____________________________________________
  155. #ifndef ARRAY_H
  156. #define ARRAY_H
  157. #include "info.h"
  158. #include "car.h"
  159. #include "podium.h"
  160.  
  161. class Array
  162. {
  163. public:
  164. Array();
  165. Array(place_size size);
  166. Array(const Array &prototype);
  167. ~Array();
  168.  
  169. void insert(Info &otherInfo, int position);
  170. void insert(Info &otherInfo); // Метод добавления элементов машин
  171. void deleteRecord(int position); // Метод удаления машин
  172. void deleteAll(); // Метод, удаляющий все машины
  173. int getLen() const; // Метод, возвращающий число хранящихся машин
  174. Info* getObject(int position) const;
  175. bool compare(Array &container) const;
  176.  
  177. bool checkCar(int position, Info *otherInfo); // метод проверки на наличие контактов между автомобилями и стенами автосалона
  178. bool checkPodium(int position, Info *otherInfo); // метод проверки на наличие контактов между подиумом и стенами автосалона
  179.  
  180. void writeToFile(const std:: string &path) const;
  181. void readFromFile(const std:: string &path);
  182.  
  183. Car convertToCar(Info &otherInfo) const;
  184. Podium convertToPodium(Info &otherInfo) const;
  185.  
  186. private:
  187. Info **_arr;
  188. int _len;
  189. int _arrSize;
  190. place_size _size;
  191. };
  192.  
  193. #endif // ARRAY_H
  194. _____________________________________________
  195. #include "array.h"
  196. #include <fstream>
  197. #include <cmath>
  198.  
  199. Array::Array()
  200. {
  201. _arrSize = 10;
  202. _arr = new Info*[_arrSize];
  203. _len = 0;
  204. _size.x = 1000;
  205. _size.y = 1000;
  206.  
  207. }
  208.  
  209. Array::Array(place_size size)
  210. {
  211. _arrSize = 10;
  212. _arr = new Info*[_arrSize];
  213. _len = _arrSize;
  214. _size.x = size.x;
  215. _size.y = size.y;
  216. }
  217.  
  218. Array::Array(const Array& prototype)
  219. {
  220. _len = prototype.getLen();
  221. _arrSize = prototype._arrSize;
  222. _arr = new Info*[_arrSize];
  223. for(int i = 0; i < _len; i++)
  224. {
  225. Info *otherInfo = prototype.getObject(i);
  226. if (otherInfo->defineChild() == 1)
  227. {
  228. Car *helper = dynamic_cast<Car*>(otherInfo);
  229. Car *newRecord = new Car(*helper);
  230. _arr[i] = newRecord;
  231. }
  232. else if (otherInfo->defineChild() == 2)
  233. {
  234. Podium *helper = dynamic_cast<Podium*>(otherInfo);
  235. Podium *newRecord = new Podium(*helper);
  236. _arr[i] = newRecord;
  237. }
  238. }
  239. }
  240.  
  241. void Array::insert(Info &otherInfo, int position)
  242. {
  243. if (position > _len)
  244. position = _len;
  245. if (position == _len){
  246. if (otherInfo.defineChild() == 1)
  247. {
  248. Car *helper = dynamic_cast<Car*>(&otherInfo);
  249. Car *newRecord = new Car(*helper);
  250. _arr[position] = newRecord;
  251. }
  252. else if (otherInfo.defineChild() == 2)
  253. {
  254. Podium *helper = dynamic_cast<Podium*>(&otherInfo);
  255. Podium *newRecord = new Podium(*helper);
  256. _arr[position] = newRecord;
  257. }
  258. }
  259. else
  260. {
  261. for (int i = _len; i > position; i--)
  262. {
  263. _arr[i] = _arr[i-1]; // move array
  264. }
  265. if (otherInfo.defineChild() == 1)
  266. {
  267. Car *helper = dynamic_cast<Car*>(&otherInfo);
  268. Car *newRecord = new Car(*helper);
  269. _arr[position] = newRecord;
  270. }
  271. else if (otherInfo.defineChild() == 2)
  272. {
  273. Podium *helper = dynamic_cast<Podium*>(&otherInfo);
  274. Podium *newRecord = new Podium(*helper);
  275. _arr[position] = newRecord;
  276. }
  277. }
  278. _len++;
  279. }
  280.  
  281. bool check(const Car& car1, const Car& car2)
  282. {
  283. float r1, r2;
  284. r1 = sqrt(pow(float(car1.getSize().x) / 2, 2) + pow(float(car1.getSize().y) / 2, 2))+0.5;
  285. r2 = sqrt(pow(float(car2.getSize().x) / 2, 2) + pow(float(car2.getSize().y) / 2, 2))+0.5;
  286. return (r1 + r2) < sqrt(pow(float(car1.getSize().x - car2.getSize().x), 2) + pow(float(car1.getSize().y - car2.getSize().y), 2));
  287. }
  288.  
  289. bool Array::checkCar(int position, Info *otherInfo)
  290. {
  291. Car *p = nullptr;
  292. p = dynamic_cast<Car*>(otherInfo);
  293. float r = sqrt(pow(float(p->getSize().x)/2, 2) + pow(float(p->getSize().y)/2, 2))+0.5;
  294. if ((p->getPlace().x + r >= _size.x) || (p->getPlace().y + r >= _size.y)|| (p->getPlace().x - r <= 0) || (p->getPlace().y - r <= 0))
  295. return false;
  296.  
  297. for (int i=0; i < _len; i++)
  298. {
  299. if ((i != position) && (_arr[i]->getName() != "Default String")) //&& (!(otherCar.check(_arr[i]))))
  300. return false;
  301. }
  302. return true;
  303. }
  304.  
  305. bool Array::checkPodium(int position, Info *otherInfo)
  306. {
  307. Podium *p = nullptr;
  308. p = dynamic_cast<Podium*>(otherInfo);
  309. if ((p->getPlace().x + p->getRadius() >= _size.x) || (p->getPlace().y + p->getRadius() >= _size.y)|| (p->getPlace().x - p->getRadius() <= 0) || (p->getPlace().y - p->getRadius() <= 0))
  310. return false;
  311. for (int i=0; i < _len; i++)
  312. {
  313. if ((i != position) && ((_arr)[i]->getName() != "Default String"))
  314. return false;
  315. }
  316. return true;
  317. }
  318.  
  319. void Array::insert(Info &otherInfo)
  320. {
  321. insert(otherInfo, _len);
  322. }
  323.  
  324. void Array::deleteRecord(int position)
  325. {
  326. if (position == _len-1)
  327. {
  328. delete (_arr[position]);
  329. }
  330. else
  331. {
  332. for (int i = position; i < _len-1; i++)
  333. {
  334. if (_arr[i+1]->defineChild() == 1)
  335. {
  336. Car *helper = dynamic_cast<Car*>(_arr[i+1]);
  337. Car *newRecord = new Car(*helper);
  338. _arr[position] = newRecord;
  339. }
  340. else if (_arr[i+1]->defineChild() == 2)
  341. {
  342. Podium *helper = dynamic_cast<Podium*>(_arr[i+1]);
  343. Podium *newRecord = new Podium(*helper);
  344. _arr[position] = newRecord;
  345. }
  346. }
  347. delete (_arr[_len - 1]);
  348. }
  349. _len--;
  350. }
  351.  
  352. void Array:: deleteAll()
  353. {
  354. for (int i = _len-1; i >=0 ; i--){
  355. delete (_arr[i]);
  356. }
  357. _len = 0;
  358. _arrSize = 10;
  359. _arr = new Info*[_arrSize];
  360. }
  361.  
  362. Array:: ~Array()
  363. {
  364. for (int i = _len-1; i >=0; i--)
  365. {
  366. delete (_arr[i]);
  367. }
  368. _len = 0;
  369. _arrSize = 0;
  370. delete[](_arr);
  371. }
  372.  
  373. int Array::getLen() const
  374. {
  375. return _len;
  376. }
  377.  
  378. Info* Array::getObject(int position) const
  379. {
  380. return _arr[position];
  381. }
  382.  
  383. void Array:: writeToFile(const string &path) const
  384. {
  385. std::ofstream stream;
  386. stream.open(path);
  387. if (stream.is_open())
  388. {
  389. for (int i = 0; i < _len; i++)
  390. {
  391. Info *otherInfo = _arr[i];
  392. stream << (*_arr[i]).defineChild() << '\n';
  393.  
  394. if((*_arr[i]).defineChild() == 1)
  395. {
  396. Car* p1 = dynamic_cast<Car*>(otherInfo);
  397. stream << p1 ->getName() << '\n';
  398. stream << p1 ->getPlace().x << '\n';
  399. stream << p1 ->getPlace().y << '\n';
  400. stream << p1 ->getSize().x << '\n';
  401. stream << p1 ->getSize().y << '\n';
  402. stream << p1 ->getAngle() << '\n';
  403. }
  404. else if ((*_arr[i]).defineChild() == 2)
  405. {
  406. Podium* p2 = dynamic_cast<Podium*>(otherInfo);
  407. stream << p2 ->getName() << '\n';
  408. stream << p2 ->getPlace().x << '\n';
  409. stream << p2 ->getPlace().y << '\n';
  410. stream << p2 ->getRadius() << '\n';
  411. }
  412. }
  413. }
  414. stream.flush();
  415. stream.close();
  416. }
  417.  
  418. void Array:: readFromFile(const string &path)
  419. {
  420. std::ifstream stream;
  421. char buffer[50];
  422. string name;
  423. place_size placeXY;
  424. place_size sizeXY;
  425. double angle;
  426. int radius;
  427. int elementDefinition;
  428. stream.open(path);
  429. if (stream.is_open())
  430. {
  431. for (int i = 0; !stream.eof(); i++)
  432. {
  433. stream.getline(buffer,50);
  434. elementDefinition = atoi(buffer);
  435.  
  436. if (elementDefinition == 1)
  437. {
  438. stream.getline(buffer,50);
  439. name = string(buffer);
  440.  
  441. stream.getline(buffer,50);
  442. placeXY.x = atoi(buffer);
  443.  
  444. stream.getline(buffer,50);
  445. placeXY.y = atoi(buffer);
  446.  
  447. stream.getline(buffer,50);
  448. sizeXY.x = atoi(buffer);
  449.  
  450. stream.getline(buffer,50);
  451. sizeXY.y = atoi(buffer);
  452.  
  453. stream.getline(buffer,50);
  454. angle = atof(buffer);
  455.  
  456. Car otherCar = Car(name,placeXY,sizeXY,angle);
  457. this->insert(otherCar);
  458. }
  459. else if (elementDefinition == 2)
  460. {
  461. stream.getline(buffer,50);
  462. name = string(buffer);
  463.  
  464. stream.getline(buffer,50);
  465. placeXY.x = atoi(buffer);
  466.  
  467. stream.getline(buffer,50);
  468. placeXY.y = atoi(buffer);
  469.  
  470. stream.getline(buffer,50);
  471. radius = atoi(buffer);
  472.  
  473. Podium otherPodium = Podium(name, placeXY, radius);
  474. this->insert(otherPodium);
  475. }
  476. }
  477. }
  478. }
  479. /*
  480. bool Array::compare(Array &container) const
  481. {
  482. for (int i=0; i < container._len; i++)
  483. {
  484. Info *firstRecord = container.getObject(i);
  485. Info *secondRecord = this->getObject(i);
  486.  
  487. if (firstRecord->defineChild() == 1 && secondRecord->defineChild()==1)
  488. {
  489. Car* ptr1 = dynamic_cast<Car*>(firstRecord);
  490. Car* ptr2 = dynamic_cast<Car*>(secondRecord);
  491.  
  492. if (ptr1->getName().compare(ptr2->getName())!=0)
  493. return false;
  494. if (ptr1->getPlace().x.compare(ptr2->getPlace().x)!=0)
  495. return false;
  496. if (ptr1->getPlace().y.compare(ptr2->getPlace().y)!=0)
  497. return false;
  498. if (ptr1->getSize().x.compare(ptr2->getSize().x)!=0)
  499. return false;
  500. if (ptr1->getSize().y )
  501. return false;
  502.  
  503. else if (firstRecord->defineChild() == 2 && secondRecord->defineChild() == 2)
  504. {
  505. Podium* ptr1 = dynamic_cast<Podium*>(firstRecord);
  506. Podium* ptr2 = dynamic_cast<Podium*>(secondRecord);
  507.  
  508. }
  509. else return false;
  510. }
  511. return true;
  512. }
  513. }
  514. */
  515.  
  516. _______________________________________________________
  517. #include "car.h"
  518.  
  519. place_size Car::getSize() const
  520. {
  521. //Селектор поля size
  522. //Ничего не принимает
  523. //Возвращает значение поля size
  524.  
  525. return _size;
  526. }
  527.  
  528. double Car::getAngle() const
  529. {
  530. //Селектор поля angle
  531. //Ничего не принимает
  532. //Возвращает значение поля angle
  533.  
  534. return _angle;
  535. }
  536.  
  537. bool Car::check(const Car &otherCar) const
  538. {
  539. float r1, r2;
  540. r1 = sqrt(pow(float(_size.x) / 2, 2) + pow(float(_size.y) / 2, 2))+0.5;
  541. r2 = sqrt(pow(float(otherCar.getSize().x) / 2, 2) + pow(float(otherCar.getSize().y) / 2, 2))+0.5;
  542. return (r1 + r2) < sqrt(pow(float(_place.x - otherCar.getPlace().x), 2) + pow(float(_place.y - otherCar.getPlace().y), 2));
  543. }
  544.  
  545.  
  546. ______________________________________________________
  547. #include "info.h"
  548.  
  549. void Info::addName(string name)
  550. {
  551. //Модификатор поля name
  552. //Принимает значение поля name
  553. //Ничего не возвращает
  554.  
  555. if (name != "")
  556. _name = name;
  557. }
  558.  
  559. void Info::addPlace(place_size place)
  560. {
  561. //Модификатор поля place
  562. //Принимает значение поля place
  563. //Ничего не возвращает
  564.  
  565. if (place.x >= 0 && place.x <= 100 && place.y >= 0 && place.y <= 100)
  566. {
  567. _place = place;
  568. }
  569. }
  570.  
  571. string Info::getName() const
  572. {
  573. //Селектор поля name
  574. //Ничего не принимает
  575. //Возвращает значение поля name
  576.  
  577. return _name;
  578. }
  579.  
  580. place_size Info::getPlace() const
  581. {
  582. //Селектор поля place
  583. //Ничего не принимает
  584. //Возвращает значение поля place
  585.  
  586. return _place;
  587. }
  588. __________________________________________________
  589. #include "podium.h"
  590.  
  591.  
  592. void Podium::addRadius(int radius)
  593. {
  594. //Модификатор поля radius
  595. //Принимает значение поля radius
  596. //Ничего не возвращает
  597.  
  598. if (radius > 0 )
  599. _radius = radius;
  600. }
  601.  
  602.  
  603. int Podium::getRadius() const
  604. {
  605. //Селектор поля radius
  606. //Ничего не принимает
  607. //Возвращает значение поля radius
  608.  
  609. return _radius;
  610. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement