c入门第十六篇——学生成绩管理系统

师弟:“师兄,我最近构建了一个学生成绩管理系统,有空试用一下么?”
我:“好啊!”

一个简单的学生成绩管理系统,基本功能包括:添加学生信息、显示所有学生信息、按学号查找学生信息、按姓名查找学生信息、添加学生成绩、修改学生成绩、显示学生的平均成绩、退出系统。

#include 
#include 
#include 

#define MAX_STUDENTS 100
#define MAX_NAME_LEN 50
#define MAX_SEX_LEN 16

typedef struct {
    int id; // 学号
    char name[MAX_NAME_LEN]; // 姓名
    float score; // 成绩
} Student;

Student students[MAX_STUDENTS]; // 学生数组
int student_count = 0; // 学生数量

void add_student()
{
    Student s;

    if(student_count < MAX_STUDENTS) {
        printf("Enter student ID: ");
        scanf("%d", &s.id);
        printf("Enter student name: ");
        scanf("%s", s.name);
        s.score = 0.0; // 初始成绩设置为0
        students[student_count++] = s;
        printf("Student added successfully, all student: %d!\n", student_count);
    } else {
        printf("Database is full!\n");
    }
}

void print_title()
{
    printf("%-4s %-10s %-5s\n", "ID", "Name", "Score");
}

void display_all_students()
{
    int i;

    printf("-------- All students info --------\n");
    if (student_count == 0) {
        printf("No students!\n");
    } else {
        print_title();
        for(i = 0; i < student_count; i++) {
            printf("%-4d %-10s %-.2f\n", students[i].id, students[i].name, students[i].score);
        }
    }
    printf("--------      End       -----------\n");
}

void find_student_by_id()
{
    int id, i;

    printf("Enter student ID to search: ");
    scanf("%d", &id);

    for(i = 0; i < student_count; i++) {
        if(students[i].id == id) {
            print_title();
            printf("%-4d %-10s %-.2f\n", students[i].id, students[i].name, students[i].score);
            return;
        }
    }

    printf("Student with ID %d not found!\n", id);
}

void find_student_by_name()
{
    int i, is_find = 0;
    char name[MAX_NAME_LEN];

    printf("Enter student name to search: ");
    scanf("%s", name);

    for(i = 0; i < student_count; i++) {
        if(strcmp(students[i].name, name) == 0) {
            print_title();
            printf("%-4d %-10s %-.2f\n", students[i].id, students[i].name, students[i].score);
            is_find = 1;
        }
    }

    if (is_find == 0) {
        printf("Student with name %s not found!\n", name);
    }
}

void add_score()
{
    int id, i;
    float score;

    printf("Enter student ID: ");
    scanf("%d", &id);
    printf("Enter student score: ");
    scanf("%f", &score);

    for(i = 0; i < student_count; i++) {
        if(students[i].id == id) {
            students[i].score = score;
            printf("Score added successfully!\n");
            return;
        }
    }
    printf("Student with ID %d not found!\n", id);
}

void display_average_score()
{
    float total = 0.0;
    int i;

    for(i = 0; i < student_count; i++) {
        total += students[i].score;
    }

    printf("Average score of all students: %.2f\n", total / student_count);
}

int main()
{
    int choice;

    do {
        printf("\nStudent Score Management System\n");
        printf("1. Add Student\n");
        printf("2. Display All Students\n");
        printf("3. Find Student by ID\n");
        printf("4. Find Student by Name\n");
        printf("5. Add Score\n");
        printf("6. Display Average Score\n");
        printf("7. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1:
                add_student();
                break;
            case 2:
                display_all_students();
                break;
            case 3:
                find_student_by_id();
                break;
            case 4:
                find_student_by_name();
                break;
            case 5:
                add_score();
                break;
            case 6:
                display_average_score();
                break;
            case 7:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice!\n");
        }
    } while(choice != 7);

    return 0;
}

这个系统涉及到哪些知识呢?
1、c基础库: 头文件包含基本的输入输出,基本的算术运算,字符串操作
2、使用了宏定义类型: 避免无效意义的数字
3、结构体定义: 使用typedef来定义结构体,这样结构体变量声明和定义时,就可以不用struct。如: Student s;
4、全局变量: Student students[MAX_STUDENTS] 记录学生信息;int student_count = 0 记录学生的个数;全局变量所有函数可以直接访问;其对应的是局部变量,只有函数本身可以访问。
5、字符串的比较strcmp
6、do-while循环
7、switch语句

我说:“可以再做两点优化: 一是数据的持久化,二是学生成绩排序,三是删除学生信息。”

你可能感兴趣的:(c语言入门,c语言,开发语言,算法,排序算法,程序人生)