Guest User

Untitled

a guest
Jul 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct
  6. {
  7. char prezime[20];
  8. float ocena;
  9. }STUDENT;
  10.  
  11. int poredi(const void *a, const void *b)
  12. {
  13. char *x = (char *)a;
  14. STUDENT y = *(STUDENT *)b;
  15.  
  16. return strcmp(x, y.prezime);
  17. }
  18.  
  19. int main()
  20. {
  21. STUDENT *a = NULL, *pom;
  22. char b[20];
  23. int n = 0, alocirano = 0, i;
  24.  
  25. printf("Upisati prezime studenta:\n");
  26. scanf("%s",b);
  27.  
  28. FILE* f = fopen("studenti.txt", "r");
  29.  
  30. if (f == NULL)
  31. {
  32. printf("Problem sa otvaranjem datoteke");
  33. return -1;
  34. }
  35.  
  36. while(1)
  37. {
  38. if (n >= alocirano)
  39. {
  40. alocirano *= 2;
  41. alocirano++;
  42.  
  43. pom = realloc(a, alocirano*sizeof(STUDENT));
  44.  
  45. if (pom == NULL)
  46. {
  47. printf("Nema dovoljno prostora za niz\n");
  48. return -1;
  49. }
  50. else a = pom;
  51. }
  52.  
  53. fscanf(f, "%s%f", a[n].prezime, &a[n].ocena);
  54.  
  55. if (feof(f))
  56. break;
  57.  
  58. n++;
  59. }
  60.  
  61.  
  62. fclose(f);
  63. pom = (STUDENT*)bsearch(b, a, n, sizeof(STUDENT), poredi);
  64.  
  65. if (pom == NULL) printf("Student ne postoji u nizu\n");
  66. else printf("Prosecna ocena je %f\n", (*pom).ocena);
  67.  
  68. return 0;
  69.  
  70. }
Add Comment
Please, Sign In to add comment