پاسخ : ترفند: یکی از روش‌های تبدیل اعداد انگلیسی به فارسی در c++ 11 به همراه boost

ساخت وبلاگ

اگر خواستید می‌تونید بجای استفاده از توابع معمولی از عبارات لامبدا (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"

}

اوبونتو...
ما را در سایت اوبونتو دنبال می کنید

برچسب : نویسنده : استخدام کار ubuntu بازدید : 252 تاريخ : دوشنبه 8 شهريور 1395 ساعت: 20:57