Guest User

Untitled

a guest
Jan 27th, 2015
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct record
  6. {
  7.    char name[30];
  8.    int score;
  9. } record;
  10.  
  11. int score = 23;
  12. char winner[30] = "gracz";
  13.  
  14. typedef struct el_list
  15. {
  16.    record record;
  17.    struct el_list* next;
  18. } el_list;
  19.  
  20. el_list *first = NULL;
  21.  
  22. int addrecord()
  23. {
  24.    el_list *record;
  25.    record = (el_list*) malloc (sizeof(el_list));
  26.    record->next = NULL;
  27.    record->record.score = score;
  28.    strcpy(record->record.name, winner);
  29.    return record;
  30. }
  31.  
  32. void addtolist(el_list** first)
  33. {
  34.    el_list *pom, *tmp = addrecord();
  35.  
  36.    if (*first == NULL)
  37.       *first = tmp;
  38.    else if ((*first)->record.score > tmp->record.score)
  39.    {
  40.       tmp->next = *first;
  41.       *first = tmp;
  42.    }
  43.    else
  44.    {
  45.       pom = (*first);
  46.       while((pom->next != NULL) && (pom->record.score < tmp->record.score))
  47.          pom = pom->next;
  48.       tmp->next = pom->next;
  49.       pom->next = tmp;
  50.    }
  51. }
  52.  
  53. void save2file(el_list* first)
  54. {
  55.    el_list *tmp;
  56.    FILE *hs = fopen("highscores.txt", "w");
  57.    if( hs == NULL)
  58.       perror("Blad z plikiem.");
  59.    else
  60.    {
  61.       tmp = first;
  62.       while(tmp != NULL)
  63.       {
  64.          fprintf( hs, "%d %s\n", score, winner);
  65.      tmp = tmp->next;
  66.       }
  67.    }
  68.    fclose(hs);
  69. }
  70.  
  71. int main()
  72. {
  73.    addtolist(&first);
  74.    save2file(&first);
  75.    return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment