ソースコード
公式ページからコピーしたデータを表計算ソフトとエディターを使って、1行1人のタブ区切りデータのテキストファイルを作り、下のプログラムで国別データを出力した。
公式ページからデータをコピーしたのは、9/10 だった。
// medals.cpp : コンソール アプリケーションのエントリ ポイントを定義します。 // #include "stdafx.h" struct Medalist { TCHAR country[50]; TCHAR athlete[50]; TCHAR sport[50]; int gold; int silver; int bronze; int total; }; int _tmain(int argc, _TCHAR* argv[]) { if (argc < 2) { return -1; } Medalist medalists[2000] = {}; int medalistCount = 0; FILE* f = NULL; if (::_tfopen_s(&f, argv[1], _T("r, ccs=UNICODE")) != 0) { return -1; } for (;;) { TCHAR line[256]; if (::_fgetts(line, 256, f) == NULL) { break; } Medalist& medalist = medalists[medalistCount]; TCHAR* p = line; TCHAR* tab = ::_tcschr(p, _T('\t')); *tab = _T('\0'); ::_tcscpy_s(medalist.country, 50, p); p = tab + 1; tab = ::_tcschr(p, _T('\t')); *tab = _T('\0'); ::_tcscpy_s(medalist.athlete, 50, p); p = tab + 1; tab = ::_tcschr(p, _T('\t')); *tab = _T('\0'); ::_tcscpy_s(medalist.sport, 50, p); p = tab + 1; medalist.gold = ::_tcstol(p, &p, 10); medalist.silver = ::_tcstol(p, &p, 10); medalist.bronze = ::_tcstol(p, &p, 10); medalist.total = ::_tcstol(p, &p, 10); if (medalist.total == 0) { continue; } ++medalistCount; } ::fclose(f); Medalist countries[500] = {}; int countryCount = 0; for (int i = 0; i < medalistCount; ++i) { Medalist& medalist = medalists[i]; for (int j = 0; j < 500; ++j) { Medalist& country = countries[j]; if (country.country[0] == _T('\0')) { country = medalist; ++countryCount; break; } else if (::_tcscmp(country.country, medalist.country) == 0) { country.gold += medalist.gold; country.silver += medalist.silver; country.bronze += medalist.bronze; country.total += medalist.total; break; } } } if (::_tfopen_s(&f, argv[2], _T("w, ccs=UNICODE")) != 0) { return -1; } ::_ftprintf_s(f, _T("Country\tGold\tSilver\tBronze\tTotal\n")); for (int i = 0; i < countryCount; ++i) { Medalist& country = countries[i]; ::_ftprintf_s(f, _T("%s\t%i\t%i\t%i\t%i\n"), country.country + ::_tcslen(country.country) - 3, country.gold, country.silver, country.bronze, country.total); } ::fclose(f); return 0; }
とりあえず出力できればいいので、エラー判定はほとんどしてない。