Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.44 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. //structura pentru jucator
  6. typedef struct Player
  7. {
  8. char *last_name;
  9. char *first_name;
  10. int score;
  11. } Player;
  12.  
  13. //structura pentru tara
  14. typedef struct Country
  15. {
  16. char *name;
  17. int nr_players;
  18. int global_score;
  19. Player *players;
  20. struct Country* next;
  21. struct Country* prev;
  22. } Country;
  23.  
  24. typedef struct Queue
  25. {
  26. Country *front;
  27. Country *rear;
  28. } Q;
  29.  
  30. typedef struct root
  31. {
  32. Player* jucator;
  33. struct root *left;
  34. struct root *right;
  35. } root;
  36.  
  37. typedef struct stackTree// structura pentru nod din stiva
  38. {
  39. root *treeNode;
  40. struct stackTree *next;
  41. } stackTree;
  42.  
  43. //structura pentru stiva
  44. /*typedef struct stackNode
  45. {
  46. Country countryElem;
  47. struct stackNode *next;
  48. }stackNode;
  49. */
  50. /*typedef struct nodeCountry
  51. {
  52. Country country;
  53. struct nodeCountry* next;
  54. struct nodeCountry* prev;
  55. } NC;*/
  56.  
  57. void add(Country *head, FILE** f); //functie de adaugare nod
  58. void FiletoList(Country **head, int* noCountries); // functie de deschidere fisier + aflarea numarului de tari + facut nodurile cu add
  59. double countryAverage(Country *head); // pentru a afla media scorurilor jucatorilor
  60. double findMinimum(Country *head); // functie de gasit media aritmetica cea mai mica
  61. void removeCountry(Country *head, int *noCountries); // functie de eliminat o tara din lista
  62. char* cerinte(); // functie pentru citirea cerintelor
  63. void printList(Country *head); // functie pentru afisarea elementelor din lista
  64. int bitCount(int n); // functie de calculat numarul de biti in binary al unui numar, necesar pentru a vedea daca un numar este putere a lui 2.
  65. void push(Country **top, Country added); // functia de adaugat elemente in varful stivei
  66. int isEmpty(Country *top); // functie ce verifica daca e plina/goala stiva
  67. Country* ListToStack(Country* head, Country** top); // functie ce imi pune elementele din lista in stiva
  68. void printStack(Country *top, int noCountries); // functie de afisare a stivei
  69. void deleteList(Country **head); // functie pentru stergerea listei inlantuite
  70. Q *createQueue(); // functie ce imi creeaza o coada
  71. void enQueue(Q *q, Country country); // functie ce imi adauga element in coada
  72. void playGames(Country **top,Q *queue, Country **winnerStack, int *noCountries); // functia ce baga elementele din stiva initiala in coada, face meciurile, le baga in stiva winner
  73. void winnertoTree(Country **winnerStack, root *node, int noCountries);
  74. void delTop(Country **top, int *remCountries); // functie de eliminat top-ul stack-ului
  75. void delTopnoCountries(Country** top, int remCountries); // functie ce elimina top-ul stack-ului, fara a modifica numarul de tari
  76. void allGames(Country *top,Q* queue,Country **winnerStack,int *noCountries,root** node); //functie in care sunt calculate outcome-urile tuturor meciurilor
  77. void printWinner (Country *winnerStack,int noCountries); // afisarea echipelor castigatoare
  78. void reverseStack(Country **top, int noCountries);
  79. void winnertoInit(Country **initTop,Country **winnerTop, int noCountries); // functie ce imi pune elementele din stiva winner in stiva initiala
  80. FILE *openAppend(); // functie pentru deschiderea unui fisier cu rol de append
  81. FILE *openWrite(); // functie pentru deschiderea unui fisier cu rol de write
  82. root* minValue(root* node); // functie de aflarea valorii minime dintr-un BST
  83. root* deleteNode(root* node,Player *toDelete); // functie ce sterge un nod din BST
  84. root* InsertNode(root* node,Player *toAdd); // functie ce adauga un element in BST
  85. root* changeScores(root* node,Player *toChange); // functie ce imi actualizeaza valorile din BST
  86. void printClasament(root * node); // functie pentru afisarea clasamentului (cerinta 4)
  87. void printDecOrder(root *node, FILE *fout);
  88. root* BTtoBST(root** node);
  89. stackTree* BTtoStack(root* node);
  90. root* StacktoBST (stackTree* top);
  91. void deleteBT(root *node);
  92. int isEmptyTree(stackTree *top); // functie ce verifica daca e plina/goala stiva pentru tree
  93.  
  94.  
  95.  
  96. int main()
  97. {
  98. FILE *input, *output;
  99. Country *head=NULL, *stackTop=NULL, *winnerStack=NULL;
  100. Q *q;
  101. root* BST = NULL;
  102. int noCountries;
  103. char *tasks=cerinte();
  104. FiletoList(&head,&noCountries);
  105. removeCountry(head,&noCountries);
  106. printList(head);
  107. stackTop = ListToStack(head,&stackTop);
  108. stackTop->prev->next = NULL;
  109. allGames(stackTop,q,&winnerStack,&noCountries,&BST);
  110. printClasament(BST);
  111. printf("%d",noCountries);
  112. return 0;
  113. }
  114.  
  115.  
  116. void printList(Country* head)
  117. {
  118. FILE *fout = openWrite();
  119. Country *travel=head->next;
  120. int i;
  121. while(travel != head)
  122. {
  123. fprintf(fout,"%s\n",travel->name);
  124. travel = travel->next;
  125. }
  126. fclose(fout);
  127.  
  128. }
  129.  
  130. void printStack(Country *top, int noCountries)
  131. {
  132. Country *traveler = top;
  133. int i,j;
  134. for (i=0 ; i<noCountries; i++)
  135. {
  136. printf("%s\n",traveler->name);
  137. traveler = traveler->next;
  138. }
  139.  
  140. }
  141.  
  142. char* cerinte() // functie pentru citirea cerintelor
  143. {
  144. FILE *fcerinte;
  145. int i;
  146. char *c = (char*)malloc(5* sizeof(char)); // 5*sizeof(char) pentru ca sunt 5 cerinte
  147. if((fcerinte=fopen("cerinte.in","r"))==NULL)
  148. {
  149. printf("Eroare la deschiderea cerinte.in pentru citire");
  150. exit(55);
  151. }
  152.  
  153. for(i=0; i<5; i++)
  154. fscanf(fcerinte,"%c %[ ]",&c[i]);
  155. fclose(fcerinte);
  156. return c;
  157. }
  158.  
  159. void add(Country *head, FILE** f) //functie de adaugare nod
  160. {
  161. Country *newNode, *tail=head->next;
  162. char str[BUFSIZ];
  163.  
  164. newNode=(Country*)malloc(sizeof(Country)); //creez un nou nod
  165. fscanf(*f,"%d %s",&newNode->nr_players, str); // citesc din fisier numarul de jucatori si numele tarii(stochez numele tarii intr-un auxiliar)
  166. newNode->name=malloc(strlen(str)+1); //alocare pentru numele tarii
  167. strcpy(newNode->name,str);
  168. newNode->global_score=0; //by default, scorul global initial este 0;
  169. newNode->players=malloc(sizeof(Player)*newNode->nr_players);
  170. for(int i=0 ; i<newNode->nr_players ; i++)
  171. {
  172. fscanf(*f,"%s",str); //citesc numele de familie la fel cum am facut cu numele tarii
  173. newNode->players[i].last_name=malloc(strlen(str)+1);
  174. strcpy(newNode->players[i].last_name,str);
  175.  
  176. fscanf(*f,"%s",str); // procedez la fel si pentru prenume
  177. newNode->players[i].first_name=malloc(strlen(str)+1);
  178. strcpy(newNode->players[i].first_name,str);
  179.  
  180. fscanf(*f, "%d", &newNode->players[i].score); //scorul jucatorului
  181. }
  182. while(tail->next != head)
  183. tail=tail->next; //ma deplasez spre 'finalul' listei
  184.  
  185. //adaug noul nod la final si fac legaturile next/prev corespunzatoare
  186. tail->next=newNode;
  187. newNode->next=head;
  188. newNode->prev=tail;
  189. head->prev=newNode;
  190. }
  191.  
  192.  
  193. void FiletoList(Country **head, int* noCountries) // functie de deschidere fisier + aflarea numarului de tari + facut nodurile cu add
  194. {
  195. FILE *finput;
  196. if((finput=fopen("date.in","r"))==NULL)
  197. {
  198. printf("Eroare la deschiderea date.in pentru citire");
  199. exit(55);
  200. }
  201. fscanf(finput,"%d",noCountries);
  202.  
  203. (*head)=(Country*)malloc(sizeof(Country));
  204. (*head)->next=(*head); //lista circulara dublu inlantuita
  205. (*head)->prev=(*head);
  206.  
  207. for(int i=0; i<(*noCountries); i++) // stim numarul de tari, deci stim cate noduri avem de creat cu add
  208. add(*head,&finput);
  209. fclose(finput);
  210. }
  211.  
  212. double countryAverage(Country *head) // media scorurilor jucatorilor unei tari
  213. {
  214. double mean=0;
  215. Country* traveler=head;
  216. for(int i=0 ; i<traveler->nr_players; i++)
  217. {
  218. mean+=traveler->players[i].score;
  219. }
  220. mean=mean/traveler->nr_players;
  221. return mean;
  222. }
  223.  
  224. double findMinimum(Country *head) // functie de gasit media aritmetica cea mai mica
  225. {
  226. double minimum=0;
  227. Country *headcopy=head->next;
  228. minimum=countryAverage(headcopy);
  229. headcopy=headcopy->next;
  230. while(headcopy!=head)
  231. {
  232. if(countryAverage(headcopy)<minimum)
  233. minimum=countryAverage(headcopy);
  234. headcopy=headcopy->next;
  235. }
  236. return minimum;
  237. }
  238.  
  239. void removeCountry(Country *head, int *noCountries) // functie de eliminat o tara
  240. {
  241. Country *traveler= head->next, *temp;
  242.  
  243.  
  244. while( bitCount(*noCountries)!=1 ) // verific daca numarul de tari e o puterea a lui 2
  245. {
  246. traveler=head->next;
  247. while(traveler!=head)
  248. {
  249. if(countryAverage(traveler)==findMinimum(head))
  250. {
  251. temp=traveler;
  252. if(traveler->next != head)
  253. (traveler->next)->prev=traveler->prev;
  254. else
  255. {
  256. (traveler->prev)->next=head;
  257. head->prev=traveler->prev;
  258. }
  259. if(traveler->prev!= head)
  260. (traveler->prev)->next=traveler->next;
  261. else
  262. {
  263. (traveler->next)->prev=head;
  264. head->next=traveler->next;
  265. }
  266. (*noCountries)--; // de fiecare data cand elimin o tara decrementez numarul de tari existente
  267.  
  268. free(temp);
  269. break;
  270. }
  271.  
  272. traveler=traveler->next;
  273. }
  274. }
  275. }
  276. int bitCount(int n) // functie pentru a verifica daca un numar e putere a lui 2. Un numar e putere a lui 2 daca in binary este format dintr-un singur bit 1
  277. {
  278. int count=0;
  279. while(n>0)
  280. {
  281. if(n & 1)
  282. ++count;
  283. n >>=1;
  284. }
  285. return count;
  286. }
  287.  
  288. void push(Country **top,Country added) // functie de adaugat un element in varful stivei
  289. {
  290. Country *newNode = (Country*)malloc(sizeof(Country));
  291. (*newNode) = added;
  292. newNode->next = (*top);
  293. (*top) = newNode;
  294.  
  295. }
  296.  
  297. int isEmpty(Country *top) // functie ce verifica daca e plina/goala stiva
  298. {
  299. return top==NULL;
  300. }
  301.  
  302. int isEmptyQueue(Q* queue)
  303. {
  304. return(queue->front == NULL);
  305. }
  306.  
  307. void delTop(Country** top, int *remCountries) // functie de sters elementul din varful stivei, modific si numarul tarilor. ex: cand pun tarile din stiva initiala in stiva winners
  308. {
  309. if(*remCountries == 0)
  310. (*top) = NULL;
  311. else
  312. {
  313. Country *temp = (*top);
  314. Country *aux = temp->next;
  315.  
  316. if(aux != NULL)
  317. (*top) = (*top)->next;
  318. else
  319. (*top) = NULL;
  320.  
  321. free(temp);
  322. (*remCountries)--;
  323. }
  324.  
  325. }
  326. void delTopnoCountries(Country** top, int remCountries) // functie de sters elementul din varful stivei. nu modific numarul de tari (ex: cand pun tarile din winners in stiva initiala)
  327. {
  328. if(remCountries == 0)
  329. (*top) = NULL;
  330. else
  331. {
  332. Country *temp = (*top);
  333. Country *aux = temp->next;
  334.  
  335. if(aux != NULL)
  336. (*top) = (*top)->next;
  337. else
  338. (*top) = NULL;
  339.  
  340. free(temp);
  341. }
  342.  
  343. }
  344.  
  345. Country* ListToStack(Country* head, Country** top) // functie de pus elementele din lista in stiva
  346. {
  347. Country *traveler = head->next;
  348. while(traveler != head)
  349. {
  350. push(top,(*traveler));
  351. traveler = traveler->next;
  352. }
  353. //traveler->next = NULL;
  354. //push(top, (*traveler));
  355. //deleteList(&head);
  356. return (*top);
  357. }
  358.  
  359. void deleteList(Country **head) //functie de stergere a listei
  360. {
  361. Country *temp = (*head)->next, *aux = temp;
  362. while(temp != (*head))
  363. {
  364. temp = temp->next;
  365. free(aux);
  366. aux = temp;
  367. }
  368. free(head);
  369. }
  370.  
  371. Q *createQueue() // functie ce imi creeaza o coada
  372. {
  373. Q *q;
  374. q = (Q*)malloc(sizeof(Q));
  375. if(q==NULL)
  376. return NULL;
  377.  
  378. q->front = q->rear = NULL;
  379. return q;
  380. }
  381.  
  382. void enQueue(Q *q, Country country) // functie ce imi adauga element in coada
  383. {
  384. Country *newNode = (Country*)malloc(sizeof(Country));
  385. *newNode = country;
  386. newNode->next = NULL;
  387.  
  388. //adaugam noul nod la finalul cozii
  389. if(q->rear==NULL) // daca nu avem niciun nod in coada
  390. q->rear = newNode;
  391. else
  392. {
  393. (q->rear)->next = newNode;
  394. q->rear= newNode;
  395. }
  396. if (q->front == NULL)
  397. q->front = q->rear; // daca e singurul element din coada
  398. }
  399.  
  400. void deleteQueue(Q *queue) // functie ce sterge o coada
  401. {
  402. Country *aux;
  403. while (!isEmptyQueue(queue))
  404. {
  405. aux = queue->front;
  406. queue->front = queue->front->next;
  407. free(aux);
  408. }
  409. queue->rear = NULL;;
  410. }
  411.  
  412. void playGames(Country **top,Q *queue, Country **winnerStack,int *noCountries) // in aceasta functie pun elementele din stiva initiala in coada de meciuri, le pun in stiva winner
  413. {
  414.  
  415. int i,j,initialCountries=(*noCountries);
  416. int firstLocal=0, secondLocal=0; // variabile pentru scorurile locale ale celor doua tari
  417. Country *firstCountry, *secondCountry;
  418. firstCountry=malloc(sizeof(Country));
  419. secondCountry=malloc(sizeof(Country));
  420. //queue = createQueue();
  421.  
  422. while(!isEmpty(*top)) // cat timp exista elemente in stiva initiala, iau primele doua tari si le pun in coada
  423. {
  424. FILE *fout = openAppend();
  425. queue = createQueue();
  426. (*firstCountry)=(**top);
  427. delTop(&(*top),&initialCountries);
  428. (*secondCountry)=(**top);
  429. delTop(&(*top),&initialCountries);
  430. enQueue(queue,(*firstCountry));
  431. enQueue(queue,(*secondCountry));
  432.  
  433. fprintf(fout,"\n%s %d ----- %s %d",queue->front->name,queue->front->global_score,queue->rear->name,queue->rear->global_score);
  434.  
  435. for(i=0 ; i<queue->front->nr_players ; i++)
  436. {
  437. for(j=0 ; j<queue->rear->nr_players ; j++)
  438. {
  439. fprintf(fout,"\n%s %s %d vs %s %s %d",queue->front->players[i].last_name,queue->front->players[i].first_name,queue->front->players[i].score,queue->rear->players[j].last_name,queue->rear->players[j].first_name,queue->rear->players[j].score);
  440. if(queue->front->players[i].score == queue->rear->players[j].score) // daca este egalitate intre cei doi jucatori
  441. {
  442. queue->front->players[i].score += 2;
  443. queue->rear->players[j].score += 2;
  444. firstLocal++;
  445. secondLocal++;
  446. }
  447. else if (queue->front->players[i].score < queue->rear->players[j].score) // daca al doilea jucator castiga
  448. {
  449. queue->rear->players[j].score += 5;
  450. secondLocal += 3;
  451. }
  452. else // daca primul jucator castiga
  453. {
  454. queue->front->players[i].score += 5;
  455. firstLocal += 3;
  456. }
  457.  
  458. }
  459. }
  460. fprintf(fout,"\n");
  461.  
  462. queue->front->global_score += firstLocal; // dupa terminarea meciurilor adaugam scorul local final la cel global
  463. queue->rear->global_score += secondLocal;
  464.  
  465.  
  466.  
  467. if(firstLocal>=secondLocal) // adaugam in stiva Winner tara cu punctajul local mai mare
  468. push(&(*winnerStack),*(queue->front));
  469. else
  470. push(&(*winnerStack),*(queue->rear));
  471.  
  472.  
  473.  
  474.  
  475. deleteQueue(queue); // meciurile s-au terminat, nu mai avem nevoie de coada
  476. firstLocal = 0;
  477. secondLocal = 0;
  478. fclose(fout);
  479. }
  480.  
  481. (*noCountries) /= 2; // dupa ce se termina toate meciurile dintr-o etapa numarul de tari se va injumatati
  482. }
  483.  
  484. void winnertoTree(Country **winnerStack, root *node, int noCountries)
  485. {
  486.  
  487. if(noCountries == 4 ) // daca sunt ultimele 4 tari ramase trebuie doar introduse in BST
  488. {
  489. Country *temp = *winnerStack;
  490. while(temp != NULL)
  491. {
  492. int i;
  493. for(i=0 ; i<temp->nr_players; i++)
  494. {
  495. node = InsertNode(node,&temp->players[i]);
  496. }
  497. temp = temp->next;
  498. }
  499. }
  500. if(noCountries == 2 || noCountries == 1) // la ultimele 2/1 tari ramase, BST-ul initial devine BT
  501. {
  502. node = BTtoBST(&node);
  503. }
  504. }
  505.  
  506. root* BTtoBST(root** node)
  507. {
  508. if(*node == NULL)
  509. return NULL;
  510. root* traveler = *node;
  511. root* aux = NULL;
  512. BTtoBST(&(*node)->left);
  513. aux = InsertNode(aux,traveler->jucator);
  514. BTtoBST(&(*node)->right);
  515.  
  516.  
  517. deleteBT(*node);
  518. return aux;
  519. }
  520.  
  521.  
  522. /*void BTtoBST(root** node) // elementele din BT sunt puse intr-o stiva, dupa care adaugam top-ul intr-un arbore a.i. sa fie BST
  523. {
  524. if(*node == NULL)
  525. return;
  526. stackTree *sTree = NULL;
  527. sTree = BTtoStack(*node);
  528. deleteBT(*node); // stergem binary tree-ul initial
  529. *node = StacktoBST(sTree);
  530. }*/
  531.  
  532. void pushTree(stackTree** top, Player* node) // functie push pentru elementele din arbore
  533. {
  534. stackTree* toAdd = (stackTree*)malloc(sizeof(stackTree));
  535. toAdd->treeNode->jucator = node;
  536. toAdd->next = *top;
  537. *top = toAdd;
  538. }
  539.  
  540. stackTree* BTtoStack(root* node) //parcurgere inorder a arborelui, dandu-i push in stiva
  541. {
  542. stackTree *S = NULL;
  543. if(node == NULL)
  544. return NULL;
  545. S = BTtoStack(node->left);
  546. pushTree(&S,node->jucator);
  547. S = BTtoStack(node->right);
  548. return S;
  549. }
  550.  
  551. int isEmptyTree(stackTree *top) // functie ce verifica daca e plina/goala stiva pentru tree
  552. {
  553. return top==NULL;
  554. }
  555.  
  556. root* StacktoBST (stackTree* top) // pune elementele din stiva intr-un alt arbore
  557. {
  558. root *BST = NULL;
  559. while(!isEmptyTree(top))
  560. {
  561. InsertNode(BST,top->treeNode->jucator);
  562. top = top->next;
  563. }
  564. return BST;
  565. }
  566.  
  567.  
  568.  
  569. void deleteBT(root *node) // stergerea arborelui, intrucat 'schimb' arborele
  570. {
  571. if(!node)
  572. return;
  573. deleteBT(node->left);
  574. deleteBT(node->right);
  575. free(node);
  576. }
  577.  
  578. void allGames(Country *top,Q *queue, Country **winnerStack,int *noCountries, root **node)
  579. {
  580.  
  581. int i=1;
  582. while((*noCountries)>1)
  583. {
  584. FILE *fout = openAppend();
  585. fprintf(fout,"\n===== ETAPA %d =====\n",i);
  586. fclose(fout);
  587. playGames(&top,queue,winnerStack,noCountries);
  588. printWinner(*winnerStack,(*noCountries));
  589. winnertoTree(winnerStack, *node, *noCountries);
  590. winnertoInit(&top,winnerStack,(*noCountries));
  591. i++;
  592. }
  593. }
  594.  
  595. void printWinner (Country *winnerStack,int noCountries) // functie ce printeaza stiva winner
  596. {
  597. FILE *fout = openAppend();
  598. Country *traveler = winnerStack;
  599. int i;
  600. fprintf(fout,"\n=== WINNER ===\n");
  601. for (i=0 ; i<noCountries; i++)
  602. {
  603. fprintf(fout,"%s --- %d\n",traveler->name,traveler->global_score);
  604. traveler = traveler->next;
  605. }
  606. fclose(fout);
  607. }
  608.  
  609. void winnertoInit(Country **initTop,Country **winnerTop, int noCountries)
  610. {
  611. while(!isEmpty(*winnerTop))
  612. {
  613. push(&(*initTop),*(*winnerTop));
  614. delTopnoCountries(winnerTop,noCountries);
  615. }
  616.  
  617. }
  618.  
  619. FILE *openWrite() // functie pentru deschiderea unui fisier cu rol de write
  620. {
  621. FILE *fout;
  622. if((fout = fopen("rezultate.out","w")) == NULL)
  623. {
  624. fprintf(fout,"eroare la deschiderea rezultate.out pentru scriere");
  625. exit(-55);
  626. }
  627. return fout;
  628. }
  629.  
  630. FILE *openAppend() // functie pentru deschiderea unui fisier cu rol de append
  631. {
  632. FILE *fout;
  633. if((fout = fopen("rezultate.out","a")) == NULL)
  634. {
  635. fprintf(fout,"eroare la deschiderea rezultate.out pentru append");
  636. exit(-55);
  637. }
  638. return fout;
  639. }
  640.  
  641. root* newNode (Player *toAdd) // functie ce imi creeaza un nou nod
  642. {
  643. root *node = (root*)malloc(sizeof(root));
  644. node->jucator = toAdd;
  645. node->left = node->right = NULL;
  646. return node;
  647. }
  648.  
  649. root* InsertNode(root* node,Player *toAdd) // functie ce insereaza un nou nod in BST
  650. {
  651. if ( node == NULL )
  652. return newNode(toAdd);
  653. if(toAdd->score == node->jucator->score) // cazul in care au acelasi scor
  654. {
  655. if(strcmp(toAdd->last_name,node->jucator->last_name)<0) // daca cel pe care il adaugam are numele mai mic dpdv lexicografic
  656. {
  657. node->jucator = toAdd;
  658. }
  659. else if(!strcmp(toAdd->last_name,node->jucator->last_name)) // daca au acelasi nume dpdv lexicografic
  660. {
  661. if(strcmp(toAdd->first_name,node->jucator->first_name)<0) // comparam prenumele si il adaugam daca are numele mai mic decat nodul deja prezent, altfel in lasam asa
  662. node->jucator = toAdd;
  663. else
  664. return node;
  665. }
  666. else
  667. return node;
  668.  
  669. }
  670.  
  671. else if(toAdd->score < node->jucator->score)
  672. node->left = InsertNode(node->left, toAdd);
  673. else if(toAdd->score > node->jucator->score)
  674. node->right = InsertNode(node->right,toAdd);
  675. return node;
  676. }
  677.  
  678. root* minValue(root* node) // functie ce cauta minimul dintr-ul BST
  679. {
  680. root* traveler = node;
  681. while(traveler->left != NULL)
  682. traveler = traveler->left;
  683. return traveler;
  684. }
  685.  
  686. root* deleteNode(root* node,Player *toDelete) // functie ce sterge un nod din BST
  687. {
  688. if(node == NULL)
  689. return node;
  690. if(!strcmp(node->jucator->first_name,toDelete->first_name) && !strcmp(node->jucator->last_name,toDelete->last_name)) // daca a gasit jucatorul de sters (acelasi nume)
  691. {
  692. if(node->left == NULL) // daca e un singur copil
  693. {
  694. root* temp = node->right;
  695. free(node);
  696. return temp;
  697. }
  698. else if (node->right == NULL)
  699. {
  700. root *temp = node->left;
  701. free(node);
  702. return temp;
  703. }
  704. root *temp = minValue(node->right); // daca are 2 copii, gasim minimul din subarborele drept pentru a schimba pozitiile
  705. node->jucator = temp->jucator;
  706. node->right = deleteNode(node->right,temp->jucator);
  707. }
  708. node->left = deleteNode(node->left,toDelete);
  709. node->right = deleteNode(node->right,toDelete);
  710. return node;
  711. }
  712.  
  713. root* changeScores(root* node,Player *toChange) // Functie ce actualizeaza BST-ul dupa scorul jucatorilor si reface legaturile corespunzatoare
  714. {
  715. node = deleteNode(node,toChange);
  716. node = InsertNode(node,toChange);
  717. return node;
  718. }
  719.  
  720. void printDecOrder(root* node, FILE* fout) // daca parcurgem DRS arborele, vom obtine elementele in ordine descrescatoare
  721. {
  722. if (node == NULL)
  723. return;
  724. printDecOrder(node->right,fout);
  725. fprintf(fout,"%s %s %d\n",node->jucator->last_name,node->jucator->first_name,node->jucator->score);
  726. printDecOrder(node->left,fout);
  727. }
  728.  
  729. void printClasament(root * node) // functie pentru afisarea clasamentului (cerinta 4)
  730. {
  731. FILE *fout = openAppend();
  732. fprintf(fout,"\n====== CLASAMENT JUCATORI ======\n");
  733. printDecOrder(node,fout);
  734. fclose(fout);
  735. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement