一道C++小题目。
2010-03-14 20:33
211 查看
今天在CSDN的C++版块看见一道题,题目是:
定义一个score类,其中包括私有数据成员和公有成员函数,即
num 学号
Math 高等数学成绩
English 英语成绩
Programming 程序设计成绩
inscore() 输入学号和各科成绩,并且计算平均成绩
showscore(时) 输出学号和各科成绩
使用score类,输入某班n(事先不能确定)个学生的学号和各科成绩,然后求各个学生的平均成绩,并列表输出学生的学号、各科成绩和平均成绩。
下面是我写的代码:
看起来挺简单的,可是做起来发现自己对类的实际应用很差很差,做了半天才做出来,在这里把代码发出来。希望大家能指出缺点。
定义一个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语言/C++】算术移位和逻辑移位以及一道移位的题目
- [C++]关于多态的一道题目
- C++题目一道: 重载`->': 您真的懂成员访问运算符的重载吗?
- 关于C++临时对象的一道题目
- 一道经典的C++题,关于分钱的问题,适合新手阅读(黑客X档案论坛题目) [c#]
- 一道比较好玩的C++题目
- 一道经典的C++题,关于分钱的问题,适合新手阅读(黑客X档案论坛题目)
- 一道c++的算法题目
- 【C++】一道考察重载、覆盖、多态的题目
- 请教一道C++题目
- 一道简单的C++题目
- c++一道学生类题目
- C++ 一道简单的题目引发的思考
- 一道有关球赛队员分配的C++程序题目
- 这是一道题目……C++的~初步弄出来
- 一道考察类的纯虚函数定义,静态变量等C++题目
- 一道有关球赛队员分配的C++程序题目
- 【C语言/C++】算术移位和逻辑移位以及一道移位的题目
- 一道经典的C++题,关于分钱的问题,适合新手阅读(黑客X档案论坛题目) [c#]
- [memory]C++中栈区 堆区 常量区(由一道面试题目而学习)