学生成绩管理程序的设计与实现 (1)对学生的姓名、各科成绩进行输入和修改; (2)老师可以对成绩按各种条件进行查询、统计、排名; (3)以班为单位打印成绩单; (4)学生对自己的成绩和排名进行查询; (5)将(2)-(4)的结果写入文件中。
#include <stdio.h>
#define bool int
#define true 1
#define false 0
#define N 30
#define M 3
typedef struct student
{
long studentID;
char studentName[10];
char className[20];
int score[M];
float averageScore;
} STUDENT;
STUDENT stu[N];
int n = 0;
const char *scoreFileName = "stu.txt";
const char *sortedScoreFileName = "sorted.txt";
const char *classScoreFileName = "class.txt";
const char *studentScoreFileName = "student.txt";
char course[M][10] = {"C", "Math", "English"};
int courseIndex = 0;
typedef bool (*COMPARE)(STUDENT, STUDENT);
void PrintMenu();
void PrintDataFormatTips(bool ignoreStudentID);
void InputScore();
void CalculateAverageScore();
void ModifyScore();
bool WriteToFile();
bool ReadFromFile();
int FindStudentByID(long ID);
void Sort(COMPARE compare);
bool CompareByCourseScore(STUDENT a, STUDENT b);
bool CompareByAverageScore(STUDENT a, STUDENT b);
bool Equals(char *s, char *d);
void PrintScore();
void PrintClassScore(char *className);
void PrintStudentScoreAndRanking(long ID);
void PrintScoreToFile(const char *fileName);
void PrintClassScoreToFile(char *className, const char *fileName);
void PrintStudentScoreAndRankingToFile(long ID, const char *fileName);
int main()
{
while (true)
{
PrintMenu();
int operation;
scanf("%d", &operation);
while (operation < 0 || operation > 7)
{
printf("Error: The number %d is not between 0 and 7 ! \n", operation);
PrintMenu();
scanf("%d", &operation);
}
if (operation == 7)
{
return 0;
}
if (operation != 1 && false == ReadFromFile())
{
printf("Error: Please select operation 1 and input the scores ! \n");
continue;
}
switch (operation)
{
case 0:
{
PrintScore();
break;
}
case 1:
{
InputScore();
WriteToFile();
break;
}
case 2:
{
PrintScore();
ModifyScore();
break;
}
case 3:
{
for (int i = 0; i < M; i++)
{
printf(" %d. %s\n", i, course[i]);
}
printf("Please input the course number:");
scanf("%d", &courseIndex);
if (courseIndex < 0 || courseIndex >= M)
{
printf("Error: input the wrong number! \n");
break;
}
Sort(CompareByCourseScore);
printf("This is the result sorted by %s: \n", course[courseIndex]);
PrintScore();
PrintScoreToFile(sortedScoreFileName);
break;
}
case 4:
{
CalculateAverageScore();
Sort(CompareByAverageScore);
printf("This is the result sorted by average score: \n");
PrintScore();
PrintScoreToFile(sortedScoreFileName);
break;
}
case 5:
{
char className[20];
printf("Please input the class:");
scanf("%s", className);
printf("This is the score report of %s: \n", className);
PrintClassScore(className);
PrintClassScoreToFile(className, classScoreFileName);
break;
}
case 6:
{
long ID;
printf("Please input the student's ID:");
scanf("%ld", &ID);
if (FindStudentByID(ID) == -1)
{
printf("Error: There is no student with ID %ld \n", ID);
break;
}
PrintStudentScoreAndRanking(ID);
PrintStudentScoreAndRankingToFile(ID, studentScoreFileName);
break;
}
}
printf("\n");
}
return 0;
}
void PrintMenu()
{
printf("--------------------------------MENU--------------------------------\n");
printf(" 0. Show all students \n");
printf(" 1. Input students' information \n");
printf(" 2. Modify information by student's ID \n");
printf(" 3. Sort by score of individual course \n");
printf(" 4. Sort by average score \n");
printf(" 5. Print score report according to class \n");
printf(" 6. Query score and ranking by student's ID \n");
printf(" 7. Exit \n");
printf("--------------------------------------------------------------------\n");
printf("Please input the operation number:");
}
void PrintDataFormatTips(bool ignoreStudentID)
{
printf("This is the input format of student information:\n");
if (ignoreStudentID)
{
printf("%10s%20s", "name", "class");
}
else
{
printf("%10s%10s%20s", "ID", "name", "class");
}
for (int i = 0; i < M; i++)
{
printf("%10s", course[i]);
}
printf("\n");
}
void InputScore()
{
printf("How many student? ");
scanf("%d", &n);
PrintDataFormatTips(false);
for (int i = 0; i < n; i++)
{
printf("Input record %d:\n", i + 1);
scanf("%ld", &stu[i].studentID);
scanf("%s", stu[i].studentName);
scanf("%s", stu[i].className);
for (int j = 0; j < M; j++)
{
scanf("%d", &stu[i].score[j]);
}
}
}
void CalculateAverageScore()
{
int sum[N];
for (int i = 0; i < n; i++)
{
sum[i] = 0;
for (int j = 0; j < M; j++)
{
sum[i] = sum[i] + stu[i].score[j];
}
stu[i].averageScore = (float)sum[i] / M;
}
}
void ModifyScore()
{
printf("Which student would you want to modify? Input the ID: ");
long ID;
scanf("%ld", &ID);
int index = FindStudentByID(ID);
if (-1 == index)
{
printf("Error: There is no student with ID %ld \n", ID);
return;
}
printf("Please input the new information \n");
PrintDataFormatTips(true);
scanf("%s", stu[index].studentName);
scanf("%s", stu[index].className);
for (int j = 0; j < M; j++)
{
scanf("%d", &stu[index].score[j]);
}
WriteToFile();
if (ReadFromFile())
{
printf("This is the new student list: \n");
PrintScore();
}
}
bool WriteToFile()
{
FILE *f = fopen(scoreFileName, "w");
if (NULL == f)
{
printf("Error: Fail to open the file %s !\n", scoreFileName);
return false;
}
fprintf(f, "%d", n);
for (int i = 0; i < n; i++)
{
fprintf(f, "%10ld%10s%20s",
stu[i].studentID,
stu[i].studentName,
stu[i].className);
for (int j = 0; j < M; j++)
{
fprintf(f, "%10ld", stu[i].score[j]);
}
fprintf(f, "\n");
}
fclose(f);
return true;
}
bool ReadFromFile()
{
FILE *f = fopen(scoreFileName, "r");
if (NULL == f)
{
printf("Error: Fail to open the file %s !\n", scoreFileName);
return false;
}
fscanf(f, "%d", &n);
for (int i = 0; i < n; i++)
{
fscanf(f, "%ld", &stu[i].studentID);
fscanf(f, "%s", stu[i].studentName);
fscanf(f, "%s", stu[i].className);
for (int j = 0; j < M; j++)
{
fscanf(f, "%d", &stu[i].score[j]);
}
}
fclose(f);
return true;
}
int FindStudentByID(long ID)
{
for (int i = 0; i < n; i++)
{
if (ID == stu[i].studentID)
{
return i;
}
}
return -1;
}
void Sort(COMPARE compare)
{
for (int i = 0; i < n - 1; i++)
{
bool swap = false;
for (int j = 0; j < n - 1 - i; j++)
{
if (compare(stu[j], stu[j + 1]) == false)
{
STUDENT temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
swap = true;
}
}
if (swap == false)
{
return;
}
}
}
bool CompareByCourseScore(STUDENT a, STUDENT b)
{
return a.score[courseIndex] >= b.score[courseIndex];
}
bool CompareByAverageScore(STUDENT a, STUDENT b)
{
return a.averageScore >= b.averageScore;
}
bool Equals(char *s, char *d)
{
int i = 0;
while (s[i] != '\0' && d[i] != '\0')
{
if (s[i] != d[i])
{
return false;
}
i++;
}
return s[i] == '\0' && d[i] == '\0';
}
void PrintScore()
{
CalculateAverageScore();
printf("%10s%10s%20s",
"ID",
"name",
"class");
for (int j = 0; j < M; j++)
{
printf("%10s", course[j]);
}
printf("%10s", "average");
printf("\n");
for (int i = 0; i < n; i++)
{
printf("%10ld%10s%20s",
stu[i].studentID,
stu[i].studentName,
stu[i].className);
for (int j = 0; j < M; j++)
{
printf("%10ld", stu[i].score[j]);
}
printf("%10.1f", stu[i].averageScore);
printf("\n");
}
}
void PrintClassScore(char *className)
{
CalculateAverageScore();
printf("%10s%10s%20s",
"ID",
"name",
"class");
for (int j = 0; j < M; j++)
{
printf("%10s", course[j]);
}
printf("%10s", "average");
printf("\n");
for (int i = 0; i < n; i++)
{
if (Equals(stu[i].className, className))
{
printf("%10ld%10s%20s",
stu[i].studentID,
stu[i].studentName,
stu[i].className);
for (int j = 0; j < M; j++)
{
printf("%10ld", stu[i].score[j]);
}
printf("%10.1f", stu[i].averageScore);
printf("\n");
}
}
}
void PrintStudentScoreAndRanking(long ID)
{
CalculateAverageScore();
int rankings[M];
int averageRanking;
for (int i = 0; i < M; i++)
{
courseIndex = i;
Sort(CompareByCourseScore);
rankings[i] = FindStudentByID(ID) + 1;
}
Sort(CompareByAverageScore);
int index = FindStudentByID(ID);
averageRanking = index + 1;
printf("This is the score report of %s: \n", stu[index].studentName);
printf("%10s : %10ld \n", "ID", stu[index].studentID);
printf("%10s : %10s \n", "name", stu[index].studentName);
printf("%10s : %10s \n", "class", stu[index].className);
for (int i = 0; i < M; i++)
{
printf("%10s :%10.1f ranking: %d \n", course[i], (float)stu[index].score[i], rankings[i]);
}
printf("%10s :%10.1f ranking: %d \n", "average", stu[index].averageScore, averageRanking);
}
void PrintScoreToFile(const char *fileName)
{
CalculateAverageScore();
FILE *f = fopen(fileName, "w");
if (NULL == f)
{
printf("Error: Fail to open the file %s !\n", fileName);
return;
}
fprintf(f, "%10s%10s%20s",
"ID",
"name",
"class");
for (int j = 0; j < M; j++)
{
fprintf(f, "%10s", course[j]);
}
fprintf(f, "%10s", "average");
fprintf(f, "\n");
for (int i = 0; i < n; i++)
{
fprintf(f, "%10ld%10s%20s",
stu[i].studentID,
stu[i].studentName,
stu[i].className);
for (int j = 0; j < M; j++)
{
fprintf(f, "%10ld", stu[i].score[j]);
}
fprintf(f, "%10.1f", stu[i].averageScore);
fprintf(f, "\n");
}
fclose(f);
}
void PrintClassScoreToFile(char *className, const char *fileName)
{
CalculateAverageScore();
FILE *f = fopen(fileName, "w");
if (NULL == f)
{
printf("Error: Fail to open the file %s !\n", fileName);
return;
}
fprintf(f, "%10s%10s%20s",
"ID",
"name",
"class");
for (int j = 0; j < M; j++)
{
fprintf(f, "%10s", course[j]);
}
fprintf(f, "%10s", "average");
fprintf(f, "\n");
for (int i = 0; i < n; i++)
{
if (Equals(stu[i].className, className))
{
fprintf(f, "%10ld%10s%20s",
stu[i].studentID,
stu[i].studentName,
stu[i].className);
for (int j = 0; j < M; j++)
{
fprintf(f, "%10ld", stu[i].score[j]);
}
fprintf(f, "%10.1f", stu[i].averageScore);
fprintf(f, "\n");
}
}
fclose(f);
}
void PrintStudentScoreAndRankingToFile(long ID, const char *fileName)
{
CalculateAverageScore();
FILE *f = fopen(fileName, "w");
if (NULL == f)
{
printf("Error: Fail to open the file %s !\n", fileName);
return;
}
int rankings[M];
int averageRanking;
for (int i = 0; i < M; i++)
{
courseIndex = i;
Sort(CompareByCourseScore);
rankings[i] = FindStudentByID(ID) + 1;
}
Sort(CompareByAverageScore);
int index = FindStudentByID(ID);
averageRanking = index + 1;
fprintf(f, "This is the score report of %s: \n", stu[index].studentName);
fprintf(f, "%10s : %10ld \n", "ID", stu[index].studentID);
fprintf(f, "%10s : %10s \n", "name", stu[index].studentName);
fprintf(f, "%10s : %10s \n", "class", stu[index].className);
for (int i = 0; i < M; i++)
{
fprintf(f, "%10s :%10.1f ranking: %d \n", course[i], (float)stu[index].score[i], rankings[i]);
}
fprintf(f, "%10s :%10.1f ranking: %d \n", "average", stu[index].averageScore, averageRanking);
fclose(f);
}
|