#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#define MAX 50
#define REDOVE 10
#define OTGOVORI "otgovori"
void CDate(int *d,int *m,int *y);
typedef struct{ char FakNo[10]; char otg[11]; } Student;
typedef struct{ char otg[11]; } verniotg;
FILE *GetFile(char *path1);
void Menu(FILE *p1,char *path1);
void input(FILE *p);
void verni();
void ocenka(FILE *p);
void srocenka(FILE *p);
void output(FILE *p,char *path);
void main() {
 FILE *list,*otg;
 int d,m,y;
 char path[50];
 CDate(&d,&m,&y);
 list = GetFile(path);
 otg = fopen(OTGOVORI,"rb");
 if (otg == NULL) verni();
 Menu(list,path);
}
void CDate(int *d,int *m,int *y) {
 printf("Vuvedete dneshnata data(den mesec godina):\n");
 scanf("%d %d %d",d,m,y);
 while((*d <= 0 || *d >31) || (*m <= 0 || *m >12) || *y < 2000) {
  printf("Greshno vuvedeni danni\n");
  printf("Vuvedete dneshnata data(den mesec godina):\n");
  scanf("%d %d %d",d,m,y);
 } 
}
FILE *GetFile(char *path1) {
 fflush(stdin);
 FILE *list1;
 printf("Vuvedete put kum faila s danni:\n");
 gets(path1);
 list1 = fopen(path1,"r");
 if (list1 == NULL)  printf("Gre6ka pri namirane na faila\n");  
 return list1;
}
void Menu(FILE *p1,char *path1) {
 int choise;
 while(choise != 5) { 
  printf("1.Izvejdane na studenti ot faila\n");
  printf("2.Zapisvane na studenti vuv fail\n");
  printf("3.Izvejdane na ocenka na student\n");
  printf("4.Izvejdane na srednata ocenka na studentite\n");
  printf("5.Exit\n");
  printf("Opciq:");
  scanf("%d",&choise);
  switch(choise) {
   case 1 : { 
	 p1 = fopen(path1,"rb");
	 if (p1 == NULL) { printf("Gre6ka pri namirane na faila ili faila e prazen\n");  getch(); }
     else { 
      output(p1,path1);
	 }
    printf("Natisnete nqkoi klavish za vrushtane kum glavnoto menu\n"); getch();
	}
   break;
    case 2 : { 
	  p1 = fopen(path1,"ab");
      input(p1);
      fclose(p1);
	}
   break;
	case 3 : { p1 = fopen(path1,"rb"); ocenka(p1);     printf("Natisnete nqkoi klavish za vrushtane kum glavnoto menu\n"); getch(); }
   break;
    case 4 : {
		p1 = fopen(path1,"rb");
		srocenka(p1);
        printf("Natisnete nqkoi klavish za vrushtane kum glavnoto menu\n"); getch();
		}
  }
 }
}
void verni() {
 verniotg m;
 FILE *p;
 p = fopen(OTGOVORI,"wb");
  for(int k=0;k<10;k++) {
   printf("Veren otgovor %d(y/n):",k+1);
   scanf("%c",&m.otg[k]);
   fflush(stdin);
  }
  m.otg[k] = '\0';
  fwrite(&m,sizeof(verniotg),1,p); 
 fclose(p);
}
void input(FILE *p) {
 Student m;
 int k;
 char i;
 do {
  fflush(stdin);
  printf("Fakulteten nomer:"); 
  scanf("%s",&m.FakNo);
  fflush(stdin);
  for(k=0;k<10;k++) {
   printf("Otgovor %d(y/n):",k+1);
   scanf("%c",&m.otg[k]);
   fflush(stdin);
  }
  m.otg[k] = '\0';
  fwrite(&m,sizeof(Student),1,p);
 } while (printf("Produljavane s vuvejdaneto Y/N?"),fflush(stdin),scanf("%c",&i),toupper(i) == 'Y');
}
void ocenka(FILE *p) {
 fflush(stdin);
 FILE *p1;
 p1 = fopen(OTGOVORI,"rb");
 int i = 0,count = 0,flag = 0;
 float ocen=0;
 verniotg Otg;
 fread(&Otg,sizeof(verniotg),1,p1);
 fclose(p1);
 Student m[MAX],a;
 char Fakno[10];
 printf("Vuvedete fak.No:");
 scanf("%s",&Fakno);
 fflush(stdin);
 while(!feof(p)) {
  fread(&m[i],sizeof(Student),1,p); count++; i++;
 }
 for(i = 0;i < (count - 1) ;i++) {
	 if (strcmp(m[i].FakNo,Fakno) == 0) { printf("Nameren e studenta\n"); a = m[i]; flag = 1;}
 }
 if (flag == 0) { printf("Nqma takuv student\n"); }
 else {
	 for(i=0;i<10;i++) {
		 if (a.otg[i] == Otg.otg[i] ) { ocen+=1; }
	 }
	 if (ocen >= 9) { ocen=6; printf("Ocenkata e:%.2f\n",ocen); }
	 else { 
		 if (ocen == 8) { ocen=5; printf("Ocenkata e:%.2f\n",ocen); }
		 else {
			 if (ocen == 7) { ocen=4; printf("Ocenkata e:%.2f\n",ocen); }
			 else {
				 if (ocen== 6) { ocen=3; printf("Ocenkata e:%.2f\n",ocen); }
				 else { ocen=2; printf("Ocenkata e:%.2f\n",ocen); }
			 }
		 }
	 }
 }
}
void srocenka(FILE *p) {
 fflush(stdin);
 FILE *p1;
 p1 = fopen(OTGOVORI,"rb");
 int i = 0,k,count = 0,flag = 0;
 float ocen=0,srocen=0;
 verniotg Otg;
 fread(&Otg,sizeof(verniotg),1,p1);
 fclose(p1);
 Student m[MAX];
 while(!feof(p)) {
  fread(&m[i],sizeof(Student),1,p); count++; i++;
 }
 printf("       Fak.No        Otgovori        Ocenka\n");
 printf("----------------------------------------------------\n");
 printf("pravilni otgovori    %s\n",Otg.otg);
 for(i=0;i<(count-1);i++) {
	 for(k=0;k<10;k++) {
	  if (m[i].otg[k] == Otg.otg[k]) ocen+=1;
	 }
	 if (ocen >= 9) { ocen=6; }
	 else { 
		 if (ocen == 8) { ocen=5; }
		 else {
			 if (ocen == 7) { ocen=4; }
			 else {
				 if (ocen== 6) { ocen=3; }
				 else { ocen=2; }
			 }
		 }
	 }
  printf("%15s %15s %10.2f\n",m[i].FakNo,m[i].otg,ocen);
  srocen+=ocen;
  ocen=0;
 }
 printf("----------------------------------------------------\n");
 printf("      Sreden procent pravilni otgovori    %.2f\n",srocen/(count-1));
}
void output(FILE *p,char *path) {
 fflush(stdin);
 int i = 0,k,count = 0;
 Student a[MAX],b[MAX],Min;
 while(!feof(p)) {
  fread(&a[i],sizeof(Student),1,p); count++; i++;  
 }
 for(i = 0;i < (count -1);i++) {
  for(k = 0;k < (count-1);k++) {
   if (i == 0) {
	if (k == 0) { Min = a[k]; }
	else {
	 if (strcmp(Min.FakNo,a[k].FakNo) > 0) { Min = a[k]; }
	}   
   }
   else {
	if (k == 0) {
	 if (strcmp(b[i-1].FakNo,a[k].FakNo) < 0) { Min = a[k]; }   
	}
    else {
	 if (strcmp(Min.FakNo,b[i-1].FakNo) == 0) { if (strcmp(b[i-1].FakNo,a[k].FakNo) < 0) Min = a[k]; }
     else { if(strcmp(Min.FakNo,a[k].FakNo) > 0 && strcmp(b[i-1].FakNo,a[k].FakNo) < 0) Min = a[k]; }
	}
   }
  }
  b[i] = Min;
 }
 fclose(p);
 p = fopen(path,"wb");
 for(i = 0;i < (count - 1);i++) {
  if (i % (REDOVE - 1) == 0 && i >= (REDOVE - 1)) {
   getch();
   printf("  Fak.No:%s Otgovori%s \n",b[i].FakNo,b[i].otg);
   fwrite(&b[i],sizeof(Student),1,p);
  }
  else { 
   printf("  Fak.No:%s Otgovori:%s \n",b[i].FakNo,b[i].otg);
   fwrite(&b[i],sizeof(Student),1,p);  
  }
 }
 fclose(p);
}