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.0191 sec