• 中文字符转拼音源码


    中文字符转拼音源码
    2011-11-21      0 个评论      
    收藏    我要投稿

    只对GB2312编码有效

    /* 汉字读音分界点处的码值数组*/ 

    static const unsigned short code_pin[] = { 

        0xb0a1,0xb0a3,0xb0b0,0xb0b9,0xb0bc,0xb0c5,0xb0d7,0xb0df,0xb0ee,0xb0fa,0xb1ad,0xb1bc,0xb1c0,0xb1c6, 

        0xb1de,0xb1ea,0xb1ee,0xb1f2,0xb1f8,0xb2a3,0xb2b8,0xb2c1,0xb2c2,0xb2cd,0xb2d4,0xb2d9,0xb2de,0xb2e3, 

        0xb2e5,0xb2f0,0xb2f3,0xb2fd,0xb3ac,0xb3b5,0xb3bb,0xb3c5,0xb3d4,0xb3e4,0xb3e9,0xb3f5,0xb4a7,0xb4a8, 

        0xb4af,0xb4b5,0xb4ba,0xb4c1,0xb4c3,0xb4cf,0xb4d5,0xb4d6,0xb4da,0xb4dd,0xb4e5,0xb4e8,0xb4ee,0xb4f4, 

        0xb5a2,0xb5b1,0xb5b6,0xb5c2,0xb5c5,0xb5cc,0xb5df,0xb5ef,0xb5f8,0xb6a1,0xb6aa,0xb6ab,0xb6b5,0xb6bc, 

        0xb6cb,0xb6d1,0xb6d5,0xb6de,0xb6ea,0xb6f7,0xb6f8,0xb7a2,0xb7aa,0xb7bb,0xb7c6,0xb7d2,0xb7e1,0xb7f0, 

        0xb7f1,0xb7f2,0xb8c1,0xb8c3,0xb8c9,0xb8d4,0xb8dd,0xb8e7,0xb8f8,0xb8f9,0xb8fb,0xb9a4,0xb9b3,0xb9bc, 

        0xb9ce,0xb9d4,0xb9d7,0xb9e2,0xb9e5,0xb9f5,0xb9f8,0xb9fe,0xbaa1,0xbaa8,0xbabb,0xbabe,0xbac7,0xbad9, 

        0xbadb,0xbadf,0xbae4,0xbaed,0xbaf4,0xbba8,0xbbb1,0xbbb6,0xbbc4,0xbbd2,0xbbe7,0xbbed,0xbbf7,0xbcce, 

        0xbcdf,0xbda9,0xbdb6,0xbdd2,0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a,0xc9ad,0xc9ae,0xc9af,0xc9b8,0xc9ba, 

        0xc9ca,0xc9d2,0xc9dd,0xc9e9,0xc9f9,0xcaa6,0xcad5,0xcadf,0xcba2,0xcba4,0xcba8,0xcbaa,0xcbad,0xcbb1, 

        0xcbb5,0xcbb9,0xcbc9,0xcbd1,0xcbd4,0xcbe1,0xcbe4,0xcbef,0xcbf2,0xcbfa,0xcca5,0xccae,0xccc0,0xcccd, 

        0xccd8,0xccd9,0xccdd,0xccec,0xccf4,0xccf9,0xccfc,0xcda8,0xcdb5,0xcdb9,0xcdc4,0xcdc6,0xcdcc,0xcdcf, 

        0xcdda,0xcde1,0xcde3,0xcdf4,0xcdfe,0xcec1,0xcecb,0xcece,0xced7,0xcef4,0xcfb9,0xcfc6,0xcfe0,0xcff4, 

        0xd0a8,0xd0bd,0xd0c7,0xd0d6,0xd0dd,0xd0e6,0xd0f9,0xd1a5,0xd1ab,0xd1b9,0xd1c9,0xd1ea,0xd1fb,0xd2ac, 

        0xd2bb,0xd2f0,0xd3a2,0xd3b4,0xd3b5,0xd3c4,0xd3d9,0xd4a7,0xd4bb,0xd4c5,0xd4d1,0xd4d4,0xd4db,0xd4df, 

        0xd4e2,0xd4f0,0xd4f4,0xd4f5,0xd4f6,0xd4fa,0xd5aa,0xd5b0,0xd5c1,0xd5d0,0xd5da,0xd5e4,0xd5f4,0xd6a5, 

        0xd6d0,0xd6db,0xd6e9,0xd7a5,0xd7a7,0xd7a8,0xd7ae,0xd7b5,0xd7bb,0xd7bd,0xd7c8,0xd7d7,0xd7de,0xd7e2, 

        0xd7ea,0xd7ec,0xd7f0,0xd7f2 }; 

    /* 汉字读音数组*/ 

    static const char *str_pin[] = { 

        "a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao", 

        "bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan", 

        "chang","chao","che","chen","cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui", 

        "chun","chuo","ci","cong","cou","cu","cuan","cui","cun","cuo","da","dai","dan","dang","dao", 

        "de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan","dui","dun","duo", 

        "e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang", 

        "gao","ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun", 

        "guo","ha","hai","han","hang","hao","he","hei","hen","heng","hong","hou","hu","hua","huai", 

        "huan","huang","hui","hun","huo","ji","jia","jian","jiang","jiao","jie","jin","jing", 

        "jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken","keng", 

        "kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang", 

        "lao","le","lei","leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou", 

        "lu","lv","luan","lue","lun","luo","ma","mai","man","mang","mao","me","mei","men","meng", 

        "mi","mian","miao","mie","min","ming","miu","mo","mou","mu","na","nai","nan","nang","nao","ne", 

        "nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong","nu","nv","nuan", 

        "nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie", 

        "pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu", 

        "quan","que","qun","ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui", 

        "run","ruo","sa","sai","san","sang","sao","se","sen","seng","sha","shai","shan","shang","shao", 

        "she","shen","sheng","shi","shou","shu","shua","shuai","shuan","shuang","shui","shun","shuo", 

        "si","song","sou","su","suan","sui","sun","suo","ta","tai","tan","tang","tao","te","teng", 

        "ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo","wa","wai","wan", 

        "wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing", 

        "xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo", 

        "yong","you","yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng", 

        "zha","zhai","zhan","zhang","zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua", 

        "zhuai","zhuan","zhuang","zhui","zhun","zhuo","zi","zong","zou","zu","zuan","zui","zun","zuo"}; 

    /* 数组长度*/ 

    static const size_t SIZE_ARRAY = sizeof(code_pin) / sizeof(short); 

    #include <iostream> 

    #include <string> 

    using std::string; 

    using std::cout; 

    using std::endl; 

    /* 获取一个汉字的拼音*/ 

    const char* get_pin(unsigned short char_zh) 

        size_t low = 0, high = SIZE_ARRAY - 1; 

        size_t index; 

        while (high - low != 1) 

        { 

            index = (low + high) / 2; 

            if (code_pin[index] == char_zh) return str_pin[index]; 

            if (code_pin[index] < char_zh) low = index; 

            else high = index; 

        } 

        return str_pin[code_pin[high] <= char_zh ? high : low];    

    /* 将中英混合字符串转化成拼音形式*/ 

    string str_to_pin(string const &input) 

        string result ; 

        unsigned short char_zh; 

        int inputLength = input.length(); 

        unsigned char high, low; 

        for (int i = 0; i < inputLength; ++i) 

        { 

            high = input[i] ; 

            if (high < 0x80) result.append(1, high); 

            else 

            { 

                low = input[++i]; 

                char_zh = (high << 8 ) + low ; 

                result.append(get_pin(char_zh)); 

            } 

        } 

        return result; 

    /* 将中英混合字符串转化成拼音形式,每个拼音后跟一个空格*/ 

    string str_to_pin_space(string const &input) 

        string result; 

        unsigned short char_zh; 

        int inputLength = input.length(); 

        unsigned char high, low; 

        for (int i = 0; i < inputLength; ++i) 

        { 

            high = input[i]; 

            if(high < 0x80) 

            { 

                if (i > 0 && input[i-1] < 0)  result.append(1, ' '); 

                result.append(1, high); 

            } 

            else 

            { 

                if(i > 0) result.append(1, ' '); 

                low = input[++i]; 

                char_zh = (high << 8 ) + low ; 

                result.append(get_pin(char_zh)); 

            } 

        } 

        return result; 

    /* 

       将中英及数字混合串转化成拼音,且每一连续部分之间放置一个空格。

       输入必须满足只有上述三种字符的情况,正确性由调用端保证。

    */ 

    string str_to_pin_space_(string const &input) 

        string result(1, ' ') ; 

        unsigned short char_zh; 

        int inputLength = input.length(); 

        unsigned char high, low; 

        for (int i = 0; i < inputLength; ++i) 

        { 

            high = input[i] ; 

            if (high < 58) 

            { 

                if (i > 0 && (input[i-1] < 0 || input[i-1] > 57)) 

                { 

                    result.append(1, ' '); 

                } 

                result.append(1, high); 

            } 

            else if (high > 64 && high < 123) 

            { 

                if (i > 0 && (input[i-1] < 0 || input[i-1] < 58)) 

                { 

                    result.append(1, ' '); 

                } 

                result.append(1, high); 

            } 

            else 

            { 

                if(i > 0) result.append(1, ' '); 

                low = input[++i]; 

                char_zh = (high << 8 ) + low ; 

                result.append(get_pin(char_zh)); 

            } 

        } 

        return result; 

    /* 测试代码*/ 

    int main() 

        cout << str_to_pin_space_("zhong hua人民dfd啊a152左边35gdaf共和国") << endl; 

        return 0; 

    }   

    摘自 学无止境的blog

  • 相关阅读:
    hdu 5146 Sequence
    hdu 1232 畅通工程
    hdu 1213 How Many Tables
    hdu 2822 Dogs
    hdu 1242 Rescue
    hdu 5101 Select
    hdu 1873 看病要排队
    hdu 5112 A Curious Matt
    hdu 5154 Harry and Magical Computer
    hdu 1548 A strange lift
  • 原文地址:https://www.cnblogs.com/lizihong/p/4322688.html
Copyright © 2020-2023  润新知