Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* T6 zadatak 5b (SA FUNKCIJAMA) - Iz ulazne datoteke učitati niz studenata koji su
- položili ispit (indeks, ime, ocena). Koristiti funkcije,
- ime ulazne datoteke prihvatiti kroz argumente
- komandne linije.
- • U izlaznu datoteku "najvisi.txt" ispisati podatke o
- studentu sa najvišim prosekom
- • U izlaznu datoteku "najnizi.txt" ispisati podatke o
- studentu sa najnižim prosekom
- • Na standardnom izlazu ispisati prosečnu ocenu
- svih studenata */
- // FORMATIRANJE: string u datoteci mora da izgleda ovako(moze da ima vise redova) ime(25)#index(5)#ocena(1) npr.
- //(Nemanja#12345#10)
- //(Petar#65443#10)
- #include <stdio.h>
- #include <string.h>
- #define MAX_SIZE 90
- typedef struct student{
- char ime[26];
- char index[6];
- int ocena;
- } STUDENT;
- int citanje(FILE* pf,char *pok,char* str,STUDENT* stud);
- void najstudent(STUDENT* stud,STUDENT *najbolji,STUDENT *najgori);
- void upis(FILE* najb, FILE* najg,STUDENT *najgori,STUDENT *najbolji);
- void ispis(STUDENT* stud,int n,double* prosek,int* br);
- int main(int brArg, char *arg[]){
- FILE *pf,*najg,*najb;
- char str[MAX_SIZE], *pok;
- STUDENT stud[MAX_SIZE];
- STUDENT najbolji,najgori;
- int i,n=10,br=0,max=0,min=0;
- double prosek;
- if(arg[1]==NULL) printf("Unesite datoteku kao argument !\n"); return 0;
- pf=fopen(arg[1],"r");
- br = citanje(pf,pok,str,stud);
- najstudent(stud,&najbolji,&najgori);
- ispis(stud,n,&prosek,&br);
- upis(najb,najg,&najbolji,&najgori);
- return 0;
- }
- int citanje(FILE* pf,char *pok,char* str,STUDENT* stud){
- int i=0,br=0;
- if(pf!=NULL){
- while(fgets(str, MAX_SIZE, pf)!=NULL){
- pok = strtok(str,"#");
- strcpy(stud[i].ime,pok);
- pok = strtok(NULL,"#");
- strcpy(stud[i].index,pok);
- pok = strtok(NULL,"#");
- stud[i].ocena = atoi(pok);
- i++;
- br++;
- pok = strtok(str,"#");
- }
- fclose(pf);
- }else printf("Nije moguce otvoriti datoteku!!\n\nDatoteku prosledite kao argument!!\n");
- return br;
- }
- void najstudent(STUDENT* stud,STUDENT *najbolji,STUDENT *najgori){
- int i=0,n=10;
- strcpy(najbolji->ime,stud[0].ime);
- strcpy(najbolji->index,stud[0].index);
- najbolji->ocena = stud[0].ocena;
- strcpy(najgori->ime,stud[0].ime);
- strcpy(najgori->index,stud[0].index);
- najgori->ocena = stud[0].ocena;
- for(i=0;i<n,stud[i].ocena!=0;i++){
- if(najbolji->ocena>stud[i].ocena){
- strcpy(najbolji->ime,stud[i].ime);
- strcpy(najbolji->index,stud[i].index);
- najbolji->ocena = stud[i].ocena;
- }
- if(najgori->ocena<stud[i].ocena){
- strcpy(najgori->ime,stud[i].ime);
- strcpy(najgori->index,stud[i].index);
- najgori->ocena = stud[i].ocena;
- }
- }
- }
- void upis(FILE* najb, FILE* najg,STUDENT *najgori,STUDENT *najbolji){
- najb = fopen("najvisi.txt","w");
- if(najb!=NULL){
- fprintf(najb,"Ime: %s\nIndex: %s\nOcena:%d",najbolji->ime,najbolji->index,najbolji->ocena);
- }else printf("Nije moguce otvoriti izlaznu datoteku.\n");
- fclose(najb);
- najg = fopen("najnizi.txt","w");
- if(najg!=NULL){
- fprintf(najg,"Ime: %s\nIndex: %s\nOcena:%d",najgori->ime,najgori->index,najgori->ocena);
- }else printf("Nije moguce otvoriti izlaznu datoteku.\n");
- fclose(najg);
- }
- void ispis(STUDENT* stud,int n,double* prosek,int* br){
- int i=0;
- for(i=0;i<n,stud[i].ocena!=0;i++){
- printf("Ime: %s\n",stud[i].ime);
- printf("Broj indexa: %s\n",stud[i].index);
- printf("Ocena: %d\n",stud[i].ocena);
- printf("\n");
- *prosek += stud[i].ocena;
- }
- *prosek = *prosek /(double)*br;
- printf("%2.3lf\n",*prosek);
- }
Advertisement
Add Comment
Please, Sign In to add comment