0~999999999の任意の数値で機能します。
このプログラムは、ユーザーから数値を取得し、それを 3 つの部分に分割して、配列に別々に格納します。3 つの数値は、それらを単語に変換する関数を介して渡されます。次に、最初の部分に "million" を追加し、2 番目の部分に "thousand" を追加します。
#include <iostream>
using namespace std;
int buffer = 0, partFunc[3] = {0, 0, 0}, part[3] = {0, 0, 0}, a, b, c, d;
long input, nFake = 0;
const char ones[][20] = {"", "one", "two", "three",
"four", "five", "six", "seven",
"eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"};
const char tens[][20] = {"", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"};
void convert(int funcVar);
int main() {
cout << "Enter the number:";
cin >> input;
nFake = input;
buffer = 0;
while (nFake) {
part[buffer] = nFake % 1000;
nFake /= 1000;
buffer++;
}
if (buffer == 0) {
cout << "Zero.";
} else if (buffer == 1) {
convert(part[0]);
} else if (buffer == 2) {
convert(part[1]);
cout << " thousand,";
convert(part[0]);
} else {
convert(part[2]);
cout << " million,";
if (part[1]) {
convert(part[1]);
cout << " thousand,";
} else {
cout << "";
}
convert(part[0]);
}
system("pause");
return (0);
}
void convert(int funcVar) {
buffer = 0;
if (funcVar >= 100) {
a = funcVar / 100;
b = funcVar % 100;
if (b)
cout << " " << ones[a] << " hundred and";
else
cout << " " << ones[a] << " hundred ";
if (b < 20)
cout << " " << ones[b];
else {
c = b / 10;
cout << " " << tens[c];
d = b % 10;
cout << " " << ones[d];
}
} else {
b = funcVar;
if (b < 20)
cout << ones[b];
else {
c = b / 10;
cout << tens[c];
d = b % 10;
cout << " " << ones[d];
}
}
}