Skip to content

Instantly share code, notes, and snippets.

@Andersama
Created January 27, 2022 22:55
Show Gist options
  • Select an option

  • Save Andersama/4960a35682a3a99829207515692f78a8 to your computer and use it in GitHub Desktop.

Select an option

Save Andersama/4960a35682a3a99829207515692f78a8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <string_view>
struct number_to_text {
size_t i;
std::string_view str;
};
constexpr size_t hundred_idx = 28;
std::array<number_to_text, 32> value_to_text = {
number_to_text{0ULL, "zero"},
number_to_text{1ULL, "one"},
number_to_text{2ULL, "two"},
number_to_text{3ULL, "three"},
number_to_text{4ULL, "four"},
number_to_text{5ULL, "five"},
number_to_text{6ULL, "six"},
number_to_text{7ULL, "seven"},
number_to_text{8ULL, "eight"},
number_to_text{9ULL, "nine"},
number_to_text{10ULL, "ten"},
number_to_text{11ULL, "eleven"},
number_to_text{12ULL, "twelve"},
number_to_text{13ULL, "thirteen"},
number_to_text{14ULL, "fourteen"},
number_to_text{15ULL, "fifteen"},
number_to_text{16ULL, "sixteen"},
number_to_text{17ULL, "seventeen"},
number_to_text{18ULL, "eighteen"},
number_to_text{19ULL, "nineteen"},
number_to_text{20ULL, "twenty"},
number_to_text{30ULL, "thirty"},
number_to_text{40ULL, "forty"},
number_to_text{50ULL, "fifty"},
number_to_text{60ULL, "sixty"},
number_to_text{70ULL, "seventy"},
number_to_text{80ULL, "eighty"},
number_to_text{90ULL, "ninety"},
number_to_text{100ULL, "hundred"}, //hundred_idx == 28
number_to_text{1000ULL, "thousand"},
number_to_text{1000000ULL, "million"},
number_to_text{1000000000ULL, "billion"}
};
void to_text(std::vector<number_to_text> &txt, size_t value) {
if (value == 0) {
txt.emplace_back(value_to_text[0]);
return;
}
size_t idx = value_to_text.size()-1;
for (; value && idx < value_to_text.size();) {
size_t nd = value / value_to_text[idx].i;
size_t nr = value % value_to_text[idx].i;
value = nr;
if (nd) {
if (idx >= hundred_idx) {
to_text(txt, nd);
}
txt.emplace_back(value_to_text[idx]);
}
idx--;
}
}
size_t print_text(std::vector<number_to_text>& txt) {
size_t letter_count = 0;
for (size_t i = 0; i < txt.size(); i++) {
std::cout << txt[i].str;
if (txt[i].i >= 100 && (i + 1) < txt.size() && (i + 3) >= txt.size() &&
txt[i + 1].i < txt[i].i) {
letter_count += 3;
std::cout << " and ";
} else if (txt[i].i < 100 && txt[i].i >= 20 && (i+1) < txt.size() && txt[i+1].i < txt[i].i) { //numbers between 0-100 eg: twenty-three etc...
std::cout << '-';
} else { //everything else append a space
std::cout << ' ';
}
letter_count += txt[i].str.size();
}
if (txt.size())
std::cout << '\n';
return letter_count;
}
int main()
{
std::vector<number_to_text> data;
data.reserve(32);
#if 0
to_text(data, 100000);
print_text(data);
data.clear();
to_text(data, 100000-1);
print_text(data);
data.clear();
to_text(data, 100000 + 1);
print_text(data);
data.clear();
to_text(data, 1'000'000);
print_text(data);
data.clear();
to_text(data, 1'000'000-1);
print_text(data);
data.clear();
to_text(data, 1'000'000 + 1);
print_text(data);
data.clear();
#endif
for (size_t i = 1; i <= 128'000; i++) {
to_text(data, i);
print_text(data);
data.clear();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment