FUnneR

T6 ZAD 5b

Nov 11th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. /* T6 zadatak 5b (SA FUNKCIJAMA) - Iz ulazne datoteke učitati niz studenata koji su
  2. položili ispit (indeks, ime, ocena). Koristiti funkcije,
  3. ime ulazne datoteke prihvatiti kroz argumente
  4. komandne linije.
  5. • U izlaznu datoteku "najvisi.txt" ispisati podatke o
  6. studentu sa najvišim prosekom
  7. • U izlaznu datoteku "najnizi.txt" ispisati podatke o
  8. studentu sa najnižim prosekom
  9. • Na standardnom izlazu ispisati prosečnu ocenu
  10. svih studenata */
  11. // FORMATIRANJE: string u datoteci mora da izgleda ovako(moze da ima vise redova) ime(25)#index(5)#ocena(1) npr.
  12. //(Nemanja#12345#10)
  13. //(Petar#65443#10)
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #define MAX_SIZE 90
  18.  
  19.  
  20. typedef struct student{
  21.     char ime[26];
  22.     char index[6];
  23.     int ocena;
  24. } STUDENT;
  25.  
  26. int citanje(FILE* pf,char *pok,char* str,STUDENT* stud);
  27. void najstudent(STUDENT* stud,STUDENT *najbolji,STUDENT *najgori);
  28. void upis(FILE* najb, FILE* najg,STUDENT *najgori,STUDENT *najbolji);
  29. void ispis(STUDENT* stud,int n,double* prosek,int* br);
  30.  
  31. int main(int brArg, char *arg[]){
  32.  
  33.     FILE *pf,*najg,*najb;
  34.     char str[MAX_SIZE], *pok;
  35.     STUDENT stud[MAX_SIZE];
  36.     STUDENT najbolji,najgori;
  37.     int i,n=10,br=0,max=0,min=0;
  38.     double prosek;
  39.  
  40.     if(arg[1]==NULL) printf("Unesite datoteku kao argument !\n"); return 0;
  41.  
  42.     pf=fopen(arg[1],"r");
  43.     br = citanje(pf,pok,str,stud);
  44.        
  45.     najstudent(stud,&najbolji,&najgori);
  46.        
  47.     ispis(stud,n,&prosek,&br);
  48.    
  49.     upis(najb,najg,&najbolji,&najgori);
  50.    
  51.     return 0;
  52. }
  53.  
  54. int citanje(FILE* pf,char *pok,char* str,STUDENT* stud){
  55.     int i=0,br=0;
  56.     if(pf!=NULL){
  57.  
  58.         while(fgets(str, MAX_SIZE, pf)!=NULL){
  59.            
  60.                 pok = strtok(str,"#");
  61.                 strcpy(stud[i].ime,pok);
  62.                
  63.                     pok = strtok(NULL,"#");
  64.                     strcpy(stud[i].index,pok);
  65.                    
  66.                     pok = strtok(NULL,"#");
  67.                     stud[i].ocena = atoi(pok);
  68.                     i++;
  69.                     br++;
  70.                     pok = strtok(str,"#");
  71.                    
  72.                 }
  73.        
  74.         fclose(pf);
  75.     }else printf("Nije moguce otvoriti datoteku!!\n\nDatoteku prosledite kao argument!!\n");
  76.    
  77.     return br;
  78. }
  79.  
  80. void najstudent(STUDENT* stud,STUDENT *najbolji,STUDENT *najgori){
  81.    
  82.     int i=0,n=10;
  83.    
  84.     strcpy(najbolji->ime,stud[0].ime);
  85.     strcpy(najbolji->index,stud[0].index);
  86.     najbolji->ocena = stud[0].ocena;
  87.    
  88.     strcpy(najgori->ime,stud[0].ime);
  89.     strcpy(najgori->index,stud[0].index);
  90.     najgori->ocena = stud[0].ocena;
  91.    
  92.     for(i=0;i<n,stud[i].ocena!=0;i++){
  93.         if(najbolji->ocena>stud[i].ocena){
  94.             strcpy(najbolji->ime,stud[i].ime);
  95.             strcpy(najbolji->index,stud[i].index);
  96.             najbolji->ocena = stud[i].ocena;
  97.         }
  98.         if(najgori->ocena<stud[i].ocena){
  99.             strcpy(najgori->ime,stud[i].ime);
  100.             strcpy(najgori->index,stud[i].index);
  101.             najgori->ocena = stud[i].ocena;
  102.         }
  103.     }
  104. }
  105.  
  106. void upis(FILE* najb, FILE* najg,STUDENT *najgori,STUDENT *najbolji){
  107.  
  108.     najb = fopen("najvisi.txt","w");
  109.     if(najb!=NULL){
  110.         fprintf(najb,"Ime: %s\nIndex: %s\nOcena:%d",najbolji->ime,najbolji->index,najbolji->ocena);
  111.     }else printf("Nije moguce otvoriti izlaznu datoteku.\n");
  112.     fclose(najb);
  113.    
  114.     najg = fopen("najnizi.txt","w");
  115.     if(najg!=NULL){
  116.         fprintf(najg,"Ime: %s\nIndex: %s\nOcena:%d",najgori->ime,najgori->index,najgori->ocena);
  117.     }else printf("Nije moguce otvoriti izlaznu datoteku.\n");
  118.     fclose(najg);
  119.  
  120. }
  121.  
  122. void ispis(STUDENT* stud,int n,double* prosek,int* br){
  123.     int i=0;
  124.     for(i=0;i<n,stud[i].ocena!=0;i++){
  125.    
  126.         printf("Ime: %s\n",stud[i].ime);
  127.         printf("Broj indexa: %s\n",stud[i].index);
  128.         printf("Ocena: %d\n",stud[i].ocena);
  129.         printf("\n");
  130.        
  131.         *prosek += stud[i].ocena;
  132.     }
  133.     *prosek = *prosek /(double)*br;
  134.     printf("%2.3lf\n",*prosek);
  135. }
Advertisement
Add Comment
Please, Sign In to add comment