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;
}
}