Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void menu2 (void);
void sorting (int option);
int main (void)
{
int option, num, midterm, final, i;
char ans;
char filename [50];
char matrix [20];
FILE* spout;
do
{
system("cls");
printf("\t\t *** Students' Marks Grading Program ***\n\n\n\n\n");
printf("Do you have an input text file for students' marks?\n\n");
printf("1)No. I want to create an input text file.\n");
printf("2)Yes. I have.\n");
printf("3)Quit.");
printf("\n\n\nPlease select your option, eg. [1]: ");
scanf("%d", &option);
system("cls");
if (option == 1)
{
printf("Please name your input file, eg. [studentsmark.txt]: ");
scanf("%s", filename);
if (!(spout=fopen(filename, "w")))
{
printf("\aError creating file!\n");
return 0;
}
spout = fopen(filename, "w");
printf("File was created successfully!\n");
printf("Please enter the number of student: ");
scanf("%d", &num);
fprintf (spout,"01234012345678901234567890123456789\n");
fprintf (spout,"No\tMatrix\t\t\tMid term\tFinal\n");
for (i=0; i<num; i++)
{
printf("Student %d\n", i+1);
printf("Matrix no: ");
scanf ("%s", matrix);
printf("Mark of mid term: ");
scanf ("%d", &midterm);
printf("Mark of final: ");
scanf ("%d", &final);
fprintf (spout,"%d\t%s\t\t%d\t\t%d\n",i+1,matrix,midterm,final);
}
if (fclose(spout)==EOF)
{
printf("\aError closing file!\n");
return 0;
}
fclose(spout);
printf("File was closed successfully!\n");
}
else if (option == 2)
{
menu2();
return 0;
}
else if (option == 3)
{
printf("Thanks for using this programme!\nHave a nice day.\n\n\nProgramme end.\n");
return 0;
}
else
{
printf("\aWrong input!\nProgramme was terminated!\n");
return 0;
}
printf("Do you want continue using this programme?\n");
printf("Press [Y] for yes, else press any key to abort\nYour choice: ");
fflush(stdin);
scanf("%c", &ans);
}while (ans == 'Y'|| ans =='y');
printf("Thanks for using this programme!\nHave a nice day.\n");
return 0;
}
void menu2 (void)
{
FILE* spRead;
FILE* spout;
char ans;
char matrix [100][20];
char filename[50];
char header [100];
char grade [100][5];
int i, j, tempn;
int count=0;
int stuNo [100], midterm[100], final[100], total[100];
int option1, option2;
char tempc [20];
char tempg [5];
printf("\t\t *** Students' Marks Grading Program ***\n\n\n\n\n");
printf("1)Read your input text file.\n");
printf("2)Back to pervious menu.\n");
printf("3)Quit.");
printf("\n\n\nPlease select your option, eg. [1]: ");
scanf("%d", &option1);
system("cls");
if (option1 == 1)
{
printf("Please enter your input file name, eg. [studentsmark.txt]: ");
scanf("%s", filename);
if (!(spRead = fopen(filename,"r")))
{
printf("\aError opening %s\n", filename);
return;
}
fgets(header, sizeof(header),spRead);
fgets(header, sizeof(header),spRead);
for (j=0; j<100; j++)
{
fscanf(spRead, "%d%s%d%d", &stuNo[j], matrix[j],&midterm[j],&final[j]);
if(stuNo[j]==j+1)
{
count++;
}
}
for(i=0; i<count; i++)
{
if(midterm[i]>100||midterm[i]<0)
{
printf("\n\aYou Key In Wrong Midterm Input For Student No.%d.\n",i+1);
printf("Please Check Again Your Input Value.Thank You!!\n");
}
if(final[i]>100||final[i]<0)
{
printf("You Key In Wrong Final Input For Student No.%d.\n",i+1);
printf("Please Check Again Your Input Value.Thank You!!\n");
}
}
for(i=0;i<count;i++)
{
total[i] = midterm[i] + final[i];
if(total[i]>100)
{
printf("\aError in Marks For Student No.%d. (Total is More Than 100)\n",i+1);
}
}
for (i=0; i<count; i++)
{
if (total[i]>=80)
{
strcpy(grade[i],"A");
}
else if (total[i]>=70 && total[i]<80)
{
strcpy(grade[i],"A-");
}
else if (total[i]>=65 && total[i]<70)
{
strcpy(grade[i],"B+");
}
else if (total[i]>=60 && total[i]<65)
{
strcpy(grade[i],"B");
}
else if (total[i]>=55 && total[i]<60)
{
strcpy(grade[i],"B-");
}
else if (total[i]>=50 && total[i]<55)
{
strcpy(grade[i],"C+");
}
else if (total[i]>=45 && total[i]<50)
{
strcpy(grade[i],"C");
}
else if (total[i]>=40 && total[i]<45)
{
strcpy(grade[i],"C-");
}
else if (total[i]>=35 && total[i]<40)
{
strcpy(grade[i],"D+");
}
else if (total[i]>=0 && total[i]<35)
{
strcpy(grade[i],"D");
}
}
printf("No\tMatrix\t\t\tMid term\tFinal\tTotal\tGrade\n");
for(i=0; i<count; i++)
{
printf("%d\t%-10s\t\t%d\t\t%d\t%d\t%s\n", stuNo[i], matrix[i], midterm[i], final[i], total[i], grade[i]);
}
if (fclose(spRead)==EOF)
{
printf("\aError closing %s\n", filename);
printf("\aFile close with error\n");
return;
}
printf("\n\n\n");
printf("Do you wish to sort your data and output to text file?\n\n");
printf("1)Yes. Sort by ascending matrix no.\n");
printf("2)Yes. Sort by descending matrix no.\n");
printf("3)Yes. Sort by ascending total mark.\n");
printf("4)Yes. Sort by descending total mark.\n");
printf("5)Quit.\n");
printf("\n\n\nPlease select your option, eg. [1]: ");
scanf("%d", &option2);
if (option2 == 5)
{
system ("cls");
printf("Thanks for using this programme!\nHave a nice day.\n\n");
return;
}
printf("Please name your output file, eg. [studentsmark.txt]: ");
scanf("%s", filename);
if (!(spout=fopen(filename, "w")))
{
printf("\aError creating file!\n");
return;
}
spout = fopen(filename, "w");
if (option2 == 1)
{
for (i=0; i<count-1; i++)
{
for (j=(i+1); j<count;j++)
{
if ((strcmp(matrix[i],matrix[j])>0))
{
strcpy(tempc, matrix[i]);
strcpy(matrix[i] , matrix[j]);
strcpy(matrix[j] , tempc);
tempn = total[i];
total[i] = total[j];
total[j] = tempn;
strcpy(tempg, grade[i]);
strcpy(grade[i] , grade[j]);
strcpy(grade[j] , tempg);
}
}
}
}
if (option2 == 2)
{
for (i=0; i<count-1; i++)
{
for (j=(i+1); j<count;j++)
{
if ((strcmp(matrix[i],matrix[j])<0))
{
strcpy(tempc, matrix[i]);
strcpy(matrix[i] , matrix[j]);
strcpy(matrix[j] , tempc);
tempn = total[i];
total[i] = total[j];
total[j] = tempn;
strcpy(tempg, grade[i]);
strcpy(grade[i] , grade[j]);
strcpy(grade[j] , tempg);
}
}
}
}
if (option2 == 3)
{
for (i=0; i<count-1; i++)
{
for (j=(i+1); j<count;j++)
{
if (total[i]>total[j])
{
strcpy(tempc, matrix[i]);
strcpy(matrix[i] , matrix[j]);
strcpy(matrix[j] , tempc);
tempn = total[i];
total[i] = total[j];
total[j] = tempn;
strcpy(tempg, grade[i]);
strcpy(grade[i] , grade[j]);
strcpy(grade[j] , tempg);
}
}
}
}
if (option2 == 4)
{
for (i=0; i<count-1; i++)
{
for (j=(i+1); j<count;j++)
{
if (total[i]<total[j])
{
strcpy(tempc, matrix[i]);
strcpy(matrix[i] , matrix[j]);
strcpy(matrix[j] , tempc);
tempn = total[i];
total[i] = total[j];
total[j] = tempn;
strcpy(tempg, grade[i]);
strcpy(grade[i] , grade[j]);
strcpy(grade[j] , tempg);
}
}
}
}
system ("cls");
fprintf (spout,"01234012345678901234567890123456789\n");
fprintf(spout, "No\tMatrix\t\t\tTotal\tGrade\n");
printf("No\tMatrix\t\t\tTotal\tGrade\n");
for(i=0; i<count; i++)
{
fprintf(spout,"%d\t%-10s\t\t%d\t%s\n", stuNo[i], matrix[i], total[i], grade[i]);
printf("%d\t%-10s\t\t%d\t%s\n", stuNo[i], matrix[i], total[i], grade[i]);
}
if (fclose(spout)==EOF)
{
printf("\aError closing file!\n");
return;
}
fclose(spout);
printf("File was closed successfully!\n");
printf("Do you want continue using this programme?\n");
printf("Press [Y] for yes, else press any key to abort\nYour choice: ");
fflush(stdin);
scanf("%c", &ans);
if (ans=='y'||ans=='Y')
{
main();
}
}
else if (option1 == 2)
{
main ();
}
else if (option1 == 3)
{
printf("Thanks for using this programme!\nHave a nice day.\n\n\nProgramme end.\n");
return;
}
else
{
printf("\aWrong input!\nProgramme was terminated!\n");
return;
}
return;
}