统计成绩(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;}
相关推荐
RuoZoe7 天前
重塑WPF辉煌?基于DirectX 12的现代.NET UI框架Jalium
c语言
祈安_10 天前
C语言内存函数
c语言·后端
郑州光合科技余经理12 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo12312 天前
matlab画图工具
开发语言·matlab
dustcell.12 天前
haproxy七层代理
java·开发语言·前端
norlan_jame12 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone12 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ40220549612 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
czy878747512 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
遥遥江上月12 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js