1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
// Make sure to save as UTF-8, and on Windows, let your compiler know
// the input is UTF-8 and "chcp 65001" your console before running.
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
std::pair <std::string, std::string> numbers[] =
{
{ "一", "yī" },
{ "二", "èr" },
{ "三", "sān" },
{ "四", "sì" },
{ "五", "wǔ" },
{ "六", "liù" },
{ "七", "qī" },
{ "八", "bā" },
{ "九", "jiǔ" },
{ "十", "shí" },
};
std::string colors[] =
{
"\033[93;40m", // yellow on black
"\033[94;40m", // light blue on black
};
std::string mandarin_digits_1_99( int n )
{
std::string result;
if (n >= 20) result += (char)(n / 10);
if (n >= 10) result += (char)10;
if (n % 10) result += (char)(n % 10);
return result;
}
void print( int n )
{
std::string cjk, pinyin;
int k = 0;
for (char c : mandarin_digits_1_99( n ))
{
if (!pinyin.empty()) pinyin += " ";
cjk += numbers[ (int)c - 1 ].first;
pinyin += numbers[ (int)c - 1 ].second;
k += 1;
}
while (k++ < 3) cjk += " "; // setw(manually) : CJK are double-wide characters
std::cout
<< std::left << colors[ n % 2 ] // alternating line color
<< std::setw( 3 ) << n << " " // western digits (3 digits wide)
<< cjk << " " // Chinese digits (3 digits wide --> 6 characters wide)
<< std::setw( 10 ) << pinyin << "\n"; // Romanization (max 10 characters)
}
int main()
{
for (int n = 1; n < 100; n++) print( n );
std::cout << colors[ 0 ] << "100 一百 yì bǎi\033[0m\n";
}
|