Advertisement
FoxTuGa

FootballChampionship

Apr 24th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. /*  Author: Leandro Soares
  2. School: INETE
  3. Date:   24-04-2012
  4. Time:   15:10   */
  5.  
  6. /* IMCOMPLETO DEVIDO AO BARULHO NA AULA */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12.  
  13. struct tagGolos {
  14.     char player[122];
  15.     int golos;
  16.     struct tagGolos *pnext;
  17.     struct tagGolos *pprev;
  18. };
  19.  
  20. void Linha_to_Struct(struct tagGolos **pHead, char buffer[]);
  21. void InsertNode(struct tagGolos **phead, struct tagGolos *pnew);
  22. struct tagGolos *NewNode(void);
  23. int isAlphaSpec(char Char);
  24. void CountGoals(struct tagGolos **pHead, struct tagGolos **pHeadSec);
  25. int ProcGoalperPlayer(struct tagGolos *pAux, char *Player);
  26. void ApagarPlayer(struct tagGolos *pStruct);
  27.  
  28. int main() {
  29.     FILE * pInput;
  30.     char buffer[121];
  31.     struct tagGolos *pHead, *pHeadSec;
  32.  
  33.     pHead = NULL;
  34.     pInput = fopen("input.txt", "r");
  35.  
  36.     while( fgets(buffer, 120, pInput) != NULL ) {
  37.         Linha_to_Struct(&pHead, buffer);
  38.     }
  39.  
  40.     CountGoals(&pHead, &pHeadSec);
  41.     fclose(pInput);
  42.     return 0;
  43. }
  44.  
  45. void Linha_to_Struct(struct tagGolos **pHead, char buffer[]) {
  46.     int idx;
  47.     char NameAux[120];
  48.     struct tagGolos *pnew;
  49.  
  50.     pnew = NewNode();
  51.  
  52.     for(idx=0; isAlphaSpec(buffer[idx]) != 0 ; idx++) {
  53.         NameAux[idx] = buffer[idx];
  54.     }
  55.  
  56.     NameAux[idx-1] = '\0';
  57.     strcpy(pnew->player, NameAux);
  58.     InsertNode(&(*pHead), pnew);
  59. }
  60. int isAlphaSpec(char Char) {
  61.     if ( ( Char >= 'A' && Char <= 'Z' ) || ( Char >= 'a' && Char <= 'z' ) || Char == ' ' )
  62.         return Char;
  63.     return 0;
  64. }
  65. void InsertNode(struct tagGolos **phead, struct tagGolos *pnew) {
  66.     struct tagGolos *paux;
  67.     paux = (*phead);
  68.  
  69.     if( (*phead) != NULL ) {
  70.         (*phead)->pprev = pnew;
  71.         pnew->pnext = (*phead);
  72.     }
  73.     (*phead) = pnew;
  74. }
  75. struct tagGolos *NewNode(void) {
  76.     struct tagGolos *Dados;
  77.  
  78.     Dados = (struct tagGolos*) malloc(sizeof(struct tagGolos));
  79.  
  80.     Dados->pnext = NULL;
  81.     Dados->pprev = NULL;
  82.  
  83.     return Dados;
  84. }
  85. void CountGoals(struct tagGolos **pHead, struct tagGolos **pHeadSec) {
  86.     struct tagGolos *pnew, *pAux, **pAux2;
  87.     int goals;
  88.  
  89.     pAux = (*pHead);
  90.  
  91.     while( pAux != NULL ) {
  92.         pnew = NewNode();
  93.         pAux2 = pAux;
  94.         pnew->golos = ProcGoalperPlayer(&pAux2, pAux->player);
  95.         strcpy(pnew->player, pAux->player);
  96.  
  97.         pAux = pAux->pnext;
  98.     }
  99. }
  100.  
  101. int ProcGoalperPlayer(struct tagGolos **pAux, char *Player) {
  102.     int goals;
  103.     struct tagGolos *pAux2;
  104.     goals = 0;
  105.    
  106.     while( (*pAux) != NULL ) {
  107.         if( !strcmp(Player, (*pAux)->player) ) {
  108.             goals++;
  109.             pAux2 = pAux;
  110.             ApagarPlayer(pAux);
  111.         }
  112.         pAux = pAux2;
  113.         (*pAux) = (*pAux)->pnext;
  114.     }
  115. }
  116.  
  117. void ApagarPlayer(struct tagGolos **pStruct) {
  118.     struct tagGolos *pAux;
  119.     pAux = (*pStruct)->pnext;
  120.  
  121.     (*pStruct)->pprev = NULL;
  122.     (*pStruct)->pnext = (*pStruct)->pnext;
  123.  
  124.     pStruct = pAux;
  125.     free(*pStruct);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement