Realization of lowercase transfers on C ++

zhaozj2021-02-08  263

When lowercase, we should pay attention to the process of human beings in the process of reading. For example, read "12345.67", the capital readings are: "万 仟 叁 肆 肆 伍 伍", in the actual During the reading process, people must know that there are 4 after 1, that is, there are 3 times behind, that is, 仟, according to the secondary, of course, because people have become used to the next one is, so no longer 2 There are several behind.

Based on the above recognition process, we should take the number of inverse identification to achieve, that is, when reading the integer part, "Wu Xi picks up 佰 壹 壹 壹 万".

Also set to zero and segment settings,

1. Zero mainly exists between two numbers, but except the "pick" bit, if you pick up Hill, because you will not read it in uppercase, you have to deal with it here.

2. The so-called segment, the habits of Chinese are different from the habits of the West, the number of the West is thousands, and our habits are tens of thousands, that is, this paragraph and higher, there is no non-zero number, then this section name You don't have to read, such as Jero Zenwu, there is no reading.

The following is the implementation of code and comments

CSTRING GETBIGMONEY (DOUBLE DMONEY)

{

// There is no exception to the exception, the user should pay attention (there will be no such huge amount in reality)

Cstring strmoney;

Strmoney.Format ("% .2f", dmoney);

CSTRING STRUNIT = "Yuan Qiu 佰 万 万 佰";

CString Strnumber = "Zero Zhai Zi Zhiwu Lu Yu";

Cstring strotherunit = "Overall";

/ / Treatment of numbers with fractional parts and decimal part

Int npos = strmoney.find (".");

INT NLENGTH = STRMONEY.GETLENGTH ();

IF (NPOS <0)

Npos = NLENGTH;

CString StrreturnValue;

INT ncount = 0;

Bool Bzero = False;

Bool BNEEDLEVEL = false; // The identification of the segment is used to have a paragraph name, such as billions, thousands, etc.

// Reverse identification processing of the integer part

For (int i = npos - 1; I> = 0; i -)

{

TCHAR CH = STRMONEY.GETAT (I);

IF (ncount% 4 == 0 && ncount> 0)

{

/ / If the number of processes is the fourth place (10,000), or the eighth (billion), etc.

Bneedlevel = true;

}

IF (CH == '0')

{

// Only identify the 0 of the picked 0, mainly considering the specialty of picking up, that is, if 10 readings, do not read zero

IF (ncount% 4! = 0)

Bzero = True;

}

Else

{

CString Strtemp (StrreturnValue);

StrreturnValue = STRNUMBER.MID ((CH - 0X30) * 2, 2); if (ncount> 0)

{

StrreturnValue = Strunit.mid (Ncount * 2, 2);

IF (ncount% 4! = 0 && bneedlevel)

{

// This judgment is required to read the name, such as 10 million, etc.

StrreturnValue = Strunit.mid (int (ncount / 4) * 8, 2);

}

Bneedlevel = false;

}

IF (Bzero)

{

// Only have a number in low than the current processing,

IF (! strtemp.isempty ())

StrreturnValue = Strnumber.Left (2);

Bzero = FALSE;

}

StrreturnValue = strTemp;

}

NCOUNT ;

}

strreturnValue = Strunit.Left (2);

Bool ballzero = true;

// The following is achieved after the decimal point

// Judge whether it is all correct, no need to continue reading

IF (NPOS

{

IF (NLENGTH> 2)

NLENGTH = 2;

For (int i = 0; i

IF (Strmoney.Getat (NPOS i 1)! = '0')

Ballzero = false;

}

IF (Ballzero)

{

StrreturnValue = StrotherUnit.Left (2);

}

Else

{

/ / Treatment of distal angle

For (int i = 0; i

{

TCHAR CH = STRMONEY.GETAT (NPOS 1 i);

IF (CH == '0' && I> 0)

{

}

Else

{

StrreturnValue = Strnumber.mid ((CH - 0X30) * 2, 2);

IF (ch! = '0')

StrreturnValue = StrotherUnit.mid ((i 1) * 2, 2);

}

}

}

Return StrreturnValue;

}

Comments: Small write to large write amount, according to understanding, we can divide a number into two hierarchical processes, namely, interpretation and segment processing, the segment processing is for less than 10,000 transitions, during segment processing The processing is processed on the basis of internal processing, apparently recursive is a feasible algorithm (see the profit and disadvantage of recursive in C applications), which may be simple.

This algorithm is that the author wrote by the like, only the reference value, does not directly apply the practical value, readers can modify this.

转载请注明原文地址:https://www.9cbs.com/read-1028.html

New Post(0)