学生成绩问题。

一、问题描述。

  有5个学生,每个学生的数据包括学号、姓名、三门课成绩,从键盘输入5个学生的数据,要求计算并输出。

1)  每个学生三门课的总成绩

2)  三门课每门课程的平均成绩

 
代码实现。


#include<string>  
#include<iostream>  
using namespace std;  
  
#define N 5 //定义常量N=5  
  
// 定义学生结构体  
typedef struct  
{  
    string no;      //学号  
    string name;    //姓名  
    float chinese;  //语文  
    float math;     //数学  
    float english;  //英语  
    float total;    //总成绩  
}Student;  
  
// 定义5个学生  
Student stu[N];  
  
/* 
 *  录入每个学生的成绩等 
 */  
void input(int n)  
{  
    string no;      //学号  
    string name;    //姓名  
    float chinese;  //语文  
    float math;     //数学  
    float english;  //英语  
  
    for(int i=0; i!=n; i++)  
    {  
        cout<<"请输入第"<<i+1<<"个学生的学号:";  
        cin>>no;  
        stu[i].no = no;  
        cout<<"请输入该学生的姓名:";  
        cin>>name;  
        stu[i].name = name;  
        cout<<"请输入该学生语文成绩:";  
        cin>>chinese;  
        stu[i].chinese = chinese;  
        cout<<"请输入该学生数学成绩:";  
        cin>>math;  
        stu[i].math = math;  
        cout<<"请输入该学生英语成绩:";  
        cin>>english;  
        stu[i].english = english;  
          
        // 计算该学生三门课的总成绩  
        stu[i].total = stu[i].chinese + stu[i].math + stu[i].english;  
    }  
}  
  
/* 
 *  打印三门课每门课程的平均成绩 
 */  
void printAvg()  
{  
    int i = 0;  
    float sumChinese=0, sumMath=0, sumEnglish=0;   
    float avgChinese=0, avgMath=0, avgEnglish=0;  
  
    for(i=0; i<N; i++)  
    {  
        sumChinese += stu[i].chinese;  
        sumMath += stu[i].math;  
        sumEnglish += stu[i].english;  
    }  
      
    avgChinese = sumChinese / N;  
    avgMath = sumMath / N;  
    avgEnglish = sumEnglish / N;  
  
    cout<<"语文课:"<<avgChinese<<"/r/n";  
    cout<<"数学课:"<<avgMath<<"/r/n";  
    cout<<"英语课:"<<avgEnglish<<"/r/n";  
}  
  
/* 
 *  主函数 
 */  
void main()  
{  
    // 录入每个学生的成绩等  
    input(N);  
  
    cout<<"/r/n---------- 统计结果 ----------/r/n";  
    cout<<"以下是每个学生三门课的总成绩:"<<"/r/n";  
    for (int i=0; i<N; i++)  
    {  
        cout<<stu[i].name<<":"<<"/t"<<stu[i].total<<"/r/n";  
    }  
  
    cout<<"/r/n以下是每门课的平均成绩:"<<"/r/n";  
    printAvg();  
  
    // 接收多余的"/r/n"  
    getchar();  
    getchar();  
}

posted @ 2023-05-14 21:16  liuxuechao  阅读(40)  评论(0编辑  收藏  举报