Commits

Martinho Fernandes  committed a2539c1

Refactored 591: main is simpler.

  • Participants
  • Parent commits 014f3df

Comments (0)

Files changed (2)

 syntax: regexp
 ^\.c?project$
+^\d{3,}$
 
     return is;
 }
 
-vector<problem> read_problems(istream& is) {
+struct format_output {
+	format_output(ostream& os) : os(os), i() {}
+	ostream& os;
+	int i;
+	void operator()(int moves) {
+    	cout << "Set #" << ++i << endl;
+    	cout << "The minimum number of moves is " << moves << "." << endl;
+    	cout << endl;
+	}
+};
+
+int main() {
+    istream_iterator<problem> end;
     vector<problem> problems;
-    istream_iterator<problem> in(is);
-    istream_iterator<problem> end;
-    copy(in, end, back_inserter(problems));
-    return problems;
-}
-
-int main(int argc, char **argv) {
-    vector<problem> problems(read_problems(cin));
+    copy(istream_iterator<problem>(cin), end, back_inserter(problems));
 
     vector<int> solutions;
     transform(problems.begin(), problems.end(), back_inserter(solutions), mem_fun_ref(&problem::solve));
 
-    for(size_t i = 0; i < solutions.size(); ++i) {
-    	cout << "Set #" << i+1 << endl;
-    	cout << "The minimum number of moves is " << solutions[i] << "." << endl;
-    	cout << endl;
-    }
+    for_each(solutions.begin(), solutions.end(), format_output(cout));
 
     return 0;
 }