U E D R , A S I H C RSS

CPP Study_2005_1/STL성적처리_1

CPPStudy_2005_1/STL

  • 01
  • ..

InputText

~cpp 
       
 59 98 75 91 
탁 66 78 77 84 
 52 50 63 72 
 97 55 52 97 
 78 82 63 73 
 72 66 73 52 
 95 62 94 53 
 78 53 74 75 
태호 66 82 82 94 

code

~cpp 
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
#define		SUBJECT_SIZE	4
struct Student_info {
	string name;
	vector<int>  subjects;
	int total;
	double average;
};
double Sum(Student_info &s);
void totalSum(vector<Student_info> &students);
double average(Student_info &s);
void totalAverage(vector<Student_info> &students);
bool totalCompare(const Student_info &s1, const Student_info &s2);
void sortBySum(vector<Student_info> &students);
ostream& displayScores(ostream &out, const vector<Student_info> &students);
istream& readScores(istream &in, ifstream &fin, vector<Student_info> &students);
int main()
{
	ifstream fin("score.txt");
	vector<Student_info> students;
	readScores(cin,fin,students);
	totalSum(students);
	totalAverage(students);
	sortBySum(students);	
	displayScores(cout,students);
	return 0;
}
double Sum(Student_info &s)
{
	s.total=accumulate(s.subjects.begin()+(s.subjects.size()-4),s.subjects.end(),0);
	return s.total;
}
void totalSum(vector<Student_info> &students)
{
	vector<int> sum;
	transform(students.begin(),students.end(),back_inserter(sum),Sum);	
}
double average(Student_info &s)
{
	return s.average=Sum(s)/SUBJECT_SIZE;
}
void totalAverage(vector<Student_info> &students)
{
	vector<double> averageScore;
	transform(students.begin(),students.end(),back_inserter(averageScore),average);	
}
void sortBySum(vector<Student_info> &students)
{
	sort(students.begin(),students.end(),totalCompare);
}
bool totalCompare(const Student_info &s1, const Student_info &s2)
{
	return s1.total > s2.total;
}
ostream& displayScores(ostream &out, const vector<Student_info> &students)
{
	for(vector<Student_info>::const_iterator it = students.begin() ; it!=students.end();++it)
		out<<it->name<<" - totalSum : "<<it->total<<" average : "<<it->average<<"\n";
	return out;
}
istream& readScores(istream &in, ifstream &fin, vector<Student_info> &students)
{
	string name, temp;
	int scoreTemp;
	Student_info student;
	vector<string> subject;
	fin>> name;
	while(fin>>temp && subject.size()!=SUBJECT_SIZE)
		subject.push_back(temp);
	do
	{		
		student.name = temp;
		for(vector<string>::const_iterator it = subject.begin() ; 
			it!=subject.end() && fin>>scoreTemp;++it)
				student.subjects.push_back(scoreTemp);
		students.push_back(student);
	}while(fin>>temp);
	return in;
}

thread

~cpp 
 s.total=accumulate(s.subjects.begin()+(s.subjects.size()-4),s.subjects.end(),0);
  • . s.begin() s.end() .
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:42
Processing time 0.0092 sec