-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2ecfc8
commit d7d6760
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <iostream> | ||
#include <string> | ||
using std::cout; | ||
|
||
std::string numberToString(int n) | ||
{ | ||
if (n >= 1000) // only works for numbers below 1000 | ||
throw std::invalid_argument("n must be betweeen 1 and 999"); | ||
|
||
std::string firsts[] = {"", "one", "two", "three", "four", "five", "six", "zeven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; | ||
std::string tens[] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; | ||
std::string value = ""; | ||
|
||
if (n > 99) // Hunderds | ||
{ | ||
value += firsts[n / 100] + "hundred"; | ||
n %= 100; | ||
if (n > 0) { value += "and"; } // if there is text after the hundered it requires "and" | ||
} | ||
if (n < 20) { return value + firsts[n]; } // special cases | ||
if (n < 100) // any number below 100 that is not special | ||
{ | ||
value += tens[n / 10]; | ||
value += firsts[n % 10]; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
int main() | ||
{ | ||
int sum = 0; | ||
for (int i=1; i<1000; i++) | ||
sum += numberToString(i).length(); | ||
|
||
sum += std::string("oneThousand").length(); | ||
cout << sum << "\n"; | ||
return 0; | ||
} |