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
|
[#include <iostream>
#include <string>
using namespace std;
string num_to_text[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string tens_to_text[] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
string power_to_text[] = {"", "hundred", "thousand", "million", "billion" };
int main() {
int number, division_by_thousand;
printf("%s \n", "Print the number:");
scanf("%d", &number);
if (number < 100 && number >= 1) { // 45
int tens = number / 10; // 4
int ones = number % 10; // 5
if (number >= 1 && number < 20) { cout << num_to_text[number] + " "; }
else { cout << tens_to_text[tens] + " " + num_to_text[ones] << endl; }
}
if (number < 1000 && number > 100) { //432
int hundreds = number / 100; //4
number %= 100; // 32
int tens = number / 10; // 3
int ones = number % 10; // 2
cout << num_to_text[hundreds] + " " + power_to_text[1] + " ";
if (number >= 1 && number < 20) { cout << num_to_text[number] + " "; }
else { cout << tens_to_text[tens] + " " + num_to_text[ones] << endl; }
}
if (number > 1000 && number < 10000) { //1256; 9831
int thousands = number / 1000; //1, 9
cout << num_to_text[thousands] + " " + power_to_text[2] + " ";
number %= 1000; // 256, 831
int hundreds = number / 100; // 2, 8
cout << num_to_text[hundreds] + " " + power_to_text[1] + " ";
number %= 100; // 56, 31
int tens = number / 10; // 5, 3
int ones = number % 10; // 6, 1
if (number >= 1 && number < 20) { cout << num_to_text[number] + " "; }
else { cout << tens_to_text[tens] + " " + num_to_text[ones] << endl; }
}
if (number >= 10000 & number < 100000){ // 99843
int thousands = number / 1000; // 99
int ten_thousands = number / 10000; // 9
int ten_ones = thousands % 10; // 9
if (thousands >= 1 && thousands < 20) { cout << num_to_text[thousands] + " " + power_to_text[2] + " "; }
else { cout << tens_to_text[ten_thousands] + " " + num_to_text[ten_ones] + " " + power_to_text[2] + " "; }
number %= 1000; // 843
int hundreds = number / 100; // 8
number %= 100; // 43
int tens = number / 10; // 4
int ones = number % 10; // 3
if (number < 1) { cout << "";}
else { cout << num_to_text[hundreds] + " " + power_to_text[1] + " "; }
if (number >= 1 && number < 20) { cout << num_to_text[number] + " "; }
else { cout << tens_to_text[tens] + " " + num_to_text[ones] << endl; }
}
}
|