My first C # program, Chinese and integer Translation

Source: Internet
Author: User

OriginalArticleIf it is reproduced, please note: transferred from ¥ forgot % windHttp://www.cnblogs.com/slave_wc}

Address:My first C #Program, Chinese and integer Translation


Test conditions:

1: Input exception judgment:

2: simple data testing

 

CodeDescription Overview:

Basic Ideas for converting numbers to Chinese:

Decomposition

1: Calculate the most basic digital translation []

2: connect two adjacent computation results in the formula (1) using the word 'wan' to obtain the translation in the range of [0, 99999999.

3: connect with a hundred million characters to obtainLarge number range.

Basic idea of converting Chinese to numbers:

Main problems:1: Where to perform multiplication? 2: What is the scope of multiplication?

Misunderstandings:

1: only consider where to perform multiplication, such as 10 million

(It is easy to think that when the word 'wan' is calculated, the Unit closest to it is 'kil'. In this case, multiplication is performed, except multiplication, then perform sequential calculation ).

**Test data: 0.1 billion, 30 million. ByAlgorithmThe obtained expression is 1e8 + 3000*10000. Due to sequential computing, the actual computing time is (1e8 + 3000) * 10000

Incorrect answer.

2: The problem encountered in (1) is taken into account, but an error occurred in determining the scope of multiplication. For example, if the data is 0.1 billion 30 million, the correct expression is 1e8 + (3000*10000)

It is assumed that only the expressions '+' and '*' can be computed, that is, the priority of '*' and '+' are considered.

So "0.1 billion 30 million" can get the correct answer.

**Test data: 0.1 billion to 1.2 million. The following algorithm is used: 1E 8 + 100 + (20*10000 ).

The actual result should be: 1e8 + (100 + 20) * 10000, and an incorrect answer is obtained.

The solution is summarized as follows: 1: Where to perform multiplication? 2: What is the scope of multiplication?

**For example, 0.1 billion and 1.2 million. In the case of 'wan', the preceding '100' <'wan' determines the current multiplication.

**Then, the first unit encountered before 'wan'> 'wan' (million here) determines the range of multiplication between the two.

Get the correct expression for the translation: 1e8 + (100 + 20) * 10000.

When you pass by, you can see any objection. You are welcome to make a brick..

Brief code framework:

Private class num2chinese {Private Static readonly string unit = "0123456789"; Private Static readonly string Ala = ""; Private Static readonly string CH = "0 "; private Static string transsmall (string num) {/* [0, 10 thousand) translation */} Private Static string trans (string num, int range, char SS) {/* recursive solution. [0,] words can be spliced into [0, 10 million]. Similarly, [0, 10 million] words can be connected by hundreds of millions of words. Parameter description: The num string is decomposed by each range as a group and connected by SS characters. (Four or eight digits) */} public static string transbiginteger (string SS) {/* calls trans for translation and exception handling, support big number */} private class chinesenumconvert {/* integer to numeric class, not support big number key points: Where to perform multiplication? Calculate the multiplication range? */Private Static readonly string digit = ""; Private Static readonly string unit = "trillion"; Private Static readonly int [] num = {1, 10,100,100 0, 10000,100 000000}; Private Static bool checkstring (string word) {/* simple judge whether the input is valid 1; other characters. 2: There are non-zero numbers adjacent */} public static long chinesenum2int (string word ){}}

The code and necessary comments are as follows:

Private class num2chinese {Private Static readonly string unit = "0123456789"; Private Static readonly string Ala = ""; Private Static readonly string CH = "0 "; private Static string transsmall (string num) {/* [0, 10 thousand) translation */string STR = ""; string nn = num. trimstart ('0'); bool flag = false; Int J = 0; For (INT I = nn. length-1; I> = 0; I --, J ++) {int NO = Ala. indexof (NN [I]); If (No! = 0) Flag = true; If (FLAG) {If (J! = 0 & NO! = 0) STR + = Unit [J]; If (! (No = 0 & STR [Str. length-1] = '0') STR + = CH [No] ;}} char [] S = Str. tochararray (); array. reverse (s); STR = new string (s); Return STR;} Private Static string trans (string num, int range, char SS) {/* recursive solution. [0,] words can be spliced into [0, 10 million]. Similarly, [0, 10 million] words can be connected by hundreds of millions of words. Parameter description: The num string is decomposed by each range as a group and connected by SS characters. (Four or eight digits) */string ret = ""; string input = num. trimstart ('0'); For (INT I = input. length-range; I>-range; I-= range) {int ST = I, Len = range; /* st and Len indicate the start position and length of each string */if (I <0) {ST = 0; Len = range + I;} string TMP = input. substring (St, Len); long nn = long. parse (TMP); string cur = ""; if (nn = 0) {ret = SS + ret; continue;} If (Ss = 'wan ') {cur = transsmall (TMP) + SS;} else {cur = Trans (TMP, 4, 'wan') + SS;}/* After calculating the value of each group, splice SS characters to the end */If (NN % 10 = 0 & ret. length> 0 & RET [0]! = '0') {/* if there is a zero value at the end of the current group, will it be + zero */int ll = num. Length-St-len; LL = ll> 8? 8: ll; // * The maximum unit is 0.1 billion. If there is no zero in the last eight bits of the current group, no zero is output. Example:, zero, and no output, 0.1 billion million and 0.1 billion million (instead of 0 billion million and million) */string TT = num. substring (ST + Len, LL); If (TT. trim ('0 ')! = "") // The last eight bits have non-0 values cur + = '0 ';} if (Ss = '000000' & NN <1000 | Ss = '000000' & NN <(long) 1e7) {/* when the front position of the current group is vacant, the front position is Zero X/cur = '0' + cur;} ret = cur + ret;} int S = 0, _ Len = ret. length-1; if (Ret [0] = '0') {S = 1; _ Len --;} If (Ret [ret. length-2] = '0') {_ Len --;}/* remove the substring after the redundant front and back SS unit operators */ret = ret. substring (S, _ Len); return ret;} public static string transbiginteger (string SS) {/* call Trans for translation and exception handling, supporting Hold large number */string ret = ss. trimstart ('0'); string TT = "0123456789"; char [] CH = TT. tochararray (); ret = ret. trim (''); If (Ret. trim (CH )! = "") {MessageBox. Show ("the input data is invalid! "); Return" ";} If (ret =" ") {return" zero ";} ret = trans (Ret, 8, '100 '); return ret ;}} private class chinesenumconvert {/* integer to numeric type, does not support big data key points: Where to perform multiplication? Calculate the multiplication range? */Private Static readonly string digit = ""; Private Static readonly string unit = "trillion"; Private Static readonly int [] num = {1, 10,100,100 0, 10000,100 000000}; Private Static bool checkstring (string word) {/* simple judge whether the input is valid 1; other characters. 2: There are non-zero numbers adjacent */WORD = word. trim (''); string TT = digit + unit; string TMP = word. trim (TT. tochararray (); If (TMP! = "") Return false; For (INT I = 0; I <word. length-1; I ++) {If (digit. indexof (word [I])> = 1 & digit. indexof (word [I + 1]) >=1) {return false ;}} return true;} public static long chinesenum2int (string word) {If (! Checkstring (Word) {MessageBox. Show ("the input data is invalid! "); Return 0;} Long sum = 0; long TMP = 0; long [] TT = new long [150]; int dig =-1, unit0 =-1, unit1 =-1, unit2 =-1; if (unit. indexof (word [0])> = 0) {word = "1" + word;} If (unit. indexof (word [word. length-1]) <0) {If (word. length> = 2 & (TMP = unit. indexof (word [word. length-2])> 0) {word = word + unit. tochararray () [TMP-1];} else {word + = 'tags';} For (INT I = 1; I <word. length; I ++) {dig = digit. indexof (word [I-1]); unit0 = unit. indexof (word [I]); If (unit0> = 0) {unit1 =-1; unit2 =-1; int idx =-1; for (Int J = I-1; j> = 0; j --) {unit1 = unit. indexof (word [J]); If (unit1> = 0) break;} // do you need to perform multiplication for (Int J = I-1; j> = 0; J --) {unit2 = unit. indexof (word [J]); If (unit2> unit0) {idx = J; break ;}} // determine the scope of multiplication. If (unit1> = 0 & unit0> = unit1) {If (DIG> = 0) sum + = dig; if (unit2 =-1) {sum * = num [unit0];} else {sum = (Sum-TT [idx]) * num [unit0] + TT [idx] ;}} else {If (DIG <0) Dig = 1; sum + = dig * num [unit0] ;}} TT [I] = sum;} return sum ;}
}

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.