- 締切済み
合格最低人数を引数にして合格最低点を求めるプログラム
人数を引数にして合格最低点を求めるプログラムを作ったですが、実行結果は違っているですが、修正してください。 (プログラムに対して、作成した点数表⇒[下の付録のzeng.txt]、合格最低人数として、 20 人、40人、60人をそれぞれ与え、合格最低点) #include <iostream> #include <vector> #include <algorithm> #include <iostream> #include <iterator> #include <sstream> struct Record { std::string exnum; // 受験番号 int score; // 得点 bool operator>(const Record &rec) const { if (score != rec.score) return score > rec.score; return exnum > rec.exnum; } }; struct ExnumCmp { bool operator()(const Record &a, const Record &b) const { if (a.exnum != b.exnum) return a.exnum < b.exnum; return a.score > b.score; } }; template<typename Ch, typename Tr> std::basic_istream<Ch, Tr> & operator>>(std::basic_istream<Ch, Tr> &bis, Record &rec) { std::string e; int s; if (bis >> e && bis >> s) { rec.exnum = e; rec.score = s; } return bis; } template<typename Ch, typename Tr> std::basic_ostream<Ch, Tr> & operator<<(std::basic_ostream<Ch, Tr> &bos, const Record &rec) { return bos << rec.exnum << ' ' << rec.score; } int main(int argc, char *argv[]) { int n = 20; if (argc > 1) { std::istringstream iss(argv[1]); iss >> n; } std::cerr << "minimum number of people: " << n << '\n'; std::vector<Record> v; std::copy(std::istream_iterator<Record>(std::cin), std::istream_iterator<Record>(), std::back_inserter(v)); if (v.empty() || n <= 0) { // 受験者がいない or 合格最低人数が 0 以下(誰も受からない) std::cout << "border line: infinity\n"; } else if (n < v.size()) { // まともな範囲(受験者がいて、合格最低人数を上回る std::cout << "border line: " << v[n - 1].score << '\n'; } else { // 合格最低人数が受験者数以上である(誰でも受かる) std::cout << "border line: 0\n"; } } 付録: grade.txt C00001 48 C00002 51 C00003 40 C00004 47 C00005 43 C00006 46 C00007 59 C00008 61 C00009 61 C00010 62 C00011 54 C00012 47 C00013 55 C00014 51 C00015 53 C00016 45 C00017 43 C00018 53 C00019 43 C00020 51 C00021 44 C00022 56 C00023 59 C00024 51 C00025 44 C00026 48 C00027 50 C00028 60 C00029 53 C00030 48 C00031 57 C00032 58 C00033 44 C00034 47 C00035 51 C00036 47 C00037 51 C00038 60 C00039 62 C00040 45 C00041 28 C00042 57 C00043 53 C00044 52 C00045 58 C00046 41 C00047 45 C00048 47 C00049 52 C00050 35 C00051 62 C00052 54 C00053 43 C00054 45 C00055 41 C00056 49 C00057 55 C00058 56 C00059 42 C00060 45 C00061 54 C00062 40 C00063 49 C00064 53 C00065 49 C00066 56 C00067 52 C00068 54 C00069 51 C00070 69 C00071 46 C00072 54 C00073 40 C00074 65 C00075 46 C00076 46 C00077 42 C00078 53 C00079 52 C00080 43 C00081 44 C00082 55 C00083 62 C00084 56 C00085 38 C00086 45 C00087 37 C00088 43 C00089 57 C00090 55 C00091 53 C00092 52 C00093 57 C00094 56 C00095 63 C00096 43 C00097 59 C00098 48 C00099 37 C00100 39 実行結果: C:\work>.\a.exe 20 < zeng.txt 合格最低人数: 20 合格最低点border line: 51 C:\work>.\a.exe 40 < zeng.txt 合格最低人数: 40 合格最低点border line: 45 C:\work>.\a.exe 60 < zeng.txt 合格最低人数: 60 合格最低点border line: 45
- みんなの回答 (1)
- 専門家の回答
みんなの回答
- sakusaker7
- ベストアンサー率62% (800/1280)
疑って申し訳ないんだけど、本当に自分で作ってます? もしそうなら、プログラムの仕様からすればつかって当然であろう functor がプログラムにあるのに使ってないのはなぜなのか説明してもらいたいんですけど。 >struct ExnumCmp これね >functor
補足
自分じゃないですけど、教えていただきたいので、ここで、質問しました。よろしくお願いします。