Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct _squadra{
- char *nome;
- int punti;
- int segnati;
- int subiti;
- struct _squadra *next;
- };
- void inserisci_squadra(char *nome_squadra, struct _squadra **head);
- void aggiorna_dopo_partita(struct _squadra **head, char *squadra1, char *squadra2, int gol1, int gol2);
- void retrocedi_ultime(int n);
- void stampa(struct _squadra *head);
- void inserisci_squadra(char *nome_squadra, struct _squadra **head)
- {
- struct _squadra *new = malloc(sizeof(struct _squadra));
- if(new == NULL){
- printf("No memory available.\n");
- exit(EXIT_FAILURE);
- }
- new->nome = nome_squadra;
- new->punti = 0;
- new->segnati = 0;
- new->subiti = 0;
- struct _squadra *previous = NULL;
- struct _squadra *n = *head;
- if(n != NULL){
- while(n !=NULL){
- previous = n;
- n = n->next;
- }
- n = new;
- new->next = NULL;
- previous->next = new;
- }else{
- new->next = *head;
- *head = new;
- }
- }
- void aggiorna_dopo_partita(struct _squadra **head, char *squadra1, char *squadra2, int gol1, int gol2)
- {
- struct _squadra *sq1 = malloc(sizeof(struct _squadra));
- struct _squadra *sq2 = malloc(sizeof(struct _squadra));
- struct _squadra *previous;
- struct _squadra *current;
- /*Assegno punti e gol segnati/subiti*/
- sq1 = *head;
- while(sq1->nome != squadra1){
- sq1 = sq1->next;
- }
- sq1->segnati += gol1;
- sq1->subiti += gol2;
- if(gol1>gol2)
- sq1->punti += 3;
- if(gol1==gol2)
- sq1->punti += 1;
- sq2 = *head;
- while(sq2->nome != squadra2){
- sq2 = sq2->next;
- }
- sq2->segnati += gol2;
- sq2->subiti += gol1;
- if(gol2>gol1)
- sq2->punti += 3;
- if(gol2==gol1)
- sq2->punti += 1;
- /*Aggiorno la posizione delle squadre in classifica*/
- current = *head;
- previous = NULL;
- while(current != NULL && current->punti > sq1->punti){
- printf("%d\n", current->punti);
- printf("%d\n", sq1->punti);
- previous = current;
- current = current->next;
- }
- if(previous == NULL){
- sq1->next = *head;
- *head = sq1;
- }else{
- previous->next = sq1;
- sq1->next = current;
- }
- }
- void retrocedi_ultime(int n)
- {
- }
- void stampa(struct _squadra *head)
- {
- struct _squadra *n = head;
- while(n != NULL){
- printf("%10s\tPunti: %d\tSegnati: %d\tSubiti: %d\t\n",
- n->nome, n->punti, n->segnati, n->subiti);
- n = n->next;
- }
- printf("\n");
- }
- int main(void)
- {
- struct _squadra *head = NULL;
- inserisci_squadra("Atalanta", &head);
- inserisci_squadra("Juventus", &head);
- inserisci_squadra("Roma", &head);
- inserisci_squadra("Venezia", &head);
- aggiorna_dopo_partita(&head, "Atalanta", "Juventus", 3, 1);
- aggiorna_dopo_partita(&head, "Roma", "Atalanta", 2, 4);
- aggiorna_dopo_partita(&head, "Juventus", "Roma", 1, 1);
- aggiorna_dopo_partita(&head, "Roma", "Venezia", 0, 5);
- stampa(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment