Guest User

Untitled

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