您的位置:首页 > 编程语言 > C语言/C++

一道C++小题目。

2010-03-14 20:33 211 查看
今天在CSDN的C++版块看见一道题,题目是:

定义一个score类,其中包括私有数据成员和公有成员函数,即
num 学号
Math 高等数学成绩
English 英语成绩
Programming 程序设计成绩
inscore() 输入学号和各科成绩,并且计算平均成绩
showscore(时) 输出学号和各科成绩
使用score类,输入某班n(事先不能确定)个学生的学号和各科成绩,然后求各个学生的平均成绩,并列表输出学生的学号、各科成绩和平均成绩。

 

下面是我写的代码:

 

#include <iostream>
#include <vector>
using namespace std;

class score
{
public:
score& inscore();
void showscore();
public:
int stu_num;       // 学号
int Math;          // 数学成绩
int English;       // 英语成绩
int Programming;   // 程序设计成绩
int avg_score;     // 平均成绩
};

score& score::inscore()
{
cin >> Math >> English >> Programming;
avg_score = (Math + English + Programming)/3;
return (*this);
}

void score::showscore()
{
cout << Math << "     " << English << "        " << Programming << "             " << avg_score << endl;
}

int main()
{
vector<score> stu_info;
int stu_count;
cout << "Input the students' number:" << endl;
cin >> stu_count;

cout << "Iuput the Math, English, Programming score:" << endl;
while(stu_count--)
{
score stu_temp;
stu_info.push_back( stu_temp.inscore() );
}

cout << endl << endl;

vector<score>::iterator beg = stu_info.begin(),
end = stu_info.end();
cout << "Math" << "   " << "English" << "   " << "Programming" << "   " << "Averige" << endl;
while(beg != end)
{
beg->showscore();
beg++;
}
return 0;
}


 

 

 

看起来挺简单的,可是做起来发现自己对类的实际应用很差很差,做了半天才做出来,在这里把代码发出来。希望大家能指出缺点。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ math iterator input c