统计成绩(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;}
相关推荐
一个很帅的帅哥40 分钟前
JavaScript事件循环
开发语言·前端·javascript
驰羽40 分钟前
[GO]gin框架:ShouldBindJSON与其他常见绑定方法
开发语言·golang·gin
程序员大雄学编程1 小时前
「用Python来学微积分」5. 曲线的极坐标方程
开发语言·python·微积分
Code小翊1 小时前
C语言bsearch的使用
java·c语言·前端
Jose_lz2 小时前
C#开发学习杂笔(更新中)
开发语言·学习·c#
一位代码2 小时前
python | requests爬虫如何正确获取网页编码?
开发语言·爬虫·python
看到我,请让我去学习2 小时前
Qt 控件 QSS 样式大全(通用属性篇)
开发语言·c++·qt
筱砚.2 小时前
【STL——vector容器】
开发语言·c++
lly2024062 小时前
数据访问对象模式(Data Access Object Pattern)
开发语言
std860212 小时前
Rust 与 Python – 这是未来的语言吗?
开发语言·python·rust