Digital translator and Its Implementation

Source: Internet
Author: User

[Problem description]
Enter a positive integer n (n is a 4-digit integer at the maximum) and output its English expression.
[Example]
Input: 1
Output: One
Input: 12
Output: twelve
Right input: 135
Output: one hundred thirty five

Idea: 1. First, a number less than 19 can be output directly ..

2. 20 ~~~ Numbers smaller than 99. Integers of up to 10 can be directly output. Otherwise, divide by 10 to output ten digits, and return one digit from 10 modulo output.

3. 100 ~~~ Divide the number within 999 and divide it by 100 to output a hundred-digit number. Then, use the modulo value of 100 to get a two-digit number and convert it to 2.

4. 1000 ~~~ Divide the number within 9999 and divide it by 1000 to output a thousand-digit number. After modulo with 1000, a three-digit number is obtained, which is converted to 3.

The complete implementation code is as follows:

# Include "iostream" using namespace STD; char table [20] [20] = {"zero", "one", "two", "three", "four ", "five", "Six", "Seven", "eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen ", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "ninteen"}; char tens [12] [20] = {"", "Ten ", "Twenty", "Thirty", "Forty", "ty", "Sixty", "Seventy", "Eighty", "ninty", "Hundred ", "Thousand"}; void print (int n) {If (n> = 0 & n <= 19) {cout <Table [N]; // directly output numbers less than 19} else if (n> = 20 & n <= 99 & N % 10 = 0) // The entire 10 {cout <tens [N/10];} else if (n> = 20 & n <= 99) // output ten first, output a single location {cout <tens [N/10] <"" <Table [n % 10];} else if (n> = 100 & n <= 999) {print (N/100); cout <"<tens [10] <""; // output hundred-bit print (N % 100); // recursive call, output ten digits and one digit} else if (n> = 1000 & n <= 999999) {print (N/1000); cout <"" <tens [11] <""; // outputs a thousand-bit print (N % 1000); // recursive call, output bits, 10 bits, and 1 bits} int main (void) {int N; while (CIN >>> N) {print (n); cout <Endl ;} system ("pause"); Return 0 ;}

Run the following command:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.