统计成绩(C语言)

【题目描述】统计成绩:从键盘输入以下10个学生的学号、姓名,以及数学、语文和英语成绩,写到文本文件f3.txt中,再从文件中取出数据,计算每个学生的总成绩和平均分,并将结果显示在屏幕上。试编写相应程序。

3050801 陈刚 81 75 82

3050802 王媛 87 68 85

3050803 李兵 73 84 80

3050804 曹新 76 81 74

3050805 方亮 83 75 71

3050806 何帆 89 78 91

3050807 季东 82 80 72

3050808 林海 72 76 88

3050809 盛天 89 87 76

3050810 高晶 93 86 85

【代码】

c 复制代码
#include <stdio.h>
#include <stdlib.h>
struct student {
    char sid[10];
    char name[40];
    int math;
    int chinese;
    int eng;
    int sum;
    int aver;
}; 
int main(void) {
    FILE *fp;
    if ((fp=fopen("f3.txt", "w")) == NULL) {
        printf("Can't open file");
        exit(0);}
    fprintf(fp, "    学号     姓名   数学  语文  英语\n");
    for (int i=0; i<10; i++) {
        struct student tmp;
        scanf("%s %s %d %d %d", tmp.sid, tmp.name, &tmp.math, &tmp.chinese, &tmp.eng);
        fprintf(fp, "%s  %s    %d     %d     %d\n", tmp.sid, tmp.name, tmp.math, tmp.chinese, tmp.eng);}
    fclose(fp);

    if ((fp=fopen("f3.txt", "r")) == NULL) {
        printf("Can't open file");
        exit(0);}
    printf("  学号   姓名   数学  语文  英语  总成绩  平均分\n");
    int cnt = 0;
    char ch;
    while (!feof(fp)) {
        if ((ch=fgetc(fp)) == '\n') {
            cnt++;}
        if (cnt>0 && cnt<=10) {
            struct student tmp;
            fscanf(fp, "%s %s %d %d %d", tmp.sid, tmp.name, &tmp.math, &tmp.chinese, &tmp.eng);
            tmp.sum = tmp.math + tmp.chinese + tmp.eng;
            tmp.aver = tmp.sum / 3;
            printf("%s  %s    %d    %d    %d    %d     %d\n", tmp.sid, tmp.name, tmp.math, tmp.chinese, tmp.eng, tmp.sum, tmp.aver);}}
    fclose(fp);
    return 0;}
相关推荐
郑州光合科技余经理3 小时前
代驾系统架构拆解:订单链路、权限组织与私有化源码交付
开发语言·后端·算法·架构·系统架构·uni-app·php
码哥DFS6 小时前
二叉树的直径
开发语言·javascript·算法
paopaokaka_luck6 小时前
基于springboot3+vue3的企业考勤管理系统(部门树递归、Echarts图形化分析)
开发语言·spring boot·学习·echarts·mybatis·需求分析·代码规范
梦雨生生8 小时前
java开发工具(学习第一天)
java·开发语言·学习
啊啊啊啊啊!!!!9 小时前
【c++】stack和queue的接口使用以及底层实现
开发语言·c++
松仔log10 小时前
Java中级——组合和继承
android·java·开发语言
上海安当技术11 小时前
C/S 桌面软件怎么接 UKey 强认证?C# SDK 与 C 动态库集成 WinForms/Qt/工控实战
c语言·qt·c#
(Charon)12 小时前
【C++】锁与原子操作(四):自旋锁的完整实现与性能优化
c语言·开发语言·c++
雨田言炎12 小时前
五、Qt控件使用说明大全
开发语言·qt
bu_shuo13 小时前
MATLAB中的meshgrid和ndgrid
开发语言·matlab