#include #include #include #include // convert a string that represents the round to an integer (if round was valid) // returns false if the round is marked with a "-" (not played) bool parse_round(std::string rd, int &round) { if (rd == "-") { round = -1; return false; } else { round = atoi(rd.c_str()); assert (round >= 0); return true; } } // convert a string that represents par to an integer (if tournament was completed) // returns false if the tournament was not completed bool parse_par(std::string par, int &_par) { if (par == "E") { _par = 0; return true; } else if (par[0] == '+') { _par = atoi(par.substr(1).c_str()); return true; } else if (par[0] == '-') { _par = -atoi(par.substr(1).c_str()); return true; } else { assert (par == "CUT" || par == "WD" || par == "DQ"); // the tournament is not completed if the player was cut, withdrawn, or disqualified return false; } } // convert a string that represents the money into an integer // remove the extra punctuation ($ and ,) from the input string int parse_money(std::string money) { assert (money.size() > 1); assert (money[0] == '$'); // copy all the non punctuation characters to a new string std::string money2; for (unsigned int i = 1; i < money.size(); i++) { if (money[i] != ',') money2.push_back(money[i]); } assert (money2.size() > 0); // convert the result to an integer return atoi(money2.c_str()); } // helper routine to convert 12345 (integer) to "$12,345" (string) std::string print_money(int money) { // print the integer to a string tmp std::string tmp; std::stringstream s; s << money; s >> tmp; // prepare the answer string std::string answer = "$"; int digits = tmp.size(); // separate thousands with a ',' for (int i = 0; i < digits; i++) { int j = digits - i; if (i > 0 && j%3 == 0) answer.push_back(','); answer.push_back(tmp[i]); } return answer; } // helper routine to print par nicely std::string print_par(int par) { std::string tmp; std::stringstream s; s << par; s >> tmp; if (par == 0) return "E"; if (par > 0) return "+" + tmp; return tmp; }