Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*U datoteci naziva "studenti.dat" nalaze se informacije o rezultatima kolokvija zapisani u obliku struktura:
  5.  
  6. ime studenta - string od 30+1 znak,
  7. prezime studenta - string od 30+1 znak,
  8. ocjena - cijeli broj*/
  9.  
  10. typedef struct{
  11.     char ime[30+1];
  12.     char prezime [30+1];
  13.     int ocjena;
  14. } Student;
  15.  
  16. int main()
  17. {
  18.     FILE* inputFile;
  19.     FILE* outputFile;
  20.  
  21.     inputFile = fopen("studenti.dat", "rb");
  22.     outputFile = fopen("rezultati.txt", "w");
  23.  
  24.     Student student;
  25.  
  26.     int i = 0, brojPozitivnih = 0, brojNegativnih = 0, opcija;
  27.     do{
  28.         scanf("%d", &opcija);
  29.     }while(opcija < 1 || opcija > 2);
  30.  
  31.     do{
  32.         fseek(inputFile, sizeof(Student) * i, SEEK_SET);
  33.         fread(&student, sizeof(Student), 1, inputFile);
  34.         if(student.ocjena > 1){
  35.             if(opcija == 1)
  36.                 fprintf(outputFile,"%s %s %d\n", student.ime, student.prezime, student.ocjena);
  37.             brojPozitivnih++;
  38.         }
  39.         else if(student.ocjena == 1){
  40.             if(opcija == 2)
  41.                 fprintf(outputFile,"%s %s %d\n", student.ime, student.prezime, student.ocjena);
  42.             brojNegativnih++;
  43.         }
  44.         i++;
  45.     }while(fread(&student, sizeof(Student), 1, inputFile) == 1);
  46.  
  47.     fprintf(outputFile, "\nKolokvij je proslo %0.0f%% studenata, a palo %0.0f%%.", ((float)brojPozitivnih/(float)i)*100, ((float)brojNegativnih/(float)i)*100);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement