U E D R , A S I H C RSS

CPP Study_2005_1/STL성적처리_2



1. Info

{{| : 2005 08 01
: 0회

map

sort|}}

2. Input Text

~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 
한 86 65 62 68 

3. Code

~cpp 
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

vector<string> tokenize(const string& line);
bool save_map(vector<string>&, map< string, vector<int> >&);
double total(const vector<int>&);
bool print_report(ostream&, 
		const map< string, vector<int> >, 
		double accu(const vector<int>&) = total);

int main(int argc, char *argv[]) {
	string line;
	vector<string> token;
	map< string, vector<int> > grades;
	
	ifstream fin("input.txt");
	while(getline(fin, line)) {
 		token = tokenize(line);
 		save_map(token, grades);
	}
	
	print_report(cout, grades);
	
    system("PAUSE");
    return EXIT_SUCCESS;
}

vector<string> tokenize(const string& line) {
	string word;
	vector<string> ret;
	int beg=0;
	int quantity=0;
	
	for(int i=0; i != line.size(); i++) {
		if (line[i] == ' ') {
			ret.push_back(line.substr(beg, quantity));
			beg=i+1;
			quantity=0;
		} else ++quantity;
	}
	return ret;
}

bool save_map(vector<string>& input, map< string, vector<int> >& grades) {
	for (int i = 1; i != input.size(); i++) {
		grades[input[0]].push_back(atoi(input[i].c_str()));
	}
	return true;
}

double total(const vector<int>& grades) {
	return accumulate(grades.begin(), grades.end(), 0.0);
}

bool print_report(ostream& os, 
	const map< string, vector<int> > record, 
	double accu(const vector<int>&)) {
	
	for(map< string, vector<int> >::const_iterator iter = record.begin();
		iter != record.end();
		iter++) {
		os<<iter->first<<'\t';
		for(vector<int>::const_iterator grades = (iter->second).begin();
			grades != (iter->second).end();
			++grades)
			cout<<*grades<<'\t';
		
		cout<<accu(iter->second)<<'\t';
		cout<<accu(iter->second)/(iter->second.size())<<'\t';
		cout<<endl;
	}
	return true;
}

4. Result


result.jpg

5. Idle Talk

.. -_-;; map .. -_-;; spec sort. ;; .
;;
  • sorting map . ㅡ,.ㅡ; .
  • DS DS .

class
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:42
Processing time 0.0121 sec