#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX 100
#define ANSWER_L 11
#define ANSWER 10
#define FACN_L 11

int menu(void);
void show(int *top);
void enter(int *top);
void save(char *filename, int *top);
void load(char *filename,int *top);
void check(int *top);
void check_all(int *top);
int data_check(int *den,int *mesec,int *godina);

struct catalog {
   char facn[FACN_L];
   char answer[ANSWER_L];
} cat[MAX];


void main()
{
  int choice,n,data,den,mesec,godina,top;
  char filename;
  top=0;
  do{
	  printf("Vavedi data(den mesec godina):");
}while (scanf("%d %d %d",&den, &mesec, &godina)!=0&&data_check(&den,&mesec,&godina)!=0);
  fflush(stdin);
 load(&filename,&top);
  do {
    choice=menu();
	switch(choice) {
		
		case 1: enter(&top); break;
		case 2: show(&top); break;
		case 3: check(&top); break;
		case 4: check_all(&top); break;
		case 5: save(&filename,&top); break;
		case 6: exit;
	  }
    } while(choice!=6);
  printf("Data %d.%d.%d\n",den,mesec,godina);
  exit(0);
}

// ??????? ?? ????? ?? ????

int menu(void)
{
  int i;
  char str[10];
printf("\n||=============================================================||\n");
  printf("||Menu:                                                        ||\n");
  printf("||                                                             ||\n");
  printf("|| 1. Vavejdane na informaciq                                  ||\n");
  printf("|| 2. Izvejdane na informaciq                                  ||\n");
  printf("|| 3. Iz4islqvane pravilnite otgovori na daden student         ||\n");
  printf("|| 4. Iz4islqvane pravilnite otgovori na vsi4ki studenti       ||\n");  
  printf("|| 5. Izvejdane na danni ot fail(save)                         ||\n");
  printf("|| 6. Izhod                                                    ||\n");
  printf("||=============================================================||\n\n");
  do {
	  printf("Izberi: ");
	  gets(str);
	  i=atoi(str);
	  printf("\n");
  } while(i<1 || i>6);

 return i;
}

//????????? ?? ............ ? ?????? ?????

void enter(int *top)
{
  int i,n;
  char temp;
  if (strlen(cat[0].answer) == 0){
retry: printf("Vavedi pravilnite otgovori:");
	  gets(cat[0].answer);
	  *top=1;
	  n=0;
	  do{

		  temp=toupper(cat[0].answer[n]);
		  cat[0].answer[n]=temp;
		  if(cat[0].answer[n]!='Y'&&cat[0].answer[n]!='N'){
			  printf("Vavedete otgovorite samo s Y/N\n");
			  goto retry;}
		  n++;
	  }while(n<strlen(cat[0].answer));
  }
  for(i=*top; i<MAX; i++) {
	printf("Vavedi fakulteten nomer(ili Enter za krai): ");
	gets(cat[i].facn);
	if(!*cat[i].facn) break;
retry1: printf("Vavedete otgovori: ");
	gets(cat[i].answer);
	n=0;
		do{	
		  temp=toupper(cat[i].answer[n]);
		  cat[i].answer[n]=temp;
		  if(cat[i].answer[n]!='Y'&&cat[i].answer[n]!='N'){
			  printf("Vavedete otgovorite samo s Y/N\n");
			  goto retry1;}
		  n++;		  
	  }while(n<strlen(cat[i].answer));
  }
  *top = i;
}

void show(int *top)
{
	int i;
printf("Fak. N:			 Otgovori:\n");
	for(i=1;i<*top;i++)
	{
		    printf("%-24s %-20s\n",cat[i].facn,cat[i].answer);
			if(i%23==0&&i!=0){   //prenasq na nova stranica
			printf("Natisnete Enter za da produlzite......");
			getchar();
			}	
		}
}

void load(char *filename,int *top)
{
		FILE *fp;
	printf("Vavedete imeto na faila:");
	gets(filename);


	if ((fp=fopen(filename,"rb"))==NULL)
	{
		printf("Failat ne su6testvuva!\n");
	return;
	} else printf("Failut e uspeshno otvoren\n");
	if(fread(top, sizeof top, 1 , fp)!=1){
		printf("Greshka pri chetene na faila! \n");
		exit(1);
	}
	if(fread(cat, sizeof cat, 1 , fp)!=1){
		printf("Greshka pri chetene na faila!  \n");
		exit(1);
	}
	fclose(fp);
}
	void save(char *filename, int *top)
	{
	FILE *fp;

		if((fp=fopen(filename,"wb")) == NULL) {
			printf("Failut ne moze da se otvori! \n");
			exit(1);
		}
	if(fwrite(top, sizeof(top), 1 , fp)!=1){
		printf("Greshka pri chetene na faila! \n");
		exit(1);
	}
	if(fwrite(cat, sizeof cat, 1 , fp)!=1){
		printf("Greshka pri chetene na faila! \n");
		exit(1);
	}

	fclose(fp);
	printf("Failat e zapazen uspe6no!\n");
	}

void check(int *top){
	char facn[FACN_L];
	char temp1[1],temp2[1];
		int i, found,n,result,ocenka;
		int correct=0;
		printf("Vavedete fak N na studenta:");
		gets(facn);
		found = 0;
	for(i=1;i<*top;i++)
		if(!strcmp(facn,cat[i].facn)){
			for(n=0;n<ANSWER;n++){
			temp1[0]=cat[i].answer[n];
			temp2[0]=cat[0].answer[n];
				
				result=strncmp(temp1,temp2,1);
				if(result==0)
				{
					correct++;
				}	
		}
		printf("Pravilni otgovori %d \n",correct);
		if(correct<=(ANSWER-5)) ocenka=2;
		if(correct==(ANSWER-4)) ocenka=3;
		if(correct==(ANSWER-3)) ocenka=4;
		if(correct==(ANSWER-2)) ocenka=5;
		if(correct==(ANSWER-1)||correct==(ANSWER)) ocenka=6;
		printf("Ocenka %d\n",ocenka);
		found = 1;
		}
		if (found==0) printf("Ne nameren studen s takuv fakulteten nomer\n");	
}


void check_all(int *top){

	char temp1[1],temp2[1];
	int i,n,result,correct,all,correct_temp=0,ocenka,ocenka_all=0;
	double correct_all;
	correct=0;
	all=0;
	printf("Fak. N:			Otgovori:	  Broi tochki       Ocenka\n");
	printf("Pravilni otgovori:	%s \n",cat[0].answer);
	for(i=1;i<(*top);i++){
			for(n=0;n<ANSWER;n++){
			temp1[0]=cat[i].answer[n];
			temp2[0]=cat[0].answer[n];			
				result=strncmp(temp1,temp2,1);
				if(result==0)
				{
					correct_temp++;
					correct++;
				}
				all++;
				
		}
		printf("%-24s",cat[i].facn);
		printf("%-23s",cat[i].answer);
		printf("%-15.d",correct_temp);
		if(correct_temp<=(ANSWER-5)) ocenka=2;
		if(correct_temp==(ANSWER-4)) ocenka=3;
		if(correct_temp==(ANSWER-3)) ocenka=4;
		if(correct_temp==(ANSWER-2)) ocenka=5;
		if(correct_temp==(ANSWER-1)||correct_temp==(ANSWER)) ocenka=6;
		printf("%d\n",ocenka);
		ocenka_all+=ocenka;
		correct_temp=0;
	}
	printf("                                                            ______\n ");
	printf("\t \t \t \t \t       Sredna ocenka: %.0f\n",(double)ocenka_all/(i-1));
}

int data_check(int *den,int *mesec,int *godina){
	int error=0;
	if (*mesec<=12&&*mesec>=1){
	if ((*mesec==1 || *mesec==3 || *mesec==5 || *mesec==7 || *mesec==8 || *mesec==10 || *mesec==12)&&*den>31)
	{
		printf("Nesu6testvuva6ta data1 \n");
			error=1;
	}
	if((*mesec==4 || *mesec==6 || *mesec==9 || *mesec==11)&&*den>30)
		{
			printf("Nesu6testvuva6ta data! \n");
			error=1;}
	if(*mesec==2)
		{
		if (*godina%4==0 &&*den>29&& *mesec==2){
			printf("Nesu6testvuva6ta data! \n");
			error=1;}
		if (*godina%4!=0&&*mesec==2&&*den>28){
			printf("Nesu6testvuva6ta data! \n");
			error=1;
			}
	}
	}
	else{
		printf("Nesu6testvuva6t mesec! \n");
		error=1;
	}
return error;
}