/* * latecalc.cpp * * Usage: latecalc * * This program takes an argument indicating how many minutes late an * assignment was and returns the lateness multiplier for that assignment. * * (c) 2006 Melissa O'Neill. All rights reserved. */ #include #include #include #include #include #include /* lateMult [global function]: * Inputs: double mins minutes past the deadline * Returns: double multiplier (i.e., 0.8 => 20% penalty) * * Calculates late penalty. */ double lateMult (double mins) { const double MAX_LATE_PERIOD = 12 * 60; // Twelve hours (in minutes) double lateness = mins / MAX_LATE_PERIOD; double root = sqrt(lateness); // The formula is strange, but double squared = lateness * lateness; // gives a good penalty curve. double penalty = 1.0 - (root * (1.0 - root) + squared * root); if (penalty < 0.0) return 0.0; else return penalty; } /* stringTo [global function template]: * Inputs: string str string to convert * Returns: T whatever type you want str converted to * Throws: std::invalid_argument if conversion fails * * Uses converts a string into any type that operator >> can read. */ template T stringTo (const std::string& str) { std::stringstream stream(str); T result; stream >> result; if (stream.eof() && !stream.fail()) { return result; } else { throw std::invalid_argument("Can't figure out `" + str + "'"); } } /* main [global function & program entry point]: * Inputs: int argc command-line argument count + 1 * const char* argv command-line arguments * Returns: int exit code * * Calculates lateness multiplier. */ int main(int argc, const char* argv[]) { // Turn C-style arguments into a C++-style vector, dropping the 0th // argument (the name the program was invoked with). std::vector args(argv+1, argv+argc); if (args.size() != 1) { std::cerr << "Usage: latecalc " << std::endl; exit(1); } double multiplier = lateMult(stringTo(args[0])); std::cout << multiplier << std::endl; return 0; }