Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct student
  6. {
  7. char imie[20];
  8. char nazwisko[20];
  9. int numer;
  10. double oceny[5];
  11. double srednia;
  12. };
  13.  
  14. void dopisz(char *nazwa);
  15. double srednia(struct student s);
  16. double minimum(char *nazwa);
  17. int usun(char *nazwa);
  18. void wydruk(char *nazwa);
  19. void popraw(char *nazwa);
  20.  
  21. main()
  22. {
  23. char z;
  24. char nazwa[30];
  25. printf("Podaj nazwe pliku na ktorym bedziemy dzis pracowac :)\n");
  26. fflush(stdin);
  27. scanf("%s", &nazwa);
  28. while (1)
  29. {
  30. printf("Co chcesz zrobic?\nd-dopisz\nu-usun\np-popraw\nq-wyjscie\nw-wydruk\n");
  31. fflush(stdin);
  32. z = getchar();
  33. switch (z)
  34. {
  35. case 'd':
  36. dopisz(nazwa);
  37. break;
  38. case 'q':
  39. return 0;
  40. case 'u':
  41. usun(nazwa);
  42. break;
  43. case 'w':
  44. wydruk(nazwa);
  45. break;
  46. case 'p':
  47. popraw(nazwa);
  48. break;
  49. }
  50. }
  51. system("pause");
  52. }
  53.  
  54. void dopisz(char *nazwa)
  55. {
  56. int i;
  57. FILE *p;
  58. struct student s;
  59. p = fopen(nazwa, "a");
  60. if (p == 0)
  61. {
  62. printf("Blad otwarcia pliku. Program zakonczy dzialanie \n");
  63. exit(0);
  64. }
  65. printf("Podaj imie studenta\n");
  66. fflush(stdin);
  67. gets(s.imie);
  68. printf("Podaj nazwisko studenta \n");
  69. fflush(stdin);
  70. gets(s.nazwisko);
  71. printf("Podaj numer studenta \n");
  72. fflush(stdin);
  73. scanf("%d", &s.numer);
  74. for (i = 0; i<5; i++)
  75. {
  76. printf("Podaj %d ocene studenta: ", i + 1);
  77. fflush(stdin);
  78. scanf("%lf", &s.oceny[i]);
  79. }
  80. s.srednia = srednia(s);
  81. printf("\n");
  82. fwrite(&s, sizeof(struct student), 1, p);
  83. fclose(p);
  84. }
  85.  
  86. double srednia(struct student s)
  87. {
  88. int i;
  89. double suma = 0;
  90. for (i = 0; i<5; i++)
  91. {
  92. suma += s.oceny[i];
  93. }
  94. return ((double)suma / 5);
  95. }
  96.  
  97. double minimum(char *nazwa)
  98. {
  99. double mini;
  100. struct student s;
  101. FILE *p;
  102. p = fopen(nazwa, "r");
  103. if (!p)
  104. {
  105. printf("Blad otwarcia pliku. Program konczy dzialanie \n");
  106. exit(0);
  107. }
  108. fread(&s, sizeof(s), 1, p);
  109. mini = s.srednia;
  110. while (fread(&s, sizeof(struct student), 1, p) == 1)
  111. {
  112. if (s.srednia<mini) mini = s.srednia;
  113. }
  114. fclose(p);
  115. return mini;
  116. }
  117.  
  118. int usun(char *nazwa)
  119. {
  120. FILE *p1, *p2;
  121. struct student s;
  122. double min = minimum(nazwa);
  123. int ile = 0;
  124. p1 = fopen(nazwa, "r");
  125. if (!p1)
  126. {
  127. printf("Blad otwarcia pliku. Zamykam program \n");
  128. exit(0);
  129. }
  130. p2 = fopen("temp.txt", "w");
  131. if (!p2)
  132. {
  133. printf("Blad otwarcia pliku temp.txt. Zamykam program. \n");
  134. exit(0);
  135. }
  136. while (fread(&s, sizeof(struct student), 1, p1) == 1)
  137. {
  138. if (s.srednia>min)
  139. {
  140. fwrite(&s, sizeof(struct student), 1, p2);
  141. }
  142. else
  143. {
  144. ile++;
  145. }
  146. }
  147. fclose(p1);
  148. fclose(p2);
  149. remove(nazwa);
  150. rename("temp.txt", nazwa);
  151. return ile;
  152. }
  153.  
  154. void wydruk(char *nazwa)
  155. {
  156. struct student s;
  157. int i;
  158. FILE *p;
  159. p = fopen(nazwa, "r");
  160. while (fread(&s, sizeof(s), 1, p))
  161. {
  162. printf("Numer studenta: %d\n", s.numer);
  163. printf("Imie studenta %s\n", s.imie);
  164. printf("Nazwisko studenta: %s\n", s.nazwisko);
  165. printf("Oceny: \n");
  166. for (i = 0; i<5; i++)
  167. {
  168. printf("%.1lf\n", s.oceny[i]);
  169. }
  170. printf("Srednia z powyzszych ocen studenta: %.1lf ", s.srednia);
  171. }
  172. fclose(p);
  173. }
  174.  
  175. void popraw(char *nazwa)
  176. {
  177. struct student s;
  178. int nr;
  179. int l = 0;
  180. FILE *p;
  181. char z;
  182. int i, jest = 0;
  183. printf("Podaj nr studenta ktorego dane chcesz zmienic \n");
  184. scanf("%d", &nr);
  185. p = fopen(nazwa, "r+");
  186. while ((fread(&s, sizeof(s), 1, p) == 1))
  187. {
  188. if (s.numer == nr)
  189. {
  190. jest = 1;
  191. break;
  192. }
  193. else l++;
  194. }
  195. if (jest)
  196. {
  197. printf("n -zmien nazwisko\ni- zmien imie\no-zmien oceny\n inny klawisz - exit \n\n");
  198. fflush(stdin);
  199. z = getchar();
  200. switch (z)
  201. {
  202. case 'i':
  203. printf("Podaj imie\n");
  204. fflush(stdin);
  205. gets(s.imie);
  206. break;
  207. case 'n':
  208. printf("Podaj nazwisko \n");
  209. fflush(stdin);
  210. gets(s.nazwisko);
  211. break;
  212. case 'o':
  213. printf("Podaj 5 ocen studenta \n");
  214. for (i = 0; i<5; i++)
  215. {
  216. printf("%d = \n", i);
  217. fflush(stdin);
  218. scanf("%lf", &s.oceny[i]);
  219. }
  220. break;
  221. default: break;
  222. }
  223. fseek(p, l - 1, SEEK_SET);
  224. fwrite(&s, sizeof(struct student), 1, p);
  225. fclose(p);
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement