std::string numberstring(const unsigned long i) { if( i < 20 ) { return lessthantwentytostring(i); } else if ( i < 100 ) { unsigned long tens; tens = i/10; return tenstostring(tens) + lessthantwentytostring(i - tens*10); } else if ( i < 1000 ) { unsigned long hundreds; hundreds = i / 100; if ( i - hundreds*100 == 0 ) { return numberstring(hundreds) + "hundred" + numberstring(i - hundreds*100); } else { return numberstring(hundreds) + "hundredand" + numberstring(i - hundreds*100); } } else if ( i < 1000000 ) { unsigned long thousands; thousands = i / 1000; return numberstring(thousands) + "thousand" + numberstring(i-thousands*1000); } else if ( i < 1000000000 ) { long millions; millions = i / 1000000; return numberstring(millions) + "million" + numberstring(i-millions*1000000); } }
std::string decode(const std::string & to_decode) { boost::regex e("(\\d+)(\\w)") ; boost::match_results<std::string::const_iterator> matches ; std::ostringstream oss ; std::string::const_iterator start = to_decode.begin() , end = to_decode.end() ; while(boost::regex_search(start , end , matches , e)) { std::string numberstring(matches[1].first, matches[1].second) ; int number = atoi(numberstring.c_str()) ; std::string character(matches[2].first , matches[2].second) ; for(int i = 0 ; i < number ; i++) oss << character ; start = matches[ 2 ].second ; } return oss.str() ; }
int main(int argc, char* argv[]) { if (argc < 1) return 0; long i; long start = 1; long incr = 1; long end; bool newline = true; if ( argc < 2 ) { std::cout << "Usage: " << argv[0] << "[<start>] [<end> [<incr>]]" << std::endl; return 1; } else if ( argc < 3 ) { end = atoi(argv[1]); // if std::string overhead is too high // do this case "constructively" // i.e. just iteratively construct all these // numbers. } else if (argc < 4) { start = atoi(argv[1]); end = atoi(argv[2]); } else if (argc < 5) { start = atoi(argv[1]); end = atoi(argv[2]); incr = atoi(argv[3]); } else { start = atoi(argv[1]); end = atoi(argv[2]); incr = atoi(argv[3]); newline = argv[4][0] =='n'; } for (i = start; i < end; i += incr ){ std::cout << numberstring(i); if (newline) std::cout << std::endl; } return 0; }