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

#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;
}

 

posted @ 2015-04-18 09:31  码农@163  阅读(1414)  评论(0编辑  收藏  举报