Describe CPPStudy_2005_1/STL성ì �처리_3_class here.
~cpp
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> //sort
#include <numeric> //accumulate
const int SUBJECT_NO = 4;
using namespace std;
class student_table
{
public:
void readdata();//파�로부터 ��터를 �어온다
void printdata(); //ì¶œë ¥
void operation(); //ì´�í•©ê³¼ í�‰ê·
vector<student_table>::iterator zbegin() { return ztable.begin(); }
vector<student_table>::iterator zend() { return ztable.end(); }
double average() {return avg;}
private:
string name;
vector<int> score;
unsigned int total;
double avg;
vector<student_table> ztable;
};
bool zcompare(student_table& x,student_table& y); //sort시 비� 조건
int main()
{
student_table z;
//�료를 �는다.
z.readdata();
//ì´�ì �ê³¼ í�‰ê·
z.operation();
//í�‰ê· 으로 ì •ë ¬
sort(z.zbegin(),z.zend(),zcompare);
//ì¶œë ¥
z.printdata();
return 0;
}
//파�로부터 ��터를 �어온다
void student_table::readdata(){
ifstream fin("data.txt");
student_table tmp;
int tmp2,i;
while(1)
{
fin >> tmp.name;
for(i=1;i<=SUBJECT_NO;i++)
{
fin >> tmp2;
tmp.score.push_back(tmp2);
}
if(fin.eof()) break;
ztable.push_back(tmp);
}
}
//sort시 비� 조건
bool zcompare(student_table& x,student_table& y)
{
return x.average() > y.average();
}
//ì¶œë ¥í•˜ê¸°
void student_table::printdata()
{
int cnt=0;
cout << "ì�´ë¦„\têµì–´\tì˜�ì–´\t수학\t과학\tì´�ì �\tí�‰ê· " << endl;
for(vector<student_table>::iterator i=ztable.begin();i<ztable.end();++i)
{
cout << i->name << "\t" <<
i->score[cnt*4+0] << "\t" <<
i->score[cnt*4+1] << "\t" <<
i->score[cnt*4+2] << "\t" <<
i->score[cnt*4+3] << "\t" <<
i->total << "\t" <<
i->avg <<endl;
cnt++;
}
}
void student_table::operation()
{
for(vector<student_table>::iterator i=ztable.begin() ;i<ztable.end();++i)
{
i->total = accumulate(i->score.begin()+i->score.size()-4,i->score.end(),0);
i->avg = i->total/4.0;
}
}










