Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct
- {
- char prezime[20];
- float ocena;
- }STUDENT;
- int poredi(const void *a, const void *b)
- {
- char *x = (char *)a;
- STUDENT y = *(STUDENT *)b;
- return strcmp(x, y.prezime);
- }
- int main()
- {
- STUDENT *a = NULL, *pom;
- char b[20];
- int n = 0, alocirano = 0, i;
- printf("Upisati prezime studenta:\n");
- scanf("%s",b);
- FILE* f = fopen("studenti.txt", "r");
- if (f == NULL)
- {
- printf("Problem sa otvaranjem datoteke");
- return -1;
- }
- while(1)
- {
- if (n >= alocirano)
- {
- alocirano *= 2;
- alocirano++;
- pom = realloc(a, alocirano*sizeof(STUDENT));
- if (pom == NULL)
- {
- printf("Nema dovoljno prostora za niz\n");
- return -1;
- }
- else a = pom;
- }
- fscanf(f, "%s%f", a[n].prezime, &a[n].ocena);
- if (feof(f))
- break;
- n++;
- }
- fclose(f);
- pom = (STUDENT*)bsearch(b, a, n, sizeof(STUDENT), poredi);
- if (pom == NULL) printf("Student ne postoji u nizu\n");
- else printf("Prosecna ocena je %f\n", (*pom).ocena);
- return 0;
- }
Add Comment
Please, Sign In to add comment