结构体--成绩统计

Home Web Board ProblemSet Standing Status Statistics
OJ系统新功能测试中,如有问题请联系 17865569030 17865569180 17865571035 尽量不要在上课时间打电话

Problem L: 结构体--成绩统计

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 3317   Solved: 1324
[ Submit][ Status][ Web Board]

Description

建立一个简单的学生信息表,包括:姓名、性别、年龄及一门课程的成绩,统计输出学生的平均成绩和不及格同学姓名和不及格人数。
根据给出的代码实现函数total,只需提交该函数。

#include<iostream>
#include<iomanip>
using namespace std;
struct student
{
    char name[20];
    char sex;
    int age;
    float score;
} ;

void input(struct student stud[],int n)
{
    int i;
    for(i=0; i<n; i++)
    {
        cin>>stud[i].name;  //输入姓名
        cin.get();
        cin>>stud[i].sex;   //输入性别
        cin>>stud[i].age;   //输入年龄
        cin>>stud[i].score; //输入成绩
    }
}


int main()
{
    struct student stud[100];
    int n;
    cin>>n;
    input(stud,n);  //输入n个学生的信息
    total(stud,n);  //统计并输出平均成绩和不及格同学姓名和不及格人数
    return 0;
}

Input

n 和 n个学生的姓名,性别,年龄,成绩

Output

学生的平均成绩和不及格同学姓名和不及格人数。

Sample Input

3
zhangsan M 20 80
lisi F 19 59
wangwu F 20 100

Sample Output

lisi不及格
平均成绩为:79.7
不及格人数为:1

HINT

[ Submit][ Status][ Web Board]

代码

#include<iostream>
#include<iomanip>
using namespace std;
struct student
{
    char name[20];
    char sex;
    int age;
    float score;
} ;

void input(struct student stud[],int n)
{
    int i;
    for(i=0; i<n; i++)
    {
        cin>>stud[i].name;  //输入姓名
        cin.get();
        cin>>stud[i].sex;   //输入性别
        cin>>stud[i].age;   //输入年龄
        cin>>stud[i].score; //输入成绩
    }
}
void total(struct student stud[], int n)
{
    int notpass = 0;
    float grade = 0;
    int i ,j=0;
    for(i= 0 ;i< n ;i++)
    {
        if(stud[i].score <60)
        {

            notpass ++;

        }


        grade+=stud[i].score ;

    }
    for(i=0 ;i <n ;i++)
    {
        if(stud[i].score<60)
           cout<<stud[i].name<<"不及格"<<endl;

    }


    cout<<"平均成绩为:"<<setiosflags(ios::fixed)<<setprecision(1)<<(grade/n)<<endl;
    cout<<"不及格人数为:"<<notpass;

}
int main()
{
    struct student stud[100];
    int n;
    cin>>n;
    input(stud,n);  //输入n个学生的信息
    total(stud,n);  //统计并输出平均成绩和不及格同学姓名和不及格人数
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/80820981
今日推荐