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