Advertisement
Guest User

555

a guest
May 29th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.03 KB | None | 0 0
  1. // ConsoleApplication33.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6.  
  7.  
  8.  
  9. #include <iostream>
  10. #include "cstdlib"
  11. #include<ostream>
  12. #include <vector>
  13. #include <algorithm>
  14. #include <string>
  15. #include <math.h>
  16.  
  17.  
  18.  
  19. template <typename station, typename number>
  20. class Train
  21. {
  22. protected:
  23. station destination, expedition;
  24. number train, vanNum;
  25. public:
  26.  
  27.  
  28. //template <typename station, typename number>friend std::ostream& operator<<(std::ostream& s, const Train<station, number> &obj);
  29. // template <typename station, typename number>friend std::istream& operator>>(std::istream& s, Train &obj);
  30.  
  31. Train& operator+(Train<station, number>& obj);
  32. //friend Train<station, number>& operator + (const Train<station, number>& obj, const Train<station, number>& obj1);
  33. Train<station, number>& operator=(Train<station, number> &obj);
  34.  
  35.  
  36. void getData();
  37. void addVan();
  38. void changeDestination();
  39. void changeExpedition();
  40. void setTrain();
  41. void getTrain();
  42. Train();
  43. Train(number tarin, number vNum, station exp, station dest);
  44. Train(const Train &p);
  45. ~Train();
  46. };
  47. /*
  48. template <typename station, typename number>
  49. std::ostream& operator<<(std::ostream& s, const Train<station, number>& obj)
  50. {
  51. s << "Train number : " << obj.train << " " << obj.expedition << " - " << obj.destination << std::endl
  52. << "Number of vans : " << obj.vanNum << std::endl;
  53. return s;
  54. }
  55.  
  56. template <typename station, typename number>
  57. std::istream& operator >> (std::istream& s, Train &obj)
  58. {
  59. std::cout << std::endl << " Please enter the number of the train :";
  60. s >> obj.train;
  61. std::cout << std::endl << "Number of vans :";
  62. s >> obj.vanNum;
  63. std::cout << std::endl << "Expedition :";
  64. s >> obj.expedition;
  65. std::cout << std::endl << "Destination :";
  66. s >> obj.destination;
  67. return s;
  68. }
  69. */
  70.  
  71. template<typename station, typename number>
  72. class advTrain : public Train<station, number>
  73. {
  74. protected:
  75. station destination, expedition;
  76. number train, vanNum;
  77. number **places;
  78. number *eachVan;
  79.  
  80. public:
  81.  
  82.  
  83. //friend advTrain<station, number> operator +(advTrain<station, number>& obj1, advTrain<station, number>& obj);
  84. advTrain<station, number>& operator+(advTrain<station, number> &obj);
  85. advTrain<station, number>& operator=(const advTrain<station, number> &obj);
  86.  
  87.  
  88. advTrain();
  89. advTrain(const advTrain &p);
  90. advTrain(number tra, number vanNum, station dest, station exp);
  91. void advAddVan();
  92. ~advTrain();
  93. void getInfo();
  94. };
  95.  
  96.  
  97.  
  98.  
  99. /*
  100. Train<typename station,typename number> operator + (const Train<station, number>& obj, const Train<station, number>& obj1)
  101. {
  102. Train<std::string, float> Fr;
  103. Fr.vanNum = obj.vanNum + obj1.vanNum;
  104. return Fr;
  105. }
  106. */
  107.  
  108. template<typename station, typename number>
  109. Train<station, number>& Train<station, number>::operator+(Train<station, number>& obj)
  110. {
  111. this->vanNum = this->vanNum + obj.vanNum;
  112. return *this;
  113.  
  114. }
  115.  
  116. template<typename station, typename number>
  117. Train<station, number>& Train<station, number>::operator=(Train<station, number> &obj)
  118. {
  119. this->vanNum = obj.vanNum;
  120. return *this;
  121. }
  122.  
  123.  
  124.  
  125.  
  126. /*advTrain<station, number> operator+(advTrain<station, number>& obj1, advTrain<station, number>& obj)
  127. {
  128. advTrain<std::string, double> Friended;
  129. for (int i = 0; i < Friended.vanNum; i++)
  130. {
  131. delete[] Friended.places[i];
  132. }
  133. delete[] Friended.places;
  134. delete[] Friended.eachVan;
  135. Friended.vanNum = obj1.vanNum + obj.vanNum;
  136. Friended.train = obj1.train + 1;
  137. Friended.destination = obj1.destination;
  138. Friended.expedition = obj1.expedition;
  139. int count = 1;
  140. Friended.eachVan = new number[Friended.vanNum];
  141. for (int i = 0; i < Friended.vanNum; i++)
  142. {
  143. if (count <= obj1.vanNum)
  144. {
  145. Friended.eachVan[i] = obj1.eachVan[i];
  146. count++;
  147. }
  148. else
  149. {
  150.  
  151. Friended.eachVan[i] = obj.eachVan[i - obj1.vanNum];
  152. }
  153. }
  154.  
  155.  
  156. count = 1;
  157. Friended.places = new number*[Friended.vanNum];
  158. for (int i = 0; i < Friended.vanNum; i++)
  159. {
  160. Friended.places[i] = new number[Friended.eachVan[i]];
  161. }
  162. for (int i = 0; i < Friended.vanNum; i++)
  163. {
  164. for (int j = 0; j < Friended.eachVan[i]; j++)
  165. {
  166. if (count <= obj1.vanNum)
  167. {
  168. Friended.places[i][j] = obj1.places[i][j];
  169.  
  170.  
  171. }
  172. else
  173. {
  174. Friended.places[i][j] = obj.places[i - obj1.vanNum][j];
  175. }
  176. }
  177. count++;
  178.  
  179. }
  180. return Friended;
  181. }
  182. */
  183.  
  184. template<typename station, typename number>
  185. advTrain<station, number>& advTrain<station, number>::operator+(advTrain<station, number>& obj)
  186. {
  187. number* tempMass = new number[vanNum];
  188. for (int i = 0; i < vanNum; i++)
  189. {
  190. tempMass[i] = eachVan[i];
  191. }
  192. delete[] this->eachVan;
  193. int tempVanNum = this->vanNum;
  194. this->vanNum = this->vanNum + obj.vanNum;
  195. this->eachVan = new number[vanNum];
  196. int eachVanCounter = 0;
  197. for (int i = 0; i < vanNum; i++)
  198. {
  199.  
  200. if (i < tempVanNum)
  201. {
  202. this->eachVan[i] = tempMass[i];
  203. }
  204. else
  205. {
  206. this->eachVan[i] = obj.eachVan[eachVanCounter];
  207. eachVanCounter++;
  208. }
  209. }
  210. int **pls = new number*[tempVanNum];
  211. for (int i = 0; i < tempVanNum; i++)
  212. {
  213. pls[i] = new number[tempMass[i]];
  214. }
  215. for (int i = 0; i < tempVanNum; i++)
  216. {
  217. for (int j = 0; j < tempMass[i]; j++)
  218. {
  219. pls[i][j] = places[i][j];
  220. }
  221. }
  222. for (int i = 0; i < tempVanNum; i++)
  223. {
  224. delete[]this->places[i];
  225. }
  226. delete[]this->places;
  227. this->places = new number*[vanNum];
  228. for (int i = 0; i < vanNum; i++)
  229. {
  230. this->places[i] = new number[eachVan[i]];
  231. }
  232. int VanCounter = 0;
  233. for (int i = 0; i < vanNum; i++)
  234. {
  235. if (i < tempVanNum)
  236. {
  237. for (int j = 0; j < eachVan[i]; j++)
  238. {
  239. this->places[i][j] = pls[i][j];
  240. }
  241. }
  242. else
  243. {
  244. for (int j = 0; j < eachVan[i]; j++)
  245. {
  246. this->places[i][j] = obj.places[VanCounter][j];
  247. }
  248. VanCounter++;
  249. }
  250. }
  251. delete[]tempMass;
  252. for (int i = 0; i < tempVanNum; i++)
  253. {
  254. delete[]pls[i];
  255. }
  256. delete[]pls;
  257. return *this;
  258. }
  259.  
  260. template<typename station, typename number>
  261. advTrain<station, number>& advTrain<station, number>::operator=(const advTrain<station, number> &obj)
  262. {
  263. for (int i = 0; i < vanNum; i++)
  264. {
  265. delete[]this->places[i];
  266. }
  267. delete[]this->places;
  268. delete[]this->eachVan;
  269. this->vanNum = obj.vanNum;
  270. this->eachVan = new number[this->vanNum];
  271. for (int i = 0; i < this->vanNum; i++)
  272. {
  273. this->eachVan[i] = obj.eachVan[i];
  274. }
  275. this->places = new number*[vanNum];
  276. for (int i = 0; i < this->vanNum; i++)
  277. {
  278. this->places[i] = new number[this->eachVan[i]];
  279. }
  280. for (int i = 0; i < vanNum; i++)
  281. {
  282. for (int k = 0; k < eachVan[i]; k++)
  283. {
  284. this->places[i][k] = obj.places[i][k];
  285. }
  286. }
  287. return *this;
  288. }
  289.  
  290.  
  291.  
  292. template<typename station, typename number>
  293. Train<station, number>::~Train() {}
  294.  
  295. template<typename station, typename number>
  296. Train<station, number>::Train()
  297. {
  298. train = 734.5;
  299. vanNum = 9;
  300. expedition = "Kyiv";
  301. destination = "Pokrovs'k";
  302. }
  303.  
  304. template<typename station, typename number>
  305. void Train<station, number>::getData()
  306. {
  307. std::cout << "Train number : " << train << " " << expedition << " - " << destination << std::endl
  308. << "Number of vans : " << vanNum << std::endl;
  309. }
  310.  
  311. template<typename station, typename number>
  312. Train<station, number>::Train(const Train<station, number> &p)
  313. {
  314. train = p.train;
  315. vanNum = p.vanNum;
  316. expedition = p.expedition;
  317. destination = p.destination;
  318. }
  319.  
  320. template<typename station, typename number>
  321. Train<station, number>::Train(number tarin, number vNum, station exp, station dest)
  322. {
  323. train = tarin;
  324. vanNum = vNum;
  325. expedition = exp;
  326. destination = dest;
  327. }
  328.  
  329.  
  330.  
  331. template<typename station, typename number>
  332. void Train<station, number>::addVan()
  333. {
  334. std::cout << "Please enter amount of additional vans " << std::endl;
  335. number additionalVans;
  336. std::cin >> additionalVans;
  337. vanNum = vanNum + additionalVans;
  338. }
  339.  
  340. template<typename station, typename number>
  341. void Train<station, number>::setTrain()
  342. {
  343. std::cout << "Please enter the number of the Train ";
  344. std::cin >> this->train;
  345. }
  346.  
  347. template<typename station, typename number>
  348. void Train<station, number>::getTrain()
  349. {
  350. std::cout << " The number of the train : " << train << std::endl;
  351. }
  352.  
  353. template<typename station, typename number>
  354. void Train<station, number>::changeExpedition()
  355. {
  356. std::cout << "Change expedition station for train number # " << train << std::endl;
  357. std::cin >> expedition;
  358. }
  359.  
  360. template<typename station, typename number>
  361. void Train<station, number>::changeDestination()
  362. {
  363.  
  364. std::cout << "Change destination station for train number # " << train << std::endl;
  365. std::cin >> destination;
  366. }
  367.  
  368.  
  369.  
  370.  
  371. template<typename station, typename number>
  372. void advTrain<station, number>::getInfo()
  373. {
  374. std::cout << "Train number : " << train << " " << expedition << " - " << destination << std::endl
  375. << "Number of vans : " << vanNum << std::endl;
  376. std::cout << "Free places : " << std::endl;
  377. for (int j = 0; j < vanNum; j++)
  378. {
  379.  
  380. std::cout << "Van #" << j + 1 << " : ";
  381. for (int k = 0; k < eachVan[j]; k++)
  382. {
  383.  
  384. std::cout << places[j][k] << ", ";
  385. }
  386. std::cout << std::endl;
  387. }
  388. }
  389.  
  390. template<typename station, typename number>
  391. advTrain<station, number>::~advTrain()
  392. {
  393. delete[]eachVan;
  394. for (int i = 0; i < vanNum; i++)
  395. {
  396. delete[]places[i];
  397. }
  398. delete[]places;
  399. }
  400.  
  401.  
  402.  
  403. template<typename station, typename number>
  404. advTrain<station, number>::advTrain()
  405. {
  406. train = 734.5;
  407. vanNum = 10;
  408. expedition = "Kyiv";
  409. destination = "Pokrovs'k";
  410. places = new number*[vanNum];
  411. eachVan = new number[vanNum];
  412. for (int h = 0; h < vanNum; h++)
  413. {
  414. eachVan[h] = rand() % 15;
  415. }
  416.  
  417. for (int i = 0; i < vanNum; i++)
  418. {
  419. places[i] = new number[eachVan[i]];
  420. }
  421.  
  422. std::vector<int> plcs(15);
  423.  
  424. int n = 0;
  425.  
  426. for (int j = 0; j < vanNum; j++) //vn-1
  427. {
  428. for (int i = 0; i < plcs.size(); i++)
  429. {
  430. plcs[i] = i + 1;
  431. }
  432. std::random_shuffle(plcs.begin(), plcs.end());
  433. for (int k = 0; k < eachVan[j]; k++)
  434. {
  435. places[j][k] = plcs[n];
  436. n++;
  437. }
  438. n = 0;
  439. }
  440. plcs.erase(plcs.begin(), plcs.end());
  441. }
  442.  
  443. template<typename station, typename number>
  444. void advTrain<station, number>::advAddVan()
  445. {
  446. std::cout << "Please enter how many vans you want to add : ";
  447. number additionalVans, tempVN;
  448. std::cin >> additionalVans;
  449. tempVN = vanNum;
  450. vanNum = vanNum + additionalVans;
  451.  
  452. int *tempEachVan;
  453. tempEachVan = new number[tempVN];
  454. for (int i = 0; i < tempVN; i++)
  455. {
  456. tempEachVan[i] = eachVan[i];
  457. }
  458. delete[]eachVan;
  459.  
  460.  
  461. this->eachVan = new number[vanNum];
  462. for (int i = 0; i < vanNum; i++)
  463. {
  464. if (i < tempVN)// ili bez =
  465. {
  466. eachVan[i] = tempEachVan[i];
  467. }
  468. else
  469. {
  470. eachVan[i] = 15;
  471. }
  472. }
  473.  
  474. int **tempMass;
  475. tempMass = new number*[tempVN];
  476. for (int i = 0; i < tempVN; i++)
  477. {
  478. tempMass[i] = new number[eachVan[i]];
  479. }
  480.  
  481. for (int j = 0; j < tempVN; j++)
  482. {
  483. for (int k = 0; k < eachVan[j]; k++)
  484. {
  485. tempMass[j][k] = places[j][k];
  486. }
  487. }
  488.  
  489. for (int i = 0; i < tempVN; i++)
  490. {
  491. delete[]places[i];
  492. }
  493. delete[]places;
  494.  
  495. places = new number*[vanNum];
  496. for (int i = 0; i < vanNum; i++)
  497. {
  498. if (i < tempVN)
  499. {
  500. places[i] = new number[eachVan[i]];
  501. }
  502. else
  503. {
  504. places[i] = new number[15];
  505. }
  506.  
  507. }
  508.  
  509. int n = 1;
  510. for (int j = 0; j < vanNum; j++)
  511. {
  512. if (j < tempVN)
  513. {
  514. for (int k = 0; k < tempEachVan[j]; k++)
  515. {
  516. places[j][k] = tempMass[j][k];
  517. }
  518.  
  519. }
  520. else
  521. {
  522. for (int k = 0; k < 15; k++)
  523. {
  524. places[j][k] = n;
  525. n++;
  526. }
  527. }
  528. n = 1;
  529. }
  530.  
  531.  
  532. delete[]tempEachVan;
  533. for (int i = 0; i < tempVN; i++)
  534. {
  535. delete[]tempMass[i];
  536. }
  537. delete[]tempMass;
  538. }
  539.  
  540.  
  541.  
  542.  
  543. template<typename station, typename number>
  544. advTrain<station, number>::advTrain(const advTrain<station, number> &p)
  545. {
  546. train = p.train;
  547. vanNum = p.vanNum;
  548. destination = p.destination;
  549. expedition = p.expedition;
  550. places = new number*[vanNum];
  551. eachVan = new number[vanNum];
  552. for (int i = 0; i < vanNum; i++)
  553. {
  554. eachVan[i] = p.eachVan[i];
  555. }
  556. for (int i = 0; i < vanNum; i++)
  557. {
  558. places[i] = new number[eachVan[i]];
  559. }
  560. for (int j = 0; j < vanNum; j++)
  561. {
  562. for (int k = 0; k < eachVan[j]; k++)
  563. {
  564. places[j][k] = p.places[j][k];
  565. }
  566. }
  567. }
  568.  
  569. template<typename station, typename number>
  570. advTrain<station, number>::advTrain(number tra, number vanNumero, station dest, station exp)
  571. {
  572. train = tra;
  573. vanNum = vanNumero;
  574. destination = dest;
  575. expedition = exp;
  576. places = new number*[vanNum];
  577. eachVan = new number[vanNum];
  578. for (int h = 0; h < vanNum; h++)
  579. {
  580. eachVan[h] = rand() % 15;
  581. }
  582.  
  583. for (int i = 0; i < vanNum; i++)
  584. {
  585. places[i] = new number[eachVan[i]];
  586. }
  587.  
  588. std::vector<number> plcs(15);
  589.  
  590. int n = 0;
  591.  
  592. for (int j = 0; j < vanNum; j++)
  593. {
  594. for (int i = 0; i < plcs.size(); i++)
  595. {
  596. plcs[i] = i + 1;
  597. }
  598. std::random_shuffle(plcs.begin(), plcs.end());
  599. for (int k = 0; k < eachVan[j]; k++)
  600. {
  601. places[j][k] = plcs[n];
  602. n++;
  603. }
  604. n = 0;
  605. }
  606. }
  607.  
  608.  
  609.  
  610. int main()
  611. {
  612.  
  613.  
  614. Train<std::string, float> asdtestT;
  615. asdtestT.getData();
  616.  
  617. Train<std::string, float> qw;
  618.  
  619. qw = qw + asdtestT;
  620. qw.getData();
  621.  
  622. advTrain<std::string, int> Testing;
  623. Testing.getInfo();
  624.  
  625. std::cin.get();
  626. std::cin.get();
  627.  
  628. return 0;
  629. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement