码迷,mamicode.com
首页 > 其他好文 > 详细

利用结构体计算每个学生的4门课程的平均分,将各科成绩及平均分输出到score.txt中

时间:2015-04-18 10:01:26      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
typedef struct date
{
    int year;
    int month;
    int day;
}DATE;
typedef struct student
{
    long studentID;
    char studentName[20];
    char studentSex;
    DATE birthday;
    int score[4];
}STUDENT;
int main()
{
    FILE *fp;
    int i,j,sum[30];
    STUDENT stu[30] = {{100310121,"王刚",M,{1991,5,19},{72,83,90,82}},
    {100310122,"李小明",M,{1992,8,20},{88,92,78,78}},
    {100310123,"王力宏",F,{1991,9,19},{98,72,89,66}},
    {100310124,"陈丽丽",F,{1992,3,22},{87,95,78,90}}};
    if(fopen_s(&fp,"score.txt","w") != NULL)
    {    printf_s("get the word");
         system("pause");
        printf_s("Failure to open demo.txt! \n");
        exit(0);
    }
    for(i = 0;i < 4;i++)
    {
        sum[i] = 0;
        for(j = 0;j < 4;j++)
        {
            sum[i] = sum[i] + stu[i].score[j];
        }
        fprintf(fp,"%10ld%8s%3c%6d/%02d/%02d%4d%4d%4d%4d%6.1f\n",stu[i].studentID,
            stu[i].studentName,
        stu[i].studentSex,
        stu[i].birthday .year,
        stu[i].birthday.month,
        stu[i].birthday.day,
        stu[i].score[0],
        stu[i].score[1],
        stu[i].score[2],
        stu[i].score[3],
        sum[i]/4.0);
     }
    fclose(fp);
    system("pause");
    return 0;
}

 

利用结构体计算每个学生的4门课程的平均分,将各科成绩及平均分输出到score.txt中

标签:

原文地址:http://www.cnblogs.com/joyclub/p/4436754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!