asp.net conversion between literal and integer characters in C #

Source: Internet
Author: User

ASP tutorials conversion between characters and integers in. NET C #
Digital to Chinese basic ideas:

is to decompose

1: Computation of the most basic [0,9999] digital Translations

2: Through the ' million ' word to connect two adjacent to the (1) calculated results, get [0,99999999] scope of translation.

3: Through the ' billion ' word connection, get a large number of translations within the range.

The basic idea of Chinese digital conversion:

Main question: 1: Where do I multiply? 2: What is the scope of multiplication?

Easy to enter the misunderstanding:

1: Only consider where to multiply, such as 10 million

(It is easy to think that, when the word "million" is calculated, the nearest unit is ' thousand ', in which case the multiplication is performed, and in addition to the multiplication, the order is computed).

* * Test data: 130 million. The expression of the algorithm is: 1e8 + 3000 * 10000. Since it is a sequential calculation, the actual calculation is (1E8 + 3000) * 10000

Got the wrong answer.

2: Taking into account the problems encountered in (1), but the multiplication of the scope of judgment error. such as data 130 million, the correct expression is 1e8 + (3000 * 10000)

It is assumed that the ' * ' and ' + ' priority is taken into account in the calculation of expressions that are similar to only ' + ' and ' * '.

such as "130 million" can get the correct answer.

* * Test data: 101.2 million. The algorithm by (2) is: 1e 8 + 100 + (20 * 10000).

and the actual result should be: 1e8 + (100 + 20) * 10000, got the wrong answer.

summed up the solution: that is to consider 1: where to multiply? 2: What is the scope of multiplication?

* * such as 101.2 million. When you encounter ' million ', the previous ' hundred ' < ' million ' determines the current need for multiplication.

* * Again by ' million ' before the first > ' Million ' units (here for billion), determines the multiplication of the scope of the two between.

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

Private Class Num2chinese
{
private static readonly string unit = "Chichong";
private static readonly string ala = "0123456789";
private static readonly String ch = "0123456789";

private static string Transsmall (String num)


{/*[0, 10,000) translation of the interval * *


String str = "";


String nn = Num.trimstart (' 0 ');


BOOL flag = FALSE;


int j = 0;


for (int i = nn.length-1 i &gt;= 0; I--, j + +)


{


int no = Ala.indexof (Nn[i]);


if (no!= 0) flag = true;


if (flag)


{


if (J!= 0 &amp;&amp; no!= 0)


str = unit[j];


if (!) ( No = 0 &amp;&amp; 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, Thousand] through the word "million" can be spliced into [0, tens of millions], the same as [0, tens of millions] through the billion word connection.
Parameter meaning:
The NUM string is decomposed as a group per range and is connected by the SS character. (4-digit group or eight-digit group) * *
string ret = "";
string input = Num.trimstart (' 0 ');

for (int i = Input.length-range i &gt;-range i-= range)


{


int st = i, Len = range;/*st and Len respectively indicate the starting position and length of each set of strings * *


if (I &lt; 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 = ' million ')


{


cur = transsmall (TMP) + SS;


}


Else


{


cur = trans (tmp, 4, ' million ') + SS;


}/* calculate the value of each group, stitching SS characters to the end.


if (nn% = = 0 &amp;&amp; ret.length &gt; 0 &amp;&amp; ret[0]!= ' 0 ')


{/* The current group at the end of zero to determine whether + 0 * *


int LL = Num.length-st-len;


LL = ll &gt; 8? 8:ll;/


/* The largest unit is billion, then the current group after 8-bit range without zero output 0


Example: 100,000,005, 0 requires output, 100,000,005 (not 100,000,005) * *


String tt = num.substring (st + Len, LL);


if (Tt.trim (' 0 ')!= "")//after eight digits there is a non-0 value


Cur + = ' 0 ';


}


if (ss = ' million ' &amp;&amp; nn &lt; 1000 | |


ss = ' billion ' &amp;&amp; nn &lt; (long) 1e7)


{/* Before the current group position is vacant, the front position is 0 * * *


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--;


}/* minus the extra front and back bits of the SS-unit character after the substring * *


ret = ret.substring (s, _len);


return ret;


}

public static string Transbiginteger (String ss)


{/* call trans for translation and exception handling, support large number * *


string ret = Ss.trimstart (' 0 ');


String tt = "0123456789";


char[] ch = tt.tochararray ();


ret = Ret.trim (');


if (Ret.trim (CH)!= "")


{


MessageBox.Show ("Enter data illegal!") ");


Return "";


}


if (ret = "")


{


return "0";


}


ret = trans (ret, 8, ' billion ');


return ret;


}


}

Private Class Chinesenumconvert
{/* integer to numeric class, does not support large number
Key point: Where do you multiply? Calculate the range of multiplication? */
private static readonly string digit = "0123456789";
private static readonly string unit = "Chivan";
private static readonly int[] num = {1, 10, 100, 1000, 10000, 100000000};

private static bool Checkstring (string word)


{/* Simple to determine whether the input is legal 1; other characters. 2: There are not 0 digits adjacent to the * *


Word = Word.trim (');


String tt = digit + unit;


string tmp = Word.trim (Tt.tochararray ());


if (tmp!= "")


return false;


for (int i = 0; i &lt; word.length-1; i++)


{


if (Digit.indexof (Word[i]) &gt;= 1


&amp;&amp; Digit.indexof (word[i + 1]) &gt;= 1)


{


return false;


}


}


return true;


}

         public static long Chinesenum2int (string word)
          {
              if (!checkstring (word))
             {
                 MessageBox.Show ("Enter data illegal!") ");
                 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]) &gt;= 0)


{


Word = "one" + word;


}


if (Unit.indexof (word[word.length-1]) &lt; 0)


{


if (Word.length &gt;= 2 &amp;&amp; (tmp = Unit.indexof (word[word.length-2))) &gt; 0)


{


Word = Word + unit.tochararray () [tmp-1];


}


Else


{


Word = = ' a ';


}


}


for (int i = 1; i &lt; word.length; i++)


{


Dig = Digit.indexof (word[i-1]);


Unit0 = Unit.indexof (Word[i]);

if (unit0 &gt;= 0)


{


Unit1 =-1; Unit2 =-1;


int idx =-1;


for (int j = i-1 J &gt;= 0; j--)


{


Unit1 = Unit.indexof (Word[j]);


if (unit1 &gt;= 0) break;


}//Whether you need to do multiplication


for (int j = i-1 J &gt;= 0; j--)


{


Unit2 = Unit.indexof (Word[j]);


if (Unit2 &gt; Unit0)


{


idx = j;


Break


}


}//determines the scope of multiplication


if (unit1 &gt;= 0 &amp;&amp; unit0 &gt;= unit1)


{


if (Dig &gt;= 0) sum + = dig;


if (unit2 = = 1)


{


Sum *= num[unit0];


}


Else


{


sum = (Sum-tt[idx]) * Num[unit0] + TT[IDX];


}


}


Else


{


if (Dig &lt; 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.