C语言实现学生成绩信息的读取和处理

时间:2024-04-13 18:54:59

功能

读取文件中关于学生姓名、专业、成绩等信息,计算其总成绩和各专业的学生数、平均成绩,并输出。

效果

读取文件:

小红 通信 84 88 84
小明 通信 77 65 58
张三 通信 89 93 95
李四 软件 88 91 94
钱五 软件 95 49 98
王五 网络 71 94 88
大明 网络 45 54 45

程序运行结果:
C语言实现学生成绩信息的读取和处理

源代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define IsCommu(x) (strcmp((x)->major, "通信") == 0) //利用宏判断专业类型
#define IsNet(x) (strcmp((x)->major, "网络") == 0)
#define IsSoft(x) (strcmp((x)->major, "软件") == 0)

struct student //存放学生数据的结构体
{
    char name[100];
    char major[100];
    int eng;
    int alg;
    int c;
    int sum;
} *p = NULL;

struct calc //存放专业统计数据的结构体
{
    char major[100];
    int snum;
    int avg;
} Commu = {"通信", 0, 0}, Net = {"网络", 0, 0}, Soft = {"软件", 0, 0};

int ReadStudentInfo(const char *filename) //打开文件,读取数据
{
    int sum = 0, i;
    char name[100];
    char major[100];
    int eng;
    int alg;
    int c;
    FILE *fp;
    int num = 0;
    struct student *p1 = NULL;
    fp = fopen(filename, "rt");
    if (fp == NULL)
        return -1;
    while (1) //统计学生数量
    {
        if (fscanf(fp, "%s%s%d%d%d", name, major, &eng, &alg, &c, &sum) < 5)
            break;
        num++;
    }
    if (num < 1)
    {
        fclose(fp);
        return num;
    }
    p1 = (struct student *)malloc(sizeof(struct student) * num); //开辟动态存储空间,存放数据
    p = p1;
    fseek(fp, 0, SEEK_SET);
    while (1)
    {
        if (fscanf(fp, "%s%s%d%d%d", name, major, &eng, &alg, &c, &sum) < 5)
            break;
        strcpy(p1->name, name);
        strcpy(p1->major, major);
        p1->eng = eng;
        p1->alg = alg;
        p1->c = c;
        p1++;
    }
    fclose(fp);
    return num;
}

void Count(int num) //计算学生总成绩,计算各专业学生人数和平均分
{
    int i, csum = 0, nsum = 0, wsum = 0;
    for (i = 0; i < num; i++)
    {
        (p + i)->sum = ((p + i)->eng + (p + i)->alg + (p + i)->c);
    }
    for (i = 0; i < num; i++)
    {
        if (IsCommu(p + i))
        {
            Commu.snum++;
            csum += (p + i)->sum;
        }
        if (IsNet(p + i))
        {
            Net.snum++;
            nsum += (p + i)->sum;
        }
        if (IsSoft(p + i))
        {
            Soft.snum++;
            wsum += (p + i)->sum;
        }
    }
    Commu.avg = (csum / Commu.snum);
    Net.avg = (nsum / Net.snum);
    Soft.avg = (wsum / Soft.snum);
}

void print(int num) //显示结果
{
    int i;
    printf("学生信息:\n-----------------------------------------------------\n");
    printf(" 序号 | 姓 名 | 专业名称 | 英语成绩 | 代数成绩 | C语言成绩 | 总成绩\n");
    for (i = 1; i <= num; i++)
    {
        printf("%5d %8s %10s %10d %10d %11d %7d\n", i, p->name, p->major, p->eng, p->alg, p->c, p->sum);
        p++;
    }
    printf("\n统计信息:\n-----------------------------------------------------\n");
    printf(" 序号 专业名称 学生人数 平均成绩\n");
    printf("    1 %8s %8d %8d\n", Commu.major, Commu.snum, Commu.avg);
    printf("    2 %8s %8d %8d\n", Net.major, Net.snum, Net.avg);
    printf("    3 %8s %8d %8d\n", Soft.major, Soft.snum, Soft.avg);
}

int main()
{
    int num;
    struct student *p = NULL;
    char file[100];
    printf("请输入文件路径\n");
    gets(file);
    num = ReadStudentInfo(file);
    if (num < 1)
    {
        printf("读取信息失败!\n");
        system("pause");
        return -1;
    }
    Count(num);
    print(num);
    system("pause");
    return 0;
}

相关文章