#include <stdio.h>
#include <string.h>

struct student
}
	char id[5];
	char name[15];
	char course[15];
	int year;
	double score[3];
	double total[3];
	double grade[3];
	double fgrade;
};

struct student s[5],temp,temp2;
struct student *p;
FILE *f;

void add()
{
	int arr=0;
	int i,j;
	f=fopen("Record.txt","w");
			if(f==NULL)
			{
				printf("Opening Student File Failed...");
			}
			else
			{
				printf("Opening Student File Successful...\n");
				system("pause");
				system("cls");
				for(i=0;i<5;i++)
				{
				printf("Enter THe Data Of Student Number %d\n",arr++);
				printf("Input ID Number: ");
				scanf("%s",s[i].id);
				printf("Input Name: ");
				scanf("%s",s[i].name);
				printf("Input Course: ");
				scanf("%s",s[i].course);
				printf("Input Year Level: ");
				scanf("%d",&s[i].year);
				fprintf(f,"%s",s[i].id);
				fprintf(f,"\n%s",s[i].name);
				fprintf(f,"\n%s",s[i].course);
				fprintf(f,"\n%d\n",s[i].year);
				for(j=1;j<=3;j++)
				{
					printf("Input Score[%d]: ",j);
					scanf("%lf",&s[i].score[j]);
					printf("Input Total Score[%d]: ",j);
					scanf("%lf",&s[i].total[j]);
					s[i].grade[j]=5-4*(s[i].score[j]/s[i].total[j]);
					printf("The Total Grade: %.2lf\n",s[i].grade[j]);
					fprintf(f,"%.2lf\n",s[i].grade[j]);		
				}
				s[i].fgrade=s[i].grade[1]*0.2+s[i].grade[2]*0.3+s[i].grade[3]*0.5;
				fprintf(f,"%.2lf\n",s[i].fgrade);
				}
			}
			fclose(f);
			system("pause");
			main();
}

void update()
{
	int i,j;
	
	f=fopen("Record.txt","r");
			if(f==NULL)
			{
				printf("Opening Student File Failed...");
			}
			else
			{
				printf("Opening Student File Successful...\n");
				system("pause");
				system("cls");
				for(i=0;i<5;i++)
				{			
					fscanf(f,"%s",s[i].id);
					fscanf(f,"%s",s[i].name);
					fscanf(f,"%s",s[i].course);
					fscanf(f,"%d",&s[i].year);
					for(j=1;j<=3;j++)
					{
						fscanf(f,"%lf",&s[i].grade[j]);
					}	
				}
			}
			fclose(f);
			system("pause");
			main();
}

void dl8()
{
	int i,j;
	f=fopen("Record.txt","r");
			if(f==NULL)
			{
				printf("Opening Student File Failed...");
			}
			else
			{
				printf("Opening Student File Successful...\n");
				system("pause");
				system("cls");
				for(i=0;i<5;i++)
				{
					
					fscanf(f,"%s",s[i].id);
					fscanf(f,"%s",s[i].name);
					fscanf(f,"%s",s[i].course);
					fscanf(f,"%d",&s[i].year);
					for(j=1;j<=3;j++)
					{
						fscanf(f,"%lf",&s[i].grade[j]);
					}	
				}
			}
			fclose(f);
			system("pause");
			main();
}

void show()
{
	int i,j;
	
	f=fopen("Record.txt","r");
			if(f==NULL)
			{
				printf("Opening Student File Failed...");
			}
			else
			{
				printf("Opening Student File Successful...\n");
				system("pause");
				system("cls");
				for(i=0;i<5;i++)
				{
					printf("============================================");
					fscanf(f,"%s",s[i].id);
					fscanf(f,"%s",s[i].name);
					fscanf(f,"%s",s[i].course);
					fscanf(f,"%d",&s[i].year);
					printf("\nID Number: %s",s[i].id);
					printf("\nName: %s",s[i].name);
					printf("\nCourse: %s",s[i].course);
					printf("\nYear Level: %d\n",s[i].year);
					for(j=1;j<=3;j++)
					{
						fscanf(f,"%lf",&s[i].grade[j]);
						printf("Total Grade[%d]: %.2lf\n",j,s[i].grade[j]);
					}
					fscanf(f,"%lf",&s[i].fgrade);
				}
			}
			printf("============================================\n");
			fclose(f);
			system("pause");
			main();
}

void compute()
{
	int i,j;
	f=fopen("Record.txt","r");
			if(f==NULL)
			{
				printf("Opening Student File Failed...");
			}
			else
			{
				printf("Opening Student File Successful...\n");
				system("pause");
				system("cls");
				for(i=0;i<5;i++)
				{
					printf("============================================");
					fscanf(f,"%s",s[i].id);
					fscanf(f,"%s",s[i].name);
					fscanf(f,"%s",s[i].course);
					fscanf(f,"%d",&s[i].year);
					printf("\nID Number: %s",s[i].id);
					printf("\nName: %s",s[i].name);
					printf("\nCourse: %s",s[i].course);
					printf("\nYear Level: %d\n",s[i].year);
					for(j=1;j<=3;j++)
					{
						fscanf(f,"%lf",&s[i].grade[j]);
						printf("Total Grade[%d]: %.2lf\n",j,s[i].grade[j]);
					}
					fscanf(f,"%lf",&s[i].fgrade);
					printf("Total Final Grade: %.2lf\n",s[i].fgrade);
				}
			}
			printf("============================================\n");
			fclose(f);
			system("pause");
			main();
}

void best()
{
	int i,j,k,l;
	
	f=fopen("Record.txt","r");
			if(f==NULL)
			{
				printf("Opening Student File Failed...");
			}
			else
			{
				printf("Opening Student File Successful...\n");
				system("pause");
				system("cls");
				for(i=0;i<5;i++)
				{
					
					fscanf(f,"%s",s[i].id);
					fscanf(f,"%s",s[i].name);
					fscanf(f,"%s",s[i].course);
					fscanf(f,"%d",&s[i].year);
					for(j=1;j<=3;j++)
					{
						fscanf(f,"%lf",&s[i].grade[j]);
					}
					fscanf(f,"%lf",&s[i].fgrade);
				}
				for(k=0;k<5-1;k++)
				{
					for(l=0;l<5-k-1;l++)
					{
						if(s[l].fgrade>s[l+1].fgrade)
						{
							temp2=s[l];
							s[l]=s[l+1];
							s[l+1]=temp2;
						}
					}
				}
				for(i=0;i<5;i++)
				{
					printf("============================================");
					printf("\nName: %s",s[i].name);
					printf("\nCourse: %s",s[i].course);
					printf("\nYear Level: %d\n",s[i].year);
					printf("Total Final Grade: %.2lf\n",s[i].fgrade);
				}
			}
			printf("============================================\n");
			fclose(f);
			system("pause");
			main();
}

void worst()
{
	int i,j;  
	f=fopen("Record.txt","r");
			if(f==NULL)
			{
				printf("Opening Student File Failed...");
			}
			else
			{
				printf("Opening Student File Successful...\n");
				system("pause");
				system("cls");
				for(i=0;i<5;i++)
				{
					
					fscanf(f,"%s",s[i].id);
					fscanf(f,"%s",s[i].name);
					fscanf(f,"%s",s[i].course);
					fscanf(f,"%d",&s[i].year);
					for(j=1;j<=3;j++)
					{
						fscanf(f,"%lf",&s[i].grade[j]);
					}
					fscanf(f,"%lf",&s[i].fgrade);
				}
				for(i=0;i<5;i++)
				{
					for(j=0;j<5;j++)
					{
						if(s[j].fgrade<s[j+1].fgrade)
						{
							temp=s[j];
							s[j]=s[j+1];
							s[j+1]=temp;
						}
					}
				}
				for(i=0;i<5;i++)
				{
					printf("============================================");
					printf("\nName: %s",s[i].name);
					printf("\nCourse: %s",s[i].course);
					printf("\nYear Level: %d\n",s[i].year);
					printf("Total Final Grade: %.2lf\n",s[i].fgrade);
				}
				printf("============================================\n");
			}
			fclose(f);
			system("pause");
			main();
}

int main(void)
{
	char pick;
	int task;
	
	system("cls");	
	printf("=========================================================");
	printf("\n=================Student Records MENU====================");
	printf("\n=========================================================");
	printf("\n# [1]Input New Student Record\t\t\t\t#");
	printf("\n# [2]Modify Student Record\t\t\t\t#");
	printf("\n# [3]Delete Student Record\t\t\t\t#");
	printf("\n# [4]Show All Existing Student Record\t\t\t#");
	printf("\n# [5]Compute The Final Grade Of The Student\t\t#");
	printf("\n# [6]Display Student Record[Best To Worst Final Grade]\t#");
	printf("\n# [7]Display Student Record[Worst To Best Final Grade]\t#");
	printf("\n# [8]Exit\t\t\t\t\t\t#");
	printf("\n=========================================================");
	printf("\nEnter Task: ");
	scanf("%d",&task);
	printf("=========================================================\n");
	switch(task)
	{
		case 1:
			add();
			break;
		case 2:
			update();
			break;
		case 3:
			dl8();
			break;
		case 4:
			show();
			break;
		case 5:
			compute();
			break;
		case 6:
			best();
			break;
		case 7:
			worst();
			break;
		case 8:
			printf("Thank You For Using The Student Database Program");
			return 0;
			break;
		default:
			printf("Invalid Option...\n");
			printf("============================================\n");
			system("pause");
			main();
			break;
	}	
}
