学生成绩管理程序的设计与实现 C语言

学生成绩管理程序的设计与实现
(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  /* 课程的数量,只考虑 C、Math、English 这3门课 */

typedef struct student
{
    long studentID;       /* 学号 */
    char studentName[10]; /* 姓名 */
    char className[20];   /* 班级 */
    int score[M];         /* M门课程的分数 */
    float averageScore;   /* 平均分 */
} STUDENT;
STUDENT stu[N]; /* 存储学生信息的结构体数组 */
int n = 0;      /* stu存储的学生的数量 */

const char *scoreFileName = "stu.txt";            /* 存储stu的文件名 */
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; /* 当前选中的课程编号,取值范围是 0,...,M-1 */

typedef bool (*COMPARE)(STUDENT, STUDENT);
void PrintMenu();                                 /* 在控制台中打印菜单 */
void PrintDataFormatTips(bool ignoreStudentID);   /* 在控制台中打印提示语,描述数据输入的格式 */
void InputScore();                                /* 输入数据,包括学号、姓名、班级、以及每门课程的成绩 */
void CalculateAverageScore();                     /* 为每位学生计算平均分 */
void ModifyScore();                               /* 修改成绩 */
bool WriteToFile();                               /* 将stu中保存的数据写入到文件中 */
bool ReadFromFile();                              /* 从文件中读数据,保存到stu中 */
int FindStudentByID(long ID);                     /* 根据学号在stu中查找学生,若找得到则返回该学生在stu中的下标,否则返回-1 */
void Sort(COMPARE compare);                       /* 按照compare函数描述的排序规则,对stu这个数组进行冒泡排序 */
bool CompareByCourseScore(STUDENT a, STUDENT b);  /* 根据第 courseIndex 号课程的成绩进行排序 */
bool CompareByAverageScore(STUDENT a, STUDENT b); /* 根据平均分进行排序 */
bool Equals(char *s, char *d);                    /* 若s和d这两个字符串相同,返回true,否则返回false */

/* 输出到控制台 */

void PrintScore();                         /* 在控制台中打印所有学生的成绩 */
void PrintClassScore(char *className);     /* 打印某个班级所有学生的成绩 */
void PrintStudentScoreAndRanking(long ID); /* 打印某个学生的成绩和排名,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"); /* 0.列出所有学生     */
    printf(" 1. Input students' information                                     \n"); /* 1.输入学生信息     */
    printf(" 2. Modify information by student's ID                              \n"); /* 2.修改学生信息     */
    printf(" 3. Sort by score of individual course                              \n"); /* 3.根据课程成绩排序 */
    printf(" 4. Sort by average score                                           \n"); /* 4.根据平均成绩排序 */
    printf(" 5. Print score report according to class                           \n"); /* 5.打印班级成绩单   */
    printf(" 6. Query score and ranking by student's ID                         \n"); /* 6.查询成绩和排名   */
    printf(" 7. Exit                                                            \n"); /* 7.退出 */
    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();
    }
}
/* 将stu中保存的数据写入到文件中 */
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;
}
/* 从文件中读数据,保存到stu中 */
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;
}
/* 根据学号在stu中查找学生,若找得到则返回该学生在stu中的下标,否则返回-1 */
int FindStudentByID(long ID)
{
    for (int i = 0; i < n; i++)
    {
        if (ID == stu[i].studentID)
        {
            return i;
        }
    }
    return -1;
}
/* 按照compare函数描述的排序规则,对stu这个数组进行冒泡排序 */
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;
        }
    }
}
/* 根据第 courseIndex 号课程的成绩进行排序 */
bool CompareByCourseScore(STUDENT a, STUDENT b)
{
    return a.score[courseIndex] >= b.score[courseIndex];
}
/* 根据平均分进行排序 */
bool CompareByAverageScore(STUDENT a, STUDENT b)
{
    return a.averageScore >= b.averageScore;
}
/* 若s和d这两个字符串相同,返回true,否则返回false */
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");
        }
    }
}
/* 打印某个学生的成绩和排名,ID是该学生的学号 */
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);
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值