C语言:学生成绩管理系统(含源代码)

一.功能

二.源代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM 100
typedef struct
{
    char no[30];
    char name[10];
    char sex[10];
    char phone[20];
    float cyuyan;
    float computer;
    float datastruct;
} *student, student1;

typedef struct
{
    student stu[MAX_NUM];
    int number;
} *studentDB;

void read(studentDB temp)
{
    FILE *infile;
    infile = fopen("./数据.txt", "r");
    if (!infile)
    {
        printf("文件打开失败!");
        exit(0);
    }
    while (!feof(infile))
    {
        temp->stu[temp->number] = malloc(sizeof(student1));
        fscanf(infile, "%s %s %s %s %f %f %f", temp->stu[temp->number]->no, temp->stu[temp->number]->name, temp->stu[temp->number]->sex, temp->stu[temp->number]->phone, &(temp->stu[temp->number]->cyuyan), &(temp->stu[temp->number]->computer), &(temp->stu[temp->number]->datastruct));
        temp->number++;
    }
    fclose(infile);
}

void write(studentDB temp)
{
    FILE *outfile;
    outfile = fopen("./数据.txt", "w");
    if (!outfile)
    {
        printf("文件打开失败!");
        exit(1);
    }
    if (temp && temp->number > 0)
    {
        int i;
        for (i = 0; i < temp->number; i++)
        {
            if (i == temp->number - 1)
            {
                fprintf(outfile, "%s %s %s %s %.2f %.2f %.2f", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);
            }
            else
            {
                fprintf(outfile, "%s %s %s %s %.2f %.2f %.2f\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);
            }
        }
    }
    fclose(outfile);
    printf("保存成功");
    read(temp);
}

void display(studentDB temp)
{
    int i;
    printf("|   学号   |姓名|性别|  手机号  |c语言|英语|高等数学|\n");
    for (i = 0; i < temp->number; i++)
    {
        printf("%s %s %s %s %.2f %.2f %.2f\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);
    }
}

void menu()
{
    printf("\n\n\t****************************简单学生信息管理系统*****************************\n");
    printf("\t*                              1.显示学生信息                             *|\n");
    printf("\t*                              2.增加学生信息                             *|\n");
    printf("\t*                              3.删除学生信息                             *|\n");
    printf("\t*                              4.修改学生信息                             *|\n");
    printf("\t*                              5.查询学生信息                             *|\n");
    printf("\t*                              6.排序学生成绩                             *|\n");
    printf("\t*                              7.计算学生平均成绩                         *|\n");
    printf("\t*                              8.保存学生信息                             *|\n");
    printf("\t*                              9.统计全部课程及格人数                     *|\n");
    printf("\t*                              10.输出总成绩最高的学生信息                *|\n");
    printf("\t*                              0.退出系统                                 *|\n");
    printf("\t***************************************************************************\n");
    printf("请选择你的操作并将序号输入:");
}

int countDigits(long long n)
{
    int count = 0;
    while (n > 0)
    {
        n /= 10;
        count++;
    }
    return count;
}

void insert(studentDB temp)
{
    char no[30];
    printf("请输入要添加学生的学号:");
    scanf("%s", no);
    int n;
    long long num;
    sscanf(no, "%lld", &num);
    n = countDigits(num);
    if (n != 11)
    {
        printf("输入的学号位数有误,请重新输入!");
        return;
    }
    else
    {
        student1 stu;
        FILE *outfile;
        outfile = fopen("./数据.txt", "a");
        if (!outfile)
        {
            printf("文件打开失败!");
            exit(0);
        }
        strcpy(stu.no, no);
        printf("请输入姓名:");
        scanf("%s", stu.name);
        printf("请输入性别:");
        scanf("%s", stu.sex);
        printf("请输入手机号:");
        scanf("%s", stu.phone);
        printf("请输入c语言成绩(带小数点):");
        scanf("%f", &stu.cyuyan);
        printf("请输入英语成绩(带小数点):");
        scanf("%f", &stu.computer);
        printf("请输入高等数学成绩(带小数点):");
        scanf("%f", &stu.datastruct);
        n = fprintf(outfile, "\n%s %s %s %s %f %f %f", stu.no, stu.name, stu.sex, stu.phone, stu.cyuyan, stu.computer, stu.datastruct);
        printf("%s %s %s %s %.2f %.2f %.2f", stu.no, stu.name, stu.sex, stu.phone, stu.cyuyan, stu.computer, stu.datastruct);
        printf("学生信息已成功插入!信息长度:%d\n", n);
        fclose(outfile);
        read(temp);
        return;
    }
}

void delete(studentDB temp)
{
    printf("请输入要删除的学生信息学号:");
    char no[15];
    int i, k;
    scanf("%s", no);
    printf("%s", no);
    for (i = 0; i < temp->number; i++)
    {
        if (strcmp(temp->stu[i]->no, no) == 0)
        {
            for (k = i; k < temp->number; k++)
            {
                temp->stu[k] = temp->stu[k + 1];
            }
            free(temp->stu[temp->number]);
            temp->number--;
            printf("学生信息已成功删除!\n");
            return;
        }
    }
    printf("学生学号输入错误!");
    return;
}

void modify(studentDB temp)
{
    printf("请输入要修改的学生信息学号:");
    char no[15];
    int i, flag = 0;
    long num;
    scanf("%s", no);
    sscanf(no, "%ld", &num);
    for (i = 0; i < temp->number; i++)
    {
        if (strcmp(no, temp->stu[i]->no) == 0)
        {
            printf("\n学号:%s  姓名:%s  性别:%s  手机号:%s  c语言:%.2f  英语:%.2f  高等数学:%.2f\n\n\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);
            printf("|1.学号|2.姓名|3.性别|4.手机号|5.c语言成绩|6.英语成绩|7.高等数学成绩|8.不改动并返回菜单|9.依次修改全部数据\n\n\n请输入要改动的数据项:");
            scanf("%d", &flag);
            switch (flag)
            {
            case 1:
                printf("请输入要修改的学号:");
                scanf("%s", temp->stu[i]->no);
                break;
            case 2:
                printf("请输入姓名:");
                scanf("%s", temp->stu[i]->name);
                break;
            case 3:
                printf("请输入性别:");
                scanf("%s", temp->stu[i]->sex);
                break;
            case 4:
                printf("请输入手机号:");
                scanf("%s", temp->stu[i]->phone);
                break;
            case 5:
                printf("请输入c语言成绩(带小数点):");
                scanf("%f", &(temp->stu[i]->cyuyan));
                break;
            case 6:
                printf("请输入英语成绩(带小数点):");
                scanf("%f", &(temp->stu[i]->computer));
                break;
            case 7:
                printf("请输入高等数学成绩(带小数点):");
                scanf("%f", &(temp->stu[i]->datastruct));
                break;
            case 8:
                return;
            case 9:
                printf("请输入姓名:");
                scanf("%s", temp->stu[i]->name);
                printf("请输入性别:");
                scanf("%s", temp->stu[i]->sex);
                printf("请输入手机号:");
                scanf("%s", temp->stu[i]->phone);
                printf("请输入c语言成绩(带小数点):");
                scanf("%f", &(temp->stu[i]->cyuyan));
                printf("请输入英语成绩(带小数点):");
                scanf("%f", &(temp->stu[i]->computer));
                printf("请输入高等数学成绩(带小数点):");
                scanf("%f", &(temp->stu[i]->datastruct));
                break;
            default:
                printf("请重新选择!");
                break;
            }
            return;
        }
    }
}

void search(studentDB temp)
{
    printf("请输入要查询的学生信息学号:");
    char no[30];
    int i, n;
    long long num;
    scanf("%s", no);
    sscanf(no, "%lld", &num);
    n = countDigits(num);
    if (n != 11)
    {
        printf("输入的学号位数有误,请重新输入!");
        return;
    }
    else
    {
        for (i = 0; i < temp->number; i++)
        {
            if (strcmp(temp->stu[i]->no, no) == 0)
            {
                printf("\n学号:%s  姓名:%s  性别:%s  手机号:%s  c语言:%.2f  英语:%.2f  高等数学:%.2f\n\n\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);
                return;
            }
        }
        printf("学生学号输入错误!");
        return;
    }
}

void sort(studentDB temp)
{
    int i, j, n;
    float t;
    for (i = 0; i < temp->number; i++)
    {
        for (j = i + 1; j < temp->number; j++)
        {
            if ((temp->stu[i]->cyuyan + temp->stu[i]->computer + temp->stu[i]->datastruct) < (temp->stu[j]->cyuyan + temp->stu[j]->computer + temp->stu[j]->datastruct))
            {
                student1 *tempstu = temp->stu[i];
                temp->stu[i] = temp->stu[j];
                temp->stu[j] = tempstu;
            }
        }
    }
    printf("已按成绩排序完成!\n");
}

void average(studentDB temp)
{
    int i;
    float ctotal = 0, comtotal = 0, dstotal = 0;
    for (i = 0; i < temp->number; i++)
    {
        ctotal += temp->stu[i]->cyuyan;
        comtotal += temp->stu[i]->computer;
        dstotal += temp->stu[i]->datastruct;
    }
    printf("C语言平均成绩:%.2f 英语平均成绩:%.2f 高等数学平均成绩:%.2f\n", ctotal / temp->number, comtotal / temp->number, dstotal / temp->number);
}

void passing(studentDB temp)
{
    int i, cpass = 0, compass = 0, dpass = 0;
    for (i = 0; i < temp->number; i++)
    {
        if (temp->stu[i]->cyuyan >= 60)
        {
            cpass++;
        }
        if (temp->stu[i]->computer >= 60)
        {
            compass++;
        }
        if (temp->stu[i]->datastruct >= 60)
        {
            dpass++;
        }
    }
    printf("C语言及格人数:%d 英语及格人数:%d 高等数学及格人数:%d\n", cpass, compass, dpass);
}

void max(studentDB temp)
{
    int i, index = 0;
    float maxScore = 0;
    for (i = 0; i < temp->number; i++)
    {
        float totalScore = temp->stu[i]->cyuyan + temp->stu[i]->computer + temp->stu[i]->datastruct;
        if (totalScore > maxScore)
        {
            maxScore = totalScore;
            index = i;
        }
    }
    printf("学号:%s  姓名:%s  性别:%s  手机号:%s  c语言:%.2f  英语:%.2f  高等数学:%.2f 总成绩:%.2f\n", temp->stu[index]->no, temp->stu[index]->name, temp->stu[index]->sex, temp->stu[index]->phone, temp->stu[index]->cyuyan, temp->stu[index]->computer, temp->stu[index]->datastruct, maxScore);
}

int main()
{
    int select;
    studentDB stu = malloc(sizeof(studentDB));
    stu->number = 0;
    read(stu);
    do
    {
        menu();
        scanf("%d", &select);
        switch (select)
        {
        case 1:
            display(stu);
            break;
        case 2:
            insert(stu);
            break;
        case 3:
            delete (stu);
            break;
        case 4:
            modify(stu);
            break;
        case 5:
            search(stu);
            break;
        case 6:
            sort(stu);
            break;
        case 7:
            average(stu);
            break;
        case 8:
            write(stu);
            break;
        case 9:
            passing(stu);
            break;
        case 10:
            max(stu);
            break;
        case 0:
            exit(1);
        }
    } while (1);
    return 0;
}

三.注意事项

(1)事先准备: 数据.txt 文件(可以含有学生信息,可以空白,但是必须有)

(2)学号为11

(3)该代码不含有C99或者C11

  • 31
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值