Guest User

Untitled

a guest
May 27th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct liczby
  5. {
  6. int x;
  7. struct liczby *nast;
  8. };
  9.  
  10. typedef struct liczby EL;
  11. typedef EL *WSK;
  12.  
  13. void Wstaw(WSK *pierwszy,WSK nowy)
  14. {
  15. WSK temp = NULL;
  16. nowy->nast = NULL;
  17. if(!(*pierwszy))
  18. {
  19. (*pierwszy) = nowy;
  20. //(*pierwszy)->id = 0;
  21. return 0;
  22. }
  23. temp = *pierwszy;
  24. while(temp->nast)
  25. temp = temp->nast;
  26. //nowy->id = temp->id+1;
  27. temp->nast = nowy;
  28. }
  29.  
  30. double srednia(WSK lista)
  31. {
  32. double srednia=0;
  33. int suma=0,i=0;
  34. WSK temp=lista;
  35. while(lista)
  36. {
  37. i++;
  38. suma+=lista->x;
  39. lista=lista->nast;
  40. }
  41.  
  42. srednia=(double)suma/i;
  43. printf("\n SREDNIA: %d / %d = %lf",suma,i,srednia);
  44. return srednia;
  45. }
  46.  
  47. double sort(WSK lista)
  48. {
  49. int i=0;
  50. WSK temp=lista;
  51. temp=temp->nast;
  52. while(temp)
  53. {
  54. if((lista->x)<temp->x)
  55. {
  56. temp=temp->nast;
  57. lista=lista->nast;
  58. }
  59. else
  60. {
  61. printf("Lista nie jest posortowana\n");
  62. break;
  63. return 0;
  64. }
  65. i++;
  66. if(i==4)
  67. printf("\nLista jest posortowana\n");
  68. }
  69. }
  70.  
  71. void plik(char *nazwa, WSK lista)
  72. {
  73. FILE *f=NULL;
  74. f=fopen(nazwa,"wb");
  75. if(!f)
  76. {
  77. printf("Blad przy otwieraniu pliku");
  78. return -1;
  79. }
  80. while(lista)
  81. {
  82. fwrite(lista,sizeof(EL),1,f);
  83. lista=lista->nast;
  84. }
  85.  
  86. fclose(f);
  87. }
  88.  
  89. void wyswietl(WSK lista)
  90. {
  91. while(lista)
  92. {
  93. printf("\nLista po odczycie to: %d",lista->x);
  94. lista=lista->nast;
  95. }
  96. }
  97.  
  98. void odczyt(char *nazwa, WSK lista)
  99. {
  100. FILE *f=NULL;
  101. f=fopen(nazwa,"rb");
  102. if(!f)
  103. {
  104. printf("Blad przy odczycie pliku");
  105. return -1;
  106. }
  107. WSK temp=(WSK)malloc(sizeof(EL));
  108. while(fread(temp->x,sizeof(EL),1,f))
  109. {
  110. temp=(WSK)malloc(sizeof(EL));
  111. Wstaw(&lista,temp);
  112. }
  113.  
  114. wyswietl(lista);
  115. fclose(f);
  116. }
  117.  
  118.  
  119.  
  120. void krance(WSK lista, int a, int b)
  121. {
  122. WSK temp=NULL;
  123. WSK temp2=NULL;
  124. while(lista)
  125. {
  126. if(lista->x > a && lista->x < b)
  127. {
  128. temp2 = (WSK)malloc(sizeof(EL));
  129. *temp2 = *lista;
  130. Wstaw(&temp,temp2);
  131. }
  132. lista=lista->nast;
  133. }
  134. while(temp)
  135. {
  136. printf("\nLista %d - %d: %d",a,b,temp->x);
  137. temp=temp->nast;
  138.  
  139. }
  140. }
  141.  
  142. int main()
  143. {
  144. int i=0;
  145. WSK lista=NULL;
  146. WSK nowy=(WSK)malloc(sizeof(EL));
  147. WSK lista2=NULL;
  148.  
  149. while(i<5)
  150. {
  151. scanf("%d",&(nowy->x));
  152. i++;
  153.  
  154. Wstaw(&lista,nowy);
  155.  
  156. nowy=(WSK)malloc(sizeof(EL));
  157. }
  158. WSK temp=lista;
  159.  
  160.  
  161.  
  162.  
  163. while(temp)
  164. {
  165. printf(" !!! %d !!! ",temp->x);
  166. temp=temp->nast;
  167. }
  168. // sort(lista);
  169. // srednia(lista);
  170. // krance(lista,3,8);
  171. plik("plika.bin",lista);
  172. odczyt("plika.bin",lista2);
  173. }
Add Comment
Please, Sign In to add comment