stefano_knd

Untitled

Feb 5th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct _squadra{
  6.     char *nome;
  7.     int punti;
  8.     int segnati;
  9.     int subiti;
  10.     struct _squadra *next;
  11. };
  12.  
  13. void inserisci_squadra(char *nome_squadra, struct _squadra **head);
  14. void aggiorna_dopo_partita(struct _squadra **head, char *squadra1, char *squadra2, int gol1, int gol2);
  15. void retrocedi_ultime(int n);
  16. void stampa(struct _squadra *head);
  17.  
  18. void inserisci_squadra(char *nome_squadra, struct _squadra **head)
  19. {
  20.  
  21.     struct _squadra *new = malloc(sizeof(struct _squadra));
  22.     if(new == NULL){
  23.         printf("No memory available.\n");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.  
  27.     new->nome = nome_squadra;
  28.     new->punti = 0;
  29.     new->segnati = 0;
  30.     new->subiti = 0;
  31.  
  32.     struct _squadra *previous = NULL;
  33.     struct _squadra *n = *head;
  34.     if(n != NULL){
  35.         while(n !=NULL){
  36.             previous = n;
  37.             n = n->next;
  38.         }
  39.         n = new;
  40.         new->next = NULL;
  41.         previous->next = new;
  42.     }else{
  43.         new->next = *head;
  44.         *head = new;
  45.     }  
  46. }
  47.  
  48. void aggiorna_dopo_partita(struct _squadra **head, char *squadra1, char *squadra2, int gol1, int gol2)
  49. {
  50.  
  51.     struct _squadra *sq1 = malloc(sizeof(struct _squadra));
  52.     struct _squadra *sq2 = malloc(sizeof(struct _squadra));
  53.     struct _squadra *previous;
  54.     struct _squadra *current;  
  55.     /*Assegno punti e gol segnati/subiti*/
  56.    
  57.     sq1 = *head;
  58.     while(sq1->nome != squadra1){
  59.         sq1 = sq1->next;
  60.     }
  61.     sq1->segnati += gol1;
  62.     sq1->subiti += gol2;
  63.     if(gol1>gol2)
  64.         sq1->punti += 3;
  65.     if(gol1==gol2)
  66.         sq1->punti += 1;
  67.  
  68.     sq2 = *head;
  69.     while(sq2->nome != squadra2){
  70.         sq2 = sq2->next;
  71.     }
  72.     sq2->segnati += gol2;
  73.     sq2->subiti += gol1;
  74.     if(gol2>gol1)
  75.         sq2->punti += 3;
  76.     if(gol2==gol1)
  77.         sq2->punti += 1;
  78.    
  79.     /*Aggiorno la posizione delle squadre in classifica*/
  80.    
  81.     current = *head;
  82.     previous = NULL;
  83.  
  84.     while(current != NULL && current->punti > sq1->punti){
  85.         printf("%d\n", current->punti);
  86.         printf("%d\n", sq1->punti);
  87.         previous = current;
  88.         current = current->next;
  89.     }
  90.     if(previous == NULL){
  91.         sq1->next = *head;
  92.         *head = sq1;   
  93.     }else{
  94.         previous->next = sq1;
  95.         sq1->next = current;
  96.     }
  97. }
  98.  
  99. void retrocedi_ultime(int n)
  100. {
  101.  
  102. }
  103.  
  104. void stampa(struct _squadra *head)
  105. {
  106.     struct _squadra *n = head;
  107.     while(n != NULL){
  108.         printf("%10s\tPunti: %d\tSegnati: %d\tSubiti: %d\t\n",
  109.         n->nome, n->punti, n->segnati, n->subiti);
  110.         n = n->next;
  111.     }
  112.     printf("\n");
  113. }
  114.  
  115. int main(void)
  116. {
  117.     struct _squadra *head = NULL;
  118.    
  119.     inserisci_squadra("Atalanta", &head);
  120.     inserisci_squadra("Juventus", &head);  
  121.     inserisci_squadra("Roma", &head);
  122.     inserisci_squadra("Venezia", &head);
  123.     aggiorna_dopo_partita(&head, "Atalanta", "Juventus", 3, 1);
  124.     aggiorna_dopo_partita(&head, "Roma", "Atalanta", 2, 4);
  125.     aggiorna_dopo_partita(&head, "Juventus", "Roma", 1, 1);
  126.     aggiorna_dopo_partita(&head, "Roma", "Venezia", 0, 5);
  127.     stampa(head);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment