View Full Version : ترفند: یکی از روشهای تبدیل اعداد انگلیسی به فارسی در C++ 11 به همراه کتابخانه boost
علی بهمنی جلالی
دوشنبه 08 شهریور 1395, 13:01 عصر
سلام
یکی از روشهای تبدیل اعداد انگلیسی به فارسی در C++ 11 به همراه کتابخانه boost، به صورت زیر است:
#include <iostream>
#include <boost/algorithm/string/replace.hpp>
#include <string>
using namespace std;
string enToPerNum( string );
int main()
{
long num = 1234567890;
string numStr1 = to_string( num );
string numStr2 = enToPerNum( numStr1 );
cout << numStr2 << endl;
}
string enToPerNum( string str )
{
boost::replace_all( str, "0", "۰" );
boost::replace_all( str, "1", "۱" );
boost::replace_all( str, "2", "۲" );
boost::replace_all( str, "3", "۳" );
boost::replace_all( str, "4", "۴" );
boost::replace_all( str, "5", "۵" );
boost::replace_all( str, "6", "۶" );
boost::replace_all( str, "7", "۷" );
boost::replace_all( str, "8", "۸" );
boost::replace_all( str, "9", "۹" );
return str;
}
خروجی:
۱۲۳۴۵۶۷۸۹۰
یکی از پیوندهایی که به من کمک کرد، پیوند زیر بود:
http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c
علی بهمنی جلالی
دوشنبه 08 شهریور 1395, 16:27 عصر
اگر میخواهید هم عدد فارسی را نشون بده و هم عدد انگلیسی رو، میتوانید از روش زیر استفاده کنید:
نکته: اگر اشتباه نکنم در یونیکد اعداد زیر، به صورت زیر نامگذاری شدهاند:
arabic numbers: 0123456789
hindu-arabic numbers: ٠١٢٣٤٥٦٧٨٩
extended hindu-arabic numbers: ۰۱۲۳۴۵۶۷۸۹
در برنامهٔ زیر، ورودی میتواند اعداد فارسی یا انگلیسی یا مخلوطی از هر دو باشد:
#include <iostream>
#include <boost/algorithm/string/replace.hpp>
#include <string>
using namespace std;
string enToPerNum( string );
string perToEnNum( string );
int main()
{
long tmpNum;
string inputNumStr;
string perNumStr;
string enNumStr;
cout << "عدد خود را به صورت فارسی و یا انگلیسی وارد کنید: ";
cin >> inputNumStr;
inputNumStr = perToEnNum( inputNumStr ); // convert "extended hindu-arabic number" string to "arabic number" string
tmpNum = stol( inputNumStr ); // convert "arabic number" string to "arabic number" long integer
perNumStr = enToPerNum( inputNumStr ); // convert "arabic number" string to "extended hindu-arabic number" string
enNumStr = to_string( tmpNum ); // OR: enNumStr = perToEnNum( inputNumStr );
cout << endl;
cout << "عدد شما به فارسی: " << perNumStr << endl; // print "extended hindu-arabic number"
cout << endl;
cout << "عدد شما به انگلیسی: " << enNumStr << endl; // print "arabic number"
}
string enToPerNum( string str )
{
boost::replace_all( str, "0", "۰" );
boost::replace_all( str, "1", "۱" );
boost::replace_all( str, "2", "۲" );
boost::replace_all( str, "3", "۳" );
boost::replace_all( str, "4", "۴" );
boost::replace_all( str, "5", "۵" );
boost::replace_all( str, "6", "۶" );
boost::replace_all( str, "7", "۷" );
boost::replace_all( str, "8", "۸" );
boost::replace_all( str, "9", "۹" );
return str;
}
string perToEnNum( string str )
{
boost::replace_all( str, "۰", "0" );
boost::replace_all( str, "۱", "1" );
boost::replace_all( str, "۲", "2" );
boost::replace_all( str, "۳", "3" );
boost::replace_all( str, "۴", "4" );
boost::replace_all( str, "۵", "5" );
boost::replace_all( str, "۶", "6" );
boost::replace_all( str, "۷", "7" );
boost::replace_all( str, "۸", "8" );
boost::replace_all( str, "۹", "9" );
return str;
}
در تصویر زیر، ورودی و خروجی نشان داده شده است:
142249
علی بهمنی جلالی
دوشنبه 08 شهریور 1395, 17:25 عصر
اگر خواستید میتونید بجای استفاده از توابع معمولی از عبارات لامبدا (lambda) استفاده کنید:
#include <iostream>
#include <boost/algorithm/string/replace.hpp>
#include <string>
using namespace std;
int main()
{
long tmpNum;
string inputNumStr;
string perNumStr;
string enNumStr;
auto enToPerNum = []( string str ) -> string
{
boost::replace_all( str, "0", "۰" );
boost::replace_all( str, "1", "۱" );
boost::replace_all( str, "2", "۲" );
boost::replace_all( str, "3", "۳" );
boost::replace_all( str, "4", "۴" );
boost::replace_all( str, "5", "۵" );
boost::replace_all( str, "6", "۶" );
boost::replace_all( str, "7", "۷" );
boost::replace_all( str, "8", "۸" );
boost::replace_all( str, "9", "۹" );
return str;
};
auto perToEnNum = []( string str ) -> string
{
boost::replace_all( str, "۰" ,"0" );
boost::replace_all( str, "۱" ,"1" );
boost::replace_all( str, "۲" ,"2" );
boost::replace_all( str, "۳" ,"3" );
boost::replace_all( str, "۴" ,"4" );
boost::replace_all( str, "۵" ,"5" );
boost::replace_all( str, "۶" ,"6" );
boost::replace_all( str, "۷" ,"7" );
boost::replace_all( str, "۸" ,"8" );
boost::replace_all( str, "۹" ,"9" );
return str;
};
cout << "عدد خود را به صورت فارسی و یا انگلیسی وارد کنید: ";
cin >> inputNumStr;
inputNumStr = perToEnNum( inputNumStr ); // convert "extended hindu-arabic number" string to "arabic number" string
tmpNum = stol( inputNumStr ); // convert "arabic number" string to "arabic number" long integer
perNumStr = enToPerNum( inputNumStr ); // convert "arabic number" string to "extended hindu-arabic number" string
enNumStr = to_string( tmpNum ); // OR: enNumStr = perToEnNum( inputNumStr );
cout << endl;
cout << "عدد شما به فارسی: " << perNumStr << endl; // print "extended hindu-arabic number"
cout << endl;
cout << "عدد شما به انگلیسی: " << enNumStr << endl; // print "arabic number"
}
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.