انجمنهای فارسی اوبونتو
کمک و پشتیبانی => برنامهسازی => نویسنده: علی ۵۰۰ در 08 شهریور 1395، 12:24 بظ
-
سلام
یکی از روشهای تبدیل اعداد انگلیسی به فارسی در 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
-
ممنون بابت کد ولی نه اون عددها بهشون میگن انگلیسی و نه به اون یکی دیگه میگن فارسی!
Arabic = 0123456789
Eastern Arabic =۰۱۲۳۴۵۶۷۸۹
https://en.wikipedia.org/wiki/Arabic_numerals
https://en.wikipedia.org/wiki/Eastern_Arabic_numerals
-
ممنون بابت کد ولی نه اون عددها بهشون میگن انگلیسی و نه به اون یکی دیگه میگن فارسی!
Arabic = 0123456789
Eastern Arabic =۰۱۲۳۴۵۶۷۸۹
https://en.wikipedia.org/wiki/Arabic_numerals
https://en.wikipedia.org/wiki/Eastern_Arabic_numerals
ممنونم از شما.
همانطور که شما فرمودید، در یونیکد اگر اشتباه نکنم اعداد به صورت زیر نامگذاری شده (از حالت سوم، در زبان فارسی استفاده میشه):
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;
}
در تصویر زیر، ورودی و خروجی نشان داده شده است:
(http://forum.ubuntu.ir/index.php?action=dlattach;topic=139271.0;attach=44471;image)
-
اگر خواستید میتونید بجای استفاده از توابع معمولی از عبارات لامبدا (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"
}
-
اینجوری هم میشه نوشتش :)
string enToPerNum( string str )
{
string nums = "۰۱۲۳۴۵۶۷۸۹", tmp = "";
for (int i=0; i<str.size(); i++)
tmp = tmp + nums[stoi(string(1,str[i]))*2] + nums[stoi(string(1,str[i]))*2+1];
return tmp;
}