Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. #include "BballRoster.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. BballRoster::BballRoster()
  6. {
  7. head = nullptr;
  8. tail = nullptr;
  9.  
  10.  
  11.  
  12. }
  13.  
  14. BballRoster::~BballRoster()
  15. {
  16. if(rosterEmpty()==true)
  17. {
  18. return;
  19. }
  20. else if (howManyPlayers() == 1)
  21. {
  22. delete head;
  23. return;
  24. }
  25. else {
  26. Node* p;
  27. p = head;
  28. while (p->next != nullptr)
  29. {
  30. p = p->next;
  31. delete p->prev;
  32.  
  33. }
  34.  
  35.  
  36.  
  37. delete tail;
  38.  
  39.  
  40. }
  41.  
  42.  
  43. }
  44.  
  45. BballRoster::BballRoster(const BballRoster& copy)
  46. {
  47.  
  48.  
  49.  
  50.  
  51. }
  52.  
  53.  
  54. const BballRoster& BballRoster::operator=(const BballRoster& copy)
  55. {
  56. int size = howManyPlayers();
  57. for (int i = 0; i < size; i++)
  58. {
  59. string firstname;
  60. string lastname;
  61. string value;
  62. choosePlayer(0, firstname, lastname, value);
  63. renouncePlayer(firstname, lastname);
  64. }
  65. int size2 = copy.howManyPlayers();
  66. Node* p = copy.head;
  67. while (p != nullptr)
  68. {
  69.  
  70. signPlayer(p->firstname, p->lastname, p->value);
  71. p = p->next;
  72. }
  73.  
  74. return *this;
  75. }
  76.  
  77.  
  78. bool BballRoster::rosterEmpty() const
  79. {
  80.  
  81.  
  82. if (howManyPlayers() == 0)
  83. {
  84. return true;
  85. }
  86. else
  87. {
  88. return false;
  89. }
  90.  
  91. }
  92.  
  93.  
  94. int BballRoster::howManyPlayers() const
  95. {
  96. Node* p;
  97. p = head;
  98. int i = 0;
  99.  
  100.  
  101. while (p != nullptr)
  102. {
  103.  
  104. p = p->next;
  105. i++;
  106. }
  107.  
  108.  
  109.  
  110. return i;
  111.  
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. bool BballRoster::signPlayer(const std::string& firstName, const std::string&lastName, const SomeType& value)
  119. {
  120. if (playerOnRoster(firstName, lastName) == true)
  121. {
  122. return false;
  123. }
  124. //put data into node
  125. Node* p;
  126. p = new Node;
  127. p->firstname = firstName;
  128. p->lastname = lastName;
  129. p->value = value;
  130.  
  131. //create unsorted linked list
  132. if (rosterEmpty() == true)
  133. {
  134. head = p;
  135. tail = p;
  136. head->next = nullptr;
  137. p->prev = nullptr;
  138. }
  139. else {
  140.  
  141. tail->next = p;
  142. p->prev = tail;
  143. tail = p;
  144. tail->next = nullptr;
  145.  
  146. }
  147.  
  148. //sort linked list
  149. Node* n;
  150. Node* k;
  151. string temp;
  152. string temp2;
  153. string temp3;
  154.  
  155. for (n = head->next; n != nullptr; n = n->next)
  156. {
  157. for (k = head; k != nullptr; k = k->next)
  158. {
  159. if (n->lastname < k->lastname)
  160. {
  161. temp = n->lastname;
  162. temp2 = n->firstname;
  163. temp3 = n->value;
  164.  
  165. n->lastname = k->lastname;
  166. n->firstname = k->firstname;
  167. n->value = k->value;
  168. k->lastname = temp;
  169. k->firstname = temp2;
  170. k->value = temp3;
  171. }
  172. else if (n->lastname == k->lastname)
  173. {
  174. if (n->firstname < k->firstname)
  175. {
  176. temp = n->lastname;
  177. temp2 = n->firstname;
  178. temp3 = n->value;
  179.  
  180. n->lastname = k->lastname;
  181. n->firstname = k->firstname;
  182. n->value = k->value;
  183. k->lastname = temp;
  184. k->firstname = temp2;
  185. k->value = temp3;
  186. }
  187.  
  188. }
  189. }
  190.  
  191. }
  192.  
  193.  
  194.  
  195.  
  196. return true;
  197. }
  198.  
  199. bool BballRoster::resignPlayer(const std::string& firstName, const std::string&lastName, const SomeType& value)
  200. {
  201. Node* p;
  202. p = head;
  203. if ((rosterEmpty() == true))
  204. {
  205. return false;
  206. }
  207.  
  208.  
  209. while (p != nullptr)
  210. {
  211. if (p->firstname == firstName && p->lastname == lastName)
  212. {
  213. p->value = value;
  214. return true;
  215. }
  216. p = p->next;
  217. }
  218.  
  219. return false;
  220. }
  221.  
  222. bool BballRoster::signOrResign(const std::string& firstName, const std::string&lastName, const SomeType& value)
  223. {
  224. Node* p;
  225. p = head;
  226. if ((rosterEmpty() == true))
  227. {
  228. signPlayer(firstName, lastName, value);
  229. return true;
  230. }
  231.  
  232.  
  233. while (p != nullptr)
  234. {
  235. if (p->firstname == firstName && p->lastname == lastName)
  236. {
  237. p->value = value;
  238. return true;
  239. }
  240. p = p->next;
  241. }
  242.  
  243. signPlayer(firstName, lastName, value);
  244. return true;
  245. }
  246.  
  247. bool BballRoster::renouncePlayer(const std::string& firstName, const std::string& lastName)
  248. {
  249. Node* p;
  250. p = head;
  251. if ((howManyPlayers() == 0))
  252. {
  253. return false;
  254. }
  255. int count = 0;
  256. while (p != nullptr)
  257. {
  258. count++;
  259. if (count > 1 && count < howManyPlayers())
  260. {
  261. if (p->firstname == firstName && p->lastname == lastName)
  262. {
  263. Node* temp = p->next;
  264. Node* temp2 = p->prev;
  265. temp->prev = temp2;
  266. temp2->next = temp;
  267. delete p;
  268. return true;
  269. }
  270. }
  271. else if (count == howManyPlayers() && howManyPlayers() > 1)
  272. {
  273. if (p->firstname == firstName && p->lastname == lastName)
  274. {
  275. Node* temp = p->prev;
  276. temp->next = nullptr;
  277. delete(p);
  278. return true;
  279. }
  280.  
  281. }
  282. else if (count == 1 && howManyPlayers() > 1)
  283. {
  284. if (p->firstname == firstName && p->lastname == lastName)
  285. {
  286. head = head->next;
  287. head->prev = nullptr;
  288. delete p;
  289. return true;
  290. }
  291. }
  292. else if (howManyPlayers() == 1)
  293. {
  294. if (p->firstname == firstName && p->lastname == lastName)
  295. {
  296. head = nullptr;
  297. tail = nullptr;
  298. delete p;
  299. return true;
  300. }
  301.  
  302. }
  303. p = p->next;
  304. }
  305. return false;
  306. }
  307.  
  308. bool BballRoster::playerOnRoster(const std::string& firstName, const std::string& lastName) const
  309. {
  310. if (howManyPlayers() == 0)
  311. {
  312. return false;
  313. }
  314. Node* p = head;
  315. while (p != nullptr)
  316. {
  317. if (p->firstname == firstName && p->lastname == lastName)
  318. {
  319. return true;
  320. }
  321. p = p->next;
  322. }
  323. return false;
  324.  
  325. }
  326.  
  327. bool BballRoster::lookupPlayer(const std::string& firstName, const std::string&lastName, SomeType& value) const
  328. {
  329. if (howManyPlayers() == 0)
  330. {
  331.  
  332. return false;
  333.  
  334. }
  335.  
  336. Node* p = head;
  337. while (p != nullptr)
  338. {
  339. if (p->firstname == firstName && p->lastname == lastName)
  340. {
  341. value = p->value;
  342. return true;
  343. }
  344. p = p->next;
  345.  
  346. }
  347.  
  348. return false;
  349. }
  350.  
  351. bool BballRoster::choosePlayer(int i, std::string& firstName, std::string&lastName, SomeType& value) const
  352. {
  353. if (i < howManyPlayers() && i>=0)
  354. {
  355. Node* p = head;
  356. for (int j=0; j < i; j++)
  357. {
  358. p = p->next;
  359. }
  360. firstName = p->firstname;
  361. lastName = p->lastname;
  362. value = p->value;
  363.  
  364. }
  365. else {
  366. return false;
  367.  
  368. }
  369. return true;
  370. }
  371.  
  372.  
  373. void BballRoster::swapRoster(BballRoster& other)
  374. {
  375. if (howManyPlayers() == 0 && other.howManyPlayers() == 0) // check if both are empty do nothing
  376. {
  377. return;
  378. }
  379.  
  380. Node* store = head; // store head
  381.  
  382.  
  383. head = other.head; //swap head with other head
  384. other.head = store;
  385.  
  386. store = tail; // store tail
  387.  
  388. tail = other.tail; // swap tail with other tail
  389. other.tail = store;
  390.  
  391.  
  392.  
  393. }
  394.  
  395. void BballRoster::printRoster()
  396. {
  397. Node* p = new Node;
  398. p = head;
  399. if (howManyPlayers() == 0)
  400. {
  401. return;
  402.  
  403. }
  404.  
  405.  
  406.  
  407.  
  408. while (p != nullptr)
  409. {
  410. cout << p->lastname << " " << p->firstname << " " << p->value << endl;
  411. p = p->next;
  412. }
  413. }
  414. bool joinRosters(const BballRoster & bbOne, const BballRoster & bbTwo, BballRoster & bbJoined)
  415. {
  416. int sizebb1 = bbOne.howManyPlayers();
  417. int sizebb2 = bbTwo.howManyPlayers();
  418. string firstname;
  419. string lastname;
  420. string value;
  421. string firstname2;
  422. string lastname2;
  423. string value2;
  424. int j = 0;
  425. int count = 0;
  426. if (sizebb1 == 0)
  427. {
  428. for (int i = 0; i < sizebb2; i++)
  429. {
  430. bbTwo.choosePlayer(i, firstname, lastname, value);
  431. bbJoined.signPlayer(firstname, lastname, value);
  432. }
  433. return true;
  434.  
  435. }
  436. if (sizebb2 == 0)
  437. {
  438. for (int i = 0; i < sizebb1; i++)
  439. {
  440. bbOne.choosePlayer(i, firstname, lastname, value);
  441. bbJoined.signPlayer(firstname, lastname, value);
  442. }
  443.  
  444. return true;
  445. }
  446. if (sizebb1 == 0 && sizebb2 == 0)
  447. {
  448. return true;
  449. }
  450.  
  451.  
  452. for (int i = 0; i < sizebb1; i++)
  453. {
  454. bbOne.choosePlayer(i, firstname, lastname, value);
  455. j = 0;
  456. for (j = 0; j < sizebb2; j++)
  457. {
  458. bbTwo.choosePlayer(j, firstname2, lastname2, value2);
  459. if (firstname == firstname2 && lastname == lastname2 && value != value2)
  460. {
  461. count++;
  462. j = sizebb2;
  463. }
  464. else
  465. {
  466. bbJoined.signPlayer(firstname, lastname, value);
  467. }
  468.  
  469.  
  470. }
  471.  
  472. }
  473. for (int i = 0; i < sizebb2; i++)
  474. {
  475. bbTwo.choosePlayer(i, firstname, lastname, value);
  476. j = 0;
  477. for (j = 0; j < sizebb1; j++)
  478. {
  479. bbOne.choosePlayer(j, firstname2, lastname2, value2);
  480. if (firstname == firstname2 && lastname == lastname2 && value != value2)
  481. {
  482. count++;
  483. j = sizebb1;
  484. }
  485. else
  486. {
  487. bbJoined.signPlayer(firstname, lastname, value);
  488. }
  489.  
  490. }
  491.  
  492. }
  493.  
  494. if (count == 0)
  495. {
  496. return true;
  497.  
  498. }
  499. else {
  500. return false;
  501. }
  502. }
  503.  
  504. void checkRoster(const std::string& fsearch, const std::string& lsearch, const BballRoster& bbOne, BballRoster& bbResult) {
  505.  
  506. string firstname;
  507. string lastname;
  508. string value;
  509. int sizebb = bbOne.howManyPlayers();
  510. string fs = fsearch;
  511. string ls = lsearch;
  512. bool option1 = false;
  513. bool option2 = false;
  514. bool option3 = false;
  515.  
  516.  
  517.  
  518.  
  519.  
  520. if (sizebb == 0)
  521. {
  522. return;
  523. }
  524.  
  525.  
  526. if (fs == "*" && ls == "*")
  527. {
  528. option3 = true;
  529.  
  530. }
  531. else if (fs == "*")
  532. {
  533. option1 = true;
  534.  
  535. }
  536. else if (ls == "*")
  537. {
  538. option2 = true;
  539.  
  540. }
  541.  
  542.  
  543.  
  544.  
  545. for (int i = 0; i < sizebb; i++)
  546. {
  547. bbOne.choosePlayer(i, firstname, lastname, value);
  548. if (option3 == true)
  549. {
  550. bbResult.signPlayer(firstname, lastname, value);
  551. }
  552. else if (option2 == true)
  553. {
  554. if (fsearch == firstname)
  555. {
  556. bbResult.signPlayer(firstname, lastname, value);
  557. }
  558. }
  559. else if (option1 == true)
  560. {
  561. if (lsearch == lastname)
  562. {
  563. bbResult.signPlayer(firstname, lastname, value);
  564. }
  565. }
  566. else
  567. {
  568. if (fsearch == firstname && lsearch == lastname)
  569. {
  570. bbResult.signPlayer(firstname, lastname, value);
  571. }
  572. }
  573.  
  574. }
  575.  
  576.  
  577.  
  578.  
  579. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement