[YTU]_2429( C语言习题 学生成绩输入和输出)

题目描述

编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括num(学号)、name(姓名)、score[3](3门课的成绩)。编写一个函数input,用来输入5个学生的数据。

输入

5个学生的学号,姓名,3门课的成绩

输出

5个学生的学号,姓名,3门课的成绩

样例输入

1001 zhangsan 100 90 86
1002 lisi 90 20 80
1003 wangwu 90 90 89
1004 yanping 100 100 100
1005 xiaoxiao 60 60 60

样例输出

1001 zhangsan 100 90 86
1002 lisi 90 20 80
1003 wangwu 90 90 89
1004 yanping 100 100 100
1005 xiaoxiao 60 60 60
#include <iostream>
using namespace std;
struct student
{
    int num;
    char name[20];
    float score[3];
};
void input(student stu[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        cin>>stu[i].num>>stu[i].name;
        int j;
        for(j=0;j<3;j++)
        {
            cin>>stu[i].score[j];
        }
    }
}
void print(student stu[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        cout<<stu[i].num<<' '<<stu[i].name<<' ';
        int j;
        for(j=0;j<2;j++)
            cout<<stu[i].score[j]<<' ';
        cout<<stu[i].score[2];
        cout<<endl;
    }
}
 
int main()
{
    const int n=5;
    student stu[n];
    void input(student [],int );
    void print(student [],int );
    input(stu,n);
    print(stu,n);
    return 0;
}

原文地址:https://www.cnblogs.com/sxy201658506207/p/7586382.html