fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <functional>
  5. #include <string>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <set>
  9.  
  10. namespace sportstracker {
  11. namespace algorithms {
  12.  
  13. #include <functional>
  14.  
  15. namespace detail {
  16.  
  17. template< typename T >
  18. size_t divizer( std::vector< T >& array, size_t start, size_t end, std::function< bool( T, T )> comp )
  19. {
  20. auto marker = start;
  21. for( auto i = start; i <= end; i++ ) {
  22. if( comp( array[ i ], array[ end ] ) ) {
  23. std::swap( array[ marker ] ,array[ i ] );
  24. marker++;
  25. }
  26. }
  27. return marker - 1;
  28. }
  29.  
  30. template< typename T >
  31. void quick_sort( std::vector< T >& array, size_t start, size_t end, std::function< bool( T, T )> comp )
  32. {
  33. if( start >= end )
  34. return;
  35.  
  36. auto pivot = divizer< T >( array, start, end, comp );
  37.  
  38. quick_sort( array, start , pivot - 1, comp );
  39. quick_sort( array, pivot + 1, end , comp );
  40. }
  41. }
  42.  
  43. /*
  44.   * Sorting vector in increasing order by default.
  45.   ***/
  46. template< typename T >
  47. void quick_sort( std::vector< T >& array )
  48. {
  49. if( array.size() < 2 )
  50. return;
  51. detail::quick_sort( array, 0, array.size() - 1, []( T a, T b ) -> bool { return ( a <= b )? true : false; } );
  52. }
  53.  
  54. /*
  55.   * Sorting vector using comparator
  56.   ***/
  57. template< typename T >
  58. void quick_sort( std::vector< T >& array, std::function< bool( T, T )> comp )
  59. {
  60. if( array.size() < 2 )
  61. return;
  62. detail::quick_sort( array, 0, array.size() - 1, comp );
  63. }
  64. }
  65.  
  66. namespace utils {
  67.  
  68. struct Nocopy {
  69. Nocopy() {}
  70. Nocopy( const Nocopy& ) = delete;
  71. Nocopy& operator=( const Nocopy& ) = delete;
  72. };
  73.  
  74. std::string year_postfix( int yaer )
  75. {
  76. auto t1 = yaer % 10;
  77. auto t2 = yaer % 100;
  78.  
  79. if( t1 == 1 && t2 != 11 ) {
  80. return "год";
  81. }
  82.  
  83. if( t1 >= 2 && t1 <= 4 && ( t2 < 10 || t2 >= 20 ) ) {
  84. return "года";
  85. }
  86. else {
  87. return "лет";
  88. }
  89. }
  90.  
  91. }
  92.  
  93. namespace football {
  94.  
  95. class IPosition {
  96. public:
  97. IPosition() {}
  98. IPosition( const IPosition& ) {}
  99.  
  100. virtual ~IPosition() {}
  101.  
  102. virtual int id () { return 0; }
  103. virtual std::string name() { return ""; }
  104. };
  105.  
  106. class PosGoalkeeper : public IPosition {
  107. public:
  108. int id () override final { return 1; }
  109. std::string name() override final { return "вратарь"; }
  110. };
  111.  
  112. class PosDefender : public IPosition {
  113. public:
  114. int id () override final { return 2; }
  115. std::string name() override final { return "защитник"; }
  116. };
  117.  
  118. class PosMidfielder: public IPosition {
  119. public:
  120. int id () override final { return 3; }
  121. std::string name() override final { return "полузащитник"; }
  122. };
  123.  
  124. class PosDefensiveMidfielder : public IPosition {
  125. public:
  126. int id () override final { return 4; }
  127. std::string name() override final { return "опорный полузащитник"; }
  128. };
  129.  
  130. class PosAttackingMidfielder : public IPosition {
  131. public:
  132. int id () override final { return 5; }
  133. std::string name() override final { return "атакующий полузащитник"; }
  134. };
  135.  
  136. class PosForward : public IPosition {
  137. public:
  138. int id () override final { return 6; }
  139. std::string name() override final { return "нападающий"; }
  140. };
  141.  
  142. class IStatus {
  143. public:
  144. IStatus() {}
  145. IStatus( const IStatus& ) {}
  146.  
  147. virtual ~IStatus() {}
  148.  
  149. virtual int id () { return 0; }
  150. virtual std::string name() { return ""; }
  151. };
  152.  
  153. class StatNewbie : public IStatus {
  154. public:
  155. int id () override final { return 1; }
  156. std::string name() override final { return "новичок"; }
  157. };
  158.  
  159. class StatMiddle : public IStatus {
  160. public:
  161. int id () override final { return 2; }
  162. std::string name() override final { return "середняк"; }
  163. };
  164.  
  165. class StatStar : public IStatus {
  166. public:
  167. int id () override final { return 3; }
  168. std::string name() override final { return "звезда"; }
  169. };
  170.  
  171. class StatSuperstar : public IStatus {
  172. public:
  173. int id () override final { return 4; }
  174. std::string name() override final { return "суперзвезда"; }
  175. };
  176.  
  177. class StatOldfag : public IStatus {
  178. public:
  179. int id () override final { return 5; }
  180. std::string name() override final { return "старожил"; }
  181. };
  182.  
  183. class Player {
  184. public:
  185.  
  186. Player()
  187. {
  188. age_ = 0;
  189. position_ = new PosGoalkeeper();
  190. status_ = new StatNewbie();
  191. }
  192.  
  193. Player( const Player& value )
  194. {
  195. firstName_ = value.firstName_;
  196. secondName_ = value.secondName_;
  197. age_ = value.age_;
  198. status_ = value.status_;
  199. position_ = value.position_;
  200. }
  201.  
  202.  
  203. Player(
  204. const std::string& firstName,
  205. const std::string& secondName,
  206. int age,
  207. IStatus* status,
  208. IPosition* position )
  209. : firstName_( firstName ),
  210. secondName_( secondName ),
  211. age_( age )
  212. {
  213. status_ = status;
  214. position_ = position;
  215. }
  216.  
  217. ~Player()
  218. {
  219. }
  220.  
  221. std::string toString ()
  222. {
  223. return firstName_ + " " +
  224. secondName_ + " " +
  225. std::to_string( age_ ) + " " +
  226. utils::year_postfix( age_ ) + " " +
  227. position_->name() + " " +
  228. status_->name();
  229. }
  230.  
  231. std::string firstName ()
  232. {
  233. return firstName_;
  234. }
  235.  
  236. std::string secondName()
  237. {
  238. return secondName_;
  239. }
  240.  
  241. IPosition* position()
  242. {
  243. return position_;
  244. }
  245.  
  246. unsigned int age()
  247. {
  248. return age_;
  249. }
  250.  
  251. void setFirstName ( const std::string& value )
  252. {
  253. firstName_ = value;
  254. }
  255.  
  256. void setSecondName( const std::string& value )
  257. {
  258. secondName_ = value;
  259. }
  260.  
  261. void setAge ( unsigned int value )
  262. {
  263. age_ = value;
  264. }
  265.  
  266. private:
  267.  
  268. std::string firstName_;
  269. std::string secondName_;
  270. unsigned int age_;
  271. IPosition* position_;
  272. IStatus* status_;
  273.  
  274. };
  275.  
  276. class SportsTeam {
  277. public:
  278.  
  279. virtual void addPlayer ( const Player& player ) = 0;
  280. virtual void removePlayerAt( size_t pos ) = 0;
  281. virtual void output () = 0;
  282.  
  283. };
  284.  
  285. class FootballTeam : public SportsTeam, public utils::Nocopy {
  286. public:
  287. FootballTeam() {}
  288. ~FootballTeam() {}
  289.  
  290. virtual void addPlayer( const Player& player ) override
  291. {
  292. players_.push_back( player );
  293. }
  294.  
  295. virtual void removePlayerAt( size_t pos ) override
  296. {
  297. // maybe it's worth to throw exception here?..
  298. players_.erase( players_.begin() + pos );
  299. }
  300.  
  301. virtual void output() override
  302. {
  303. for( auto& itr : players_ )
  304. std::cout << itr.toString() << std::endl;
  305. }
  306.  
  307. void sort()
  308. {
  309. auto comp = []( Player a, Player b ) -> bool {
  310. bool result = true;
  311. if( a.secondName().compare( b.secondName() ) == 0 ) {
  312. result = ( a.secondName() > b.secondName() ) && ( a.firstName() > b.firstName() );
  313. }
  314. else {
  315. result = ( a.secondName() > b.secondName() );
  316. }
  317.  
  318. result &= ( a.age() > b.age() );
  319. result &= ( a.position()->id() > b.position()->id() );
  320.  
  321. return result;
  322. };
  323.  
  324. std::sort( players_.begin(), players_.end(), comp );
  325.  
  326. }
  327.  
  328. private:
  329.  
  330. std::vector< Player > players_;
  331.  
  332. };
  333.  
  334. class Game {
  335. public:
  336. static Game& getInstance()
  337. {
  338. std::cout << "I'm a singletone, lol!" << std::endl;
  339. static Game instance;
  340. return instance;
  341. }
  342.  
  343. private:
  344.  
  345. FootballTeam firstTeam_;
  346. FootballTeam secondTeam_;
  347.  
  348. Game() {}
  349.  
  350. Game( Game const& ) = delete;
  351. void operator=( Game const& ) = delete;
  352. };
  353.  
  354.  
  355. }
  356. }
  357.  
  358. using namespace sportstracker;
  359. using namespace sportstracker::football;
  360. using namespace std;
  361.  
  362. int main(int argc, char const *argv[])
  363. {
  364. // vector< int > array = { 1, 5, 3, 8 };
  365.  
  366. // algorithms::quick_sort< int >( array, []( int a, int b ) -> bool { return ( a <= b )? true : false; } );
  367.  
  368. // for( auto& itr : array )
  369. // cout << itr << endl;
  370.  
  371.  
  372. auto germany = new FootballTeam();
  373.  
  374. germany->addPlayer( Player( "Мануэль" , "Нойер" , 28, new StatStar() , new PosGoalkeeper() ) );
  375. germany->addPlayer( Player( "Рон" , "Цилер" , 25, new StatNewbie() , new PosGoalkeeper() ) );
  376. germany->addPlayer( Player( "Филипп" , "Лам" , 32, new StatStar() , new PosDefender() ) );
  377. germany->addPlayer( Player( "Тонн" , "Кросс" , 23, new StatSuperstar(), new PosGoalkeeper() ) );
  378. germany->addPlayer( Player( "Месут" , "Озил" , 26, new StatSuperstar(), new PosDefensiveMidfielder() ) );
  379. germany->addPlayer( Player( "Бастиан" , "Швайнштайгер", 30, new StatSuperstar(), new PosDefensiveMidfielder() ) );
  380. germany->addPlayer( Player( "Мирослав", "Клозе" , 36, new StatOldfag() , new PosAttackingMidfielder() ) );
  381. germany->addPlayer( Player( "Марио" , "Гётце" , 22, new StatOldfag() , new PosGoalkeeper() ) );
  382. germany->addPlayer( Player( "Лукаш" , "Подольски" , 29, new StatOldfag() , new PosDefensiveMidfielder() ) );
  383.  
  384.  
  385. germany->sort();
  386. germany->output();
  387.  
  388. set< IPosition* > positions;
  389.  
  390. positions.insert( new PosGoalkeeper() );
  391. positions.insert( new PosDefender() );
  392. positions.insert( new PosMidfielder() );
  393. positions.insert( new PosDefensiveMidfielder() );
  394. positions.insert( new PosAttackingMidfielder() );
  395. positions.insert( new PosForward() );
  396.  
  397. cout << '\n';
  398.  
  399. for( auto& itr : positions )
  400. cout << itr->name() << " " << itr->id() << endl;
  401.  
  402. set< IStatus* > statuses;
  403.  
  404. statuses.insert( new StatNewbie() );
  405. statuses.insert( new StatMiddle() );
  406. statuses.insert( new StatStar() );
  407. statuses.insert( new StatSuperstar() );
  408. statuses.insert( new StatOldfag() );
  409.  
  410. cout << '\n';
  411.  
  412. for( auto& itr : statuses )
  413. cout << itr->name() << " " << itr->id() << endl;
  414.  
  415. vector< IPosition* > posVector( positions.begin(), positions.end() );
  416.  
  417. cout << '\n';
  418.  
  419. algorithms::quick_sort< IPosition* >( posVector, []( IPosition* a, IPosition* b ) -> bool { return ( a->id() > b->id() )? true : false; } );
  420.  
  421. for( auto& itr : posVector )
  422. cout << itr->name() << " " << itr->id() << endl;
  423.  
  424.  
  425. vector< IStatus* > statVector( statuses.begin(), statuses.end() );
  426.  
  427. cout << '\n';
  428.  
  429. // algorithms::quick_sort< IStatus* >( statVector, []( IStatus* a, IStatus* b ) -> bool { return ( a->id() > b->id() )? true : false; } );
  430.  
  431. for( auto& itr : statVector )
  432. cout << itr->name() << " " << itr->id() << endl;
  433.  
  434.  
  435.  
  436.  
  437. cout << '\n';
  438.  
  439. Game::getInstance();
  440.  
  441.  
  442. return 0;
  443. }
  444.  
Runtime error #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
Бастиан Швайнштайгер 30 лет опорный полузащитник суперзвезда
Мануэль Нойер 28 лет вратарь звезда
Рон Цилер 25 лет вратарь новичок
Филипп Лам 32 года защитник звезда
Месут Озил 26 лет опорный полузащитник суперзвезда
Тонн Кросс 23 года вратарь суперзвезда
Мирослав Клозе 36 лет атакующий полузащитник старожил
Лукаш Подольски 29 лет опорный полузащитник старожил
Марио Гётце 22 года вратарь старожил

вратарь 1
защитник 2
полузащитник 3
опорный полузащитник 4
атакующий полузащитник 5
нападающий 6

новичок 1
середняк 2
звезда 3
суперзвезда 4
старожил 5