3-5 学生成绩统计



题目描述

通过本题目练习可以掌握对象数组的用法,主要是对象数组中数据的输入输出操作。

设计一个学生类Student它具有私有的数据成员:学号、姓名、数学成绩、英语成绩、计算机成绩;具有公有的成员函数:求三门课总成绩的函数int sum();求三门课平均成绩的函数double average();输出学生基本信息、总成绩和平均成绩的函数void print()设置学生数据信息的函数voidset_stu_info(int n,char *p,int m,int e,int c)

请编写主函数,建立学生对象数组,从键盘输入一组学生数据,输出学生的成绩统计表:

stuID姓名数学 英语计算机 总成绩平均成绩

001 xxx 90 85 95 270 90.0

 

002 yyy 95 98 92 285 95.0

输入

输入数据有5行,代表5个学生的信息。

每行有5个数据,数据间用一个空格分隔,分别代表学生的学号、姓名、数学成绩、英语成绩和计算机成绩。除了姓名是符号串外,其他均为整型数据,数据在int类型范围内。

输出

输出数据一共有7行。

第一行输出提示信息“Input the messages of five students(StudentID Name Math English Computer )

第二行输出一个空行,进行输入输出间的间隔

第三行输出表头“StuID Name Math Eng Com Total Average,这一行有7个数据,数据间用制表符\t’分隔,分别代表学生的学号、姓名、数学成绩、英语成绩、计算机成绩、总成绩和平均成绩。其中平均成绩为实型数据,保留1位小数。

4-8行分别输出5个学生的相关数据。每个数据占一个制表符的空间。格式同上。

示例输入

1001 Andy 89 90 93
1002 Mary 93 95 98
1003 Luis 90 85 98
1004 Sam 91 95 98
1005 Lily 87 98 99

示例输出

Input the messages of five students(StudentID Name Math English Computer )

StuID	Name	Math	Eng	Com	Total	Average
1001	Andy	89	90	93	272	90.7
1002	Mary	93	95	98	286	95.3
1003	Luis	90	85	98	273	91.0
1004	Sam	91	95	98	284	94.7
1005	Lily	87	98	99	284	94.7
 
  
分析:又一次用到了头文件"#include <iomanip>",在C++中,保留小数位个数,用到setiosflags(ios::fixed)<<setprecision(1),在结果有小数的情况下,使小数位不为零,把被除数乘以1.0就可以了
 
  
代码:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Student
{
private:
    int stuid;
    string name;
    int math;
    int eng;
    int com;
public:
    int sum();//成绩和
    double average();//平均成绩
    void print();//输出信息
    void set_stu_info();//输入信息
};
void Student::set_stu_info()
{
    cin>>stuid>>name>>math>>eng>>com;
}
int Student::sum()
{
    return math + eng + com;
}
double Student::average()
{
    return (Student::sum())*1.0/3;
}
void Student::print()
 {
     cout<<stuid<<"\t"<<name<<"\t"<<math<<"\t";
     cout<<eng<<"\t"<<com<<"\t"<<Student::sum()<<"\t";
     cout<<setiosflags(ios::fixed)<<setprecision(1)<<Student::average()<<endl;
 }
 int main()
 {
     //float yjy[5];
     Student yjy[5];
     int i;
     for(i = 0; i < 5; i++)
     {
         yjy[i].set_stu_info();
     }
     cout<<"Input the messages of five students(StudentID Name Math English Computer )"<<endl<<endl;
     cout<<"StuID\t"<<"Name\t"<<"Math\t"<<"Eng\t"<<"Com\t"<<"Total\t"<<"Average"<<endl;
     for(i = 0; i < 5; i++)
     {
         yjy[i].print();
     }
     return 0;
 }

猜你喜欢

转载自blog.csdn.net/yjysunshine/article/details/50074259