Guest User

jjjkl;j

a guest
May 21st, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stddef.h>
  5.  
  6. typedef struct dane * Dane;
  7.  
  8. struct dane {
  9.     char imie[20];
  10.     int id;
  11.     Dane next;
  12. };
  13.  
  14. typedef struct lista * Lista;
  15. struct lista {
  16.     Dane head;
  17.     Dane last;
  18. };
  19.  
  20. int findId(Lista l){
  21.     if(!(l->head)){
  22.         return 1;
  23.     } else {
  24.         return (l->last->id) + 1;
  25.     }
  26.    
  27. }
  28.  
  29.  
  30. void addDane(Lista l){
  31.     int id;
  32.     char imie[20];
  33.     Dane d = (Dane)malloc(sizeof(struct dane));
  34.     id = findId(l);
  35.     puts("imie?");
  36.     gets(imie);
  37.     strcpy(d->imie, imie);
  38.     d->id = id;
  39.     d->next = NULL;
  40.    
  41.     if(!(l->head)){
  42.         l->head = d;
  43.     } else {
  44.         l->last->next = d;
  45.     }
  46.     l->last = d;
  47. }
  48.  
  49.  
  50. Lista makeList(){
  51.     Lista l = (Lista)malloc(sizeof(struct lista));
  52.     l->head = NULL;
  53.     l->last = NULL;
  54.    
  55.     return l;
  56.    
  57. }
  58. void printList(Lista l){
  59.     Dane d = l->head;
  60.     while(d){
  61.         printf("%d. %s\n",d->id, d->imie);
  62.         d=d->next;
  63.     }
  64.    
  65. }
  66. int zliczaj(Lista l){
  67.     Dane d = l->head;
  68.     int licznik = 0;
  69.     while(d){
  70.         licznik++;
  71.         d=d->next;
  72.     }
  73.     return licznik;
  74. }
  75. void wczytaj(Lista l){
  76.     int licz;
  77.     FILE * plik = fopen("imiona.db", "rb");
  78.    
  79.     fread(&licz,sizeof(int),1, plik);
  80.     Dane d = l->head;
  81.     while(licz > 0){
  82.         addDane(l);
  83.         d=d->next;
  84.     licz--;
  85.     }
  86.     fclose(plik);
  87. }
  88.  
  89. void zapisz(Lista l){
  90.     int licz = zliczaj(l);
  91.     FILE * plik = fopen("imiona.db", "wb");
  92.     fwrite(&licz,sizeof(int), 1, plik);
  93.    
  94.     Dane d = l->head;
  95.     while(licz > 0){
  96.         fwrite(d,sizeof(struct dane), 1, plik);
  97.         d=d->next;
  98.         licz--;
  99.     }
  100.     fclose(plik);
  101. }
  102. int main(){
  103.    
  104.     Lista studenci = makeList();
  105.     wczytaj(studenci);
  106.     int i;
  107.     for(i=0;i<5;i++){
  108.         addDane(studenci);
  109.     }
  110.     printList(studenci);
  111.     zapisz(studenci);
  112.    
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment