The amount of the amount to Chinese capital-C ++ code

xiaoxiao2021-03-05  22

This is a printed little thing you have made, you need to use amount conversion.

Many of the amount of the amount conversion on the Internet are all the code of VB or JavaScript, which is basically used for a string replacement. It feels that it is easier to understand if it feels PUSH in the first Push.

Before conversion:

After conversion:

test program:

// MoneyConvertst.cpp: Defines The entry point for the console application.

//

#include "stdafx.h"

#include "MoneyConverter.h"

#include

Int main (int Argc, char * argv [])

{

MoneyConverter Converter;

TSTRING MONEY = _T ("123400123.19);

Cout << Converter.digitalStr2CHINESE (Money) << ENDL;

Return 0;

}

Test program output results:

Source code of MoneyConverter:

// MoneyConverter.h: Interface for the MoneyConverter Class.

//

#if! defined (AFX_MONEYCONVERTER_H__AA9A99D8_5205_4BF1_89DE_5E9718A39887__included_)

#define AFX_MONEYCONVERTER_H__AA9A99D8_5205_4BF1_89DE_5E9718A39887__included_

#iF _MSC_VER> 1000

#pragma overce

#ENDIF / / 100 m _ _ _

#include

#include

#include

Typedef st :: basic_string tstring;

Using namespace std;

Class MoneyConverter

{

PUBLIC:

MoneyConverter ();

Virtual ~ MoneyConvert ();

Tstring DigitalStr2CHINESE (TSTRING & STR);

protected:

// Is it legal numeric string

Bool IsDigitstring (Tstring & Str);

/ / Convert integer part

Tstring ConvertIntergerPart (TSTRING & STR);

// Convert the fractional part

Tstring ConvertFractionalPart (TSTRING & STR);

Private:

TCHAR * Unit1 [4];

TCHAR * Unit2 [3];

TCHAR * Unit3 [2];

TCHAR * DIGITAl [10];

}

#ndif //! defined (AFX_MONEYCONVERTER_H__AA9A99D8_5205_4BF1_89DE_5E9718A39887__inCluded_)

// MoneyConverter.cpp: Implementation of the MoneyConverter Class.

//

#include "stdafx.h"

#include "MoneyConverter.h"

// construction / destruction

MoneyConverter :: MoneyConverter ()

{

Unit1 [0] = _ t (""); unit1 [1] = _ t ("pick"); Unit1 [2] = _ t (""); unit1 [3] = _ t (""); unit2 [0 ] = _ T (""); unit2 [1] = _ t ("10,000"); Unit2 [2] = _ T ("100 million");

Unit3 [0] = _ t ("angle"); Unit3 [1] = _ t ("points");

Digital [0] = _ t ("zero"); DIGITAL [1] = _ T (""); Digital [2] = _ T (""); Digital [3] = _ t ("叁");

Digital [4] = _ T (""); Digital [5] = _ T ("Wu"); Digital [6] = _ T ("Lu"); Digital [7] = _ T ("柒");

Digital [8] = _ T ("捌"); Digital [9] = _ T ("");

}

MoneyConverter :: ~ MoneyConverter ()

{

}

TSTRING MONEYCONVERTER :: DigitalStr2CHINESE (TSTRING & STR)

{

IF (! isdigitstring (str))

Return_T ("Not Valid Digital String!");

INT POS = str.find_first_of ('.');

Tstring str1 = str.substr (0, POS);

Tstring str2;

IF (POS> 0)

Str2 = str.substr (POS 1, str.rfind ('.'));

Return ConvertIntergerPart (str1) ConvertFractionalPart (STR2);

}

TSTRING MONEYCONVERTER :: ConvertIntergerPart (Tstring & Str)

{

Std :: stack s;

Int index1, index2, pos;

Index1 = index2 = POS = 0;

S.empty ();

TSTRING:: Reverse_iterator it; // From the low position

For (it = str.rbegin (); it! = str.rend (); it ) //

{

IF (INDEX2> 0 && index1 == 0)

s.push (unit2 [index2]);

IF (* it! = '0')

s.push (unit1 [index1]);

// 个 不 写, continuous zero only one

IF (! (* it == '0' && (INDEX1 == 0 || * (IT-1) == '0')))))))))

S.push (Digital [* IT-'0 ']);

//

POS ;

INDEX1 = POS% 4; //

INDEX2 = (POS / 4)% 3; //

//

IF (POS> 8 && index2 == 0) / / greater than 10 million

INDEX2 ;

}

//

TSTRING RESULT;

While (! s.empty ()) // out of the stack {

Tstring c = S.top ();

IF (! c.empty ())

Result.Append (c);

S.POP ();

}

Result.Append (_t ("round"));

Return Result;

}

TSTRING MONEYCONVERTER :: ConvertFractionalPart (TSTRING & STR)

{

Tstring sjiao, sfen, result;

IF (str.length () == 1)

Sjiao = str.substr (0,1);

IF (str.length ()> 1)

Sfen = str.substr (1, 1);

IF (SJIAO == _ T ("0")) SJIAO = _t ("");

IF (sfen == _ t ("0")) sfen = _t ("");

IF (sjiao.empty () && sfen.empty ()) {

Result = _t ("whole");

}

Else if (! sjiao.empty () && sfen.empty ()) {

Result = TSTRING (Digital [ATOI (sjiao.c_str ())]) _ T ("angle");

}

Else IF (sjiao.empty () &&! sfen.empty ()) {

Result = TSTRING (_T ("zero")) TSTRING (Digital [ATOI (sfen.c_str ())]) _ T ("Division");

}

Else {

Result = TSTRING (DIGITAL [ATOI (Sjiao.c_str ())]) TSTRING (_t ("angle"))

Tstring (Digital [atoi (sfen.c_str ())]) _ T ("points");

}

Return Result;

}

Bool Moneyconverter :: Isdigitstring (Tstring & Str)

{

IF (str.size () == 0)

Return False;

Std :: string :: size_type index = 0;

Bool sawdot = false;

IF ((Str [0] == '-') || (STR [0] == ' ')))

INDEX ;

For (; index

{

IF (Str [Index] == '.')

{

IF (Sawdot)

Return False;

Else sawdot = true;

}

Else if (! isdigit (str [index]))

Return False;

}

Return True;

}

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

New Post(0)