Convert numeric values into word forms, numeric Word Forms

Source: Internet
Author: User

Convert numeric values into word forms, numeric Word Forms

Chapter 2 of C and pointers: 7th programming questions:

Compile Functions

Void written_amount (unsigned int amount, char * buffer );

It converts the value represented by amount to the word form and stores it in the buffer. This function can be used in a program that prints check. For example, if the amount value is 16 312, the string stored in the buffer should be

SIXTEEN THOUSAND THREE HUNDRED TWELVE

The caller ensures that the buffer space is large enough. For example, 1 200 can be one thousand two hundred or twelve hundred. You can select a form you like.

1/* 2 ** converts a non-negative integer value into a word form. 3 */4 5 # include <stdio. h> 6 # include <string. h> 7 8 void written_amount (unsigned int amount, char * buffer); 9 10 int 11 main () 12 {13 unsigned int num; 14 scanf ("% u ", & num); 15 char B [100] = {0}; 16 17 written_amount (num, B); 18 19 puts (B); 20 21 return 0; 22} 23 24/* 25 ** function: Convert the value of amount into a word form. The result is stored in buffer 26 */27 void 28 written_amount (unsigned int amount, char * buffer) 29 {30/* 31 ** number corresponding words, divided into four groups of 32 */33 char word_00009 [10] [6] = {"ZERO ", "ONE", "TWO", "THREE", "FOUR", "FIVE", 34 "SIX", "SEVEN", "EIGHT", "NINE "}; 35 char word_10_19 [10] [10] = {"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", 36 "SIXTEEN ", "SEVENTEEN", "EIGHTEEN", "NINETEEN"}; 37 char word_ty [10] [8] = {"", "", "TWENTY", "THIRTY ", "FORTY", "ty", 38 "SIXTY", "SEVENTY", "EIGHTY", "NINETY "}; 39 char word_div [4] [9] = {"HUNDRED", "THOUSAND", "MILLION", "BILLION"}; 40 41 static int count = 0; // record the number of sub-bits 42 unsigned int tmp = amount/1000; 43 44/* 45 ** use a recursive function until the value <1000 46 */47 if (tmp) 48 {49 count ++; // The number of kilobytes increases by 50 written_amount (tmp, buffer); // recursion, processing the next kilobytes of 51} 52 53 int val = amount % 1000; // convert words in three places, three places, and three places. The value of 54, 55, and * 56 ** is greater than 100, first, store the words in hundreds of BITs in buffer to 57 ** followed by HUNDRED 58 ** to change the value of % 100 to two digits, continue to process 59 */60 if (val> = 100) 61 {62 strcat (buffer, word_00009 [val/100]); 63 strcat (buffer ,""); 64 strcat (buffer, word_div [0]); 65 val % = 100; 66} 67 68/* 69 ** values between 20 and 99, add dozens of corresponding words to the buffer 70 ** and then % 10, and then process the number of digits 71 */72 if (val >=20 & val <= 99) 73 {74 strcat (buffer, ""); 75 strcat (buffer, word_ty [val/10]); 76 val % = 10; 77} 78 79/* 80 ** in the range of 10 to 19, add the corresponding word to the buffer 81 */82 if (val >=10 & val <= 19) 83 {84 strcat (buffer, ""); 85 strcat (buffer, word_10_19 [val % 10]); 86} 87 88/* 89 ** values between 0 and 9, add the corresponding word to the buffer 90 */91 if (val> = 0 & val <= 9) 92 {93 strcat (buffer, ""); 94 strcat (buffer, word_00009 [val]); 95} 96 97/* 98 ** after processing a thousand bits, add the corresponding sub-point words "THOUSAND", "MILLION", etc. 99 */100 if (count> 0) 101 {102 strcat (buffer ,""); 103 strcat (buffer, word_div [count --]); 104 strcat (buffer, ""); 105} 106}

 

Related Article

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.