نسخه جدید تابع برای کامپایلرهای ++ANSI C. ( با استفاده از کتابخانه STL ):
/************************************************** **********************
* *
* $Author: Hadi Rezaee m_hadi_rezaee@hotmail.com *
* $Company: Galaxy Road http://www.GalaxyRoad.com *
* $Description: Number to string ! ( STL ver ) *
* $Date: 3/8/2004 *
* $Version: 1.0 *
* $NOTICE: You're FREE to use or change the code in your *
* programs, This code has full compatibility with *
* ANSI C++ compilers. *
* It has been tested with VC++ 7.1 successfully. *
* *
************************************************** **********************/
#include <string>
using namespace std;
const char ch1[9][10] = { "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه", };
const char ch2[10][10] = { "ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده", };
const char ch3[8][10] = { "بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود", };
const char ch4[9][10] = { "صد", "دویست", "سیصد", "چهارصد", "پانصد", "ششصد", "هفتصد", "هشتصد", "نهصد", };
const char ch5[3][10] = { "هزار", "میلیون", "میلیارد", };
void AppendAnd( string &strSource, bool bAnd )
{
if( bAnd )
strSource += " و ";
}
string NumToStr( int num )
{
if( num == 0 )
return "صفر";
char szNumTemp[12];
string strNumTemp, strResult;
itoa( num, szNumTemp, 10 );
strNumTemp = szNumTemp;
while( strNumTemp.length() % 3 != 0 )
strNumTemp.insert( 0, "0" );
int nCount = (int) strNumTemp.length() / 3;
int i = 0;
bool bAndFlag = false;
byte v_l, v_m, v_r;
for( int n = nCount; n > 0; n-- )
{
v_l = strNumTemp[ i ] - 48;
v_m = strNumTemp[ i + 1 ] - 48;
v_r = strNumTemp[ i + 2 ] - 48;
if( v_l > 0 )
{
AppendAnd( strResult, bAndFlag );
strResult += ch4[ v_l - 1 ];
bAndFlag = true;
}
if( v_m == 1 && v_r >= 0 )
{
AppendAnd( strResult, bAndFlag );
strResult += ch2[ v_r ];
bAndFlag = true;
}
if( v_m > 1 )
{
AppendAnd( strResult, bAndFlag );
strResult += ch3[ v_m - 2 ];
bAndFlag = true;
}
if( v_m != 1 && v_r > 0 )
{
AppendAnd( strResult, bAndFlag );
strResult += ch1[ v_r - 1 ];
bAndFlag = true;
}
if( n - 1 > 0 )
{
bAndFlag = false;
strResult += " ";
strResult += ch5[ n - 2 ];
AppendAnd( strResult, bAndFlag );
bAndFlag = true;
}
i += 3;
}
return strResult;
}
موفق باشید !