C++接口实现汉字拼音转换

方法1:

#define _CRT_SECURE_NO_WARNINGS 

#include<string>
#include<iostream>

using namespace std;



void GetPinYin(unsigned char* Chinese, string& PinYin);

int main()
{
   
	unsigned char chinese[] = "易烊千玺";
	string pinyin;
	GetPinYin(chinese, pinyin);
	int dsa = 1;
	cout << pinyin << endl;
	system("pause");
	return 0;

}

string pinyin[] =
{
   
	"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",
	"pou",    "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"
};

int code_qw[] =
{
   
	1601,1603,1616,1625,1628,1637,1655,1663,1678,1690,
	1713,1728,1732,1738,1762,1774,1778,1782,1788,1803,
	1822,1833,1834,1845,1852,1857,1862,1867,1869,1880,
	1883,1893,1912,1921,1927,1937,1952,1968,1973,1985,
	2007,2008,2015,2021,2026,2033,2035,2047,2053,2054,
	2058,2061,2069,2072,2078,2084,2102,2117,2122,2134,
	2137,2144,2163,2179,2188,2201,2210,2211,2221,2228,
	2243,2249,2253,2262,2274,2287,2288,2302,2310,2327,
	2338,2350,2365,2380,2381,2382,2433,2435,2441,2452,
	2461,2471,2488,2489,2491,2504,2519,2528,2546,2552,
	2555,2566,2569,2585,2588,2594,2601,2608,2627,2630,
	2639,2657,2659,2663,2668,2677,2684,2708,2717,2722,
	2736,2750,2771,2777,2787,2846,2863,2909,2922,2950,
	2977,3003,3028,3030,3047,3072,3079,3089,3106,3110,
	3115,3121,3128,3132,3147,3151,3153,3157,3161,3168,
	3173,3177,3179,3187,3204,3208,3212,3219,3222,3237,
	3244,3253,3255,3266,3269,3309,3310,3324,3335,3348,
	3353,3364,3379,3390,3405,3411,3431,3445,3451,3453,
	3460,3472,3481,3487,3502,3508,3520,3521,3537,3540,
	3548,3562,3571,3579,3581,3587,3593,3594,3617,3620,
	3635,3642,3647,3650,3651,3656,3657,3659,3660,3661,
	3672,3679,3681,3683,3690,3691,3703,3707,3711,3714,
	3715,3716,3718,3722,3723,3730,3736,3742,3750,3755,
	3762,3771,3773,3787,3810,3814,3818,3820,3825,3834,
	3842,3843,3858,3894,3903,3925,3933,3948,3953,3964,
	3977,3979,3987,4006,4017,4025,4027,4031,4036,4039,
	4041,4051,4053,4054,4064,4067,4077,4079,4082,4084,
	4086,4089,4093,4103,4106,4110,4113,4114,4115,4124,
	4126,4142,4150,4161,4173,4189,4206,4253,4263,4302,
	4304,4308,4310,4313,4317,4321,4325,4341,4349,4353,
	4365,4368,4379,4382,4390,4405,4414,4432,4445,4456,
	4457,4461,4476,4484,4489,4492,4508,4521,4525,4536,
	4538,4544,4547,4558,4565,4567,4584,4594,4633,4643,
	4646,4655,4684,4725,4738,4764,4784,4809,4829,4839,
	4854,4861,4870,4889,4905,4911,4925,4941,4974,4991,
	5012,5027,5080,5102,5120,5121,5136,5156,5207,5227,
	5237,5249,5252,5259,5263,5266,5280,5284,5285,5286,
	5290,5310,5316,5333,5348,5358,5368,5384,5405,5448,
	5459,5473,5505,5507,5508,5514,5521,5527,5529,5540,
	5555,5562,5566,5574,5576,5580,5582
};

void GetOtherCodePinYin(int nCode, string& strValue);

void GetPinYin(unsigned char* Chinese, string& PinYin)
{
   
	int chinese_str_len = strlen((char*)Chinese);
	for (int i = 0; i < chinese_str_len; i++)
	{
   
		if (Chinese[i] > 0 && Chinese[i] < 160)
		{
   
			PinYin += Chinese[i];
		}
		else
		{
   
			//int ascii_code = Chinese[i]*256 + Chinese[i+1] - 256*256;
			int ascii_code = (Chinese[i] - 0xa0) * 100 + Chinese[i + 1] - 0xa0;
			++i;
			if (ascii_code < 1601) //未知字符
			{
   
				PinYin += "?";
				continue;
			}

			if (ascii_code > 5589)//OtherCode
			{
   
				string s;
				GetOtherCodePinYin(ascii_code, s);
				PinYin += s;
				continue;
			}

			for (int j = 396; j >= 0; j--) //区位码
			{
   
				if (ascii_code >= code_qw[j])
				{
   
					PinYin += pinyin[j];
					break;
				}
			}
		}
	}
}

void GetOtherCodePinYin(int nCode, string& strValue)
{
   
	switch (nCode)
	{
   
	case 6325: case 6436: case 7571: case 7925:  strValue = "a";  break;
	case 6263: case 6440: case 7040: case 7208: case 7451: case 7733: case 7945: case 8616:  strValue = "ai";  break;
	case 5847: case 5991: case 6278: case 6577: case 6654: case 7281: case 7907: case 8038: case 8786:  strValue = "an";  break;
		strValue = "ang";  break;
	case 5974: case 6254: case 6427: case 6514: case 6658: case 6959: case 7033: case 7081: case 7365: case 8190: case 8292: case 8643: case 8701: case 8773:  strValue = "ao";  break;
	case 6056: case 6135: case 6517: case 7857: case 8446: case 8649: case 8741:  strValue = "ba";  break;
	case 6267: case 6334: case 7494:  strValue = "bai";  break;
	case 5870: case 5964: case 7851: case 8103: case 8113: case 8418:  strValue = "ban";  break;
	case 6182: case 6826:  strValue = "bang";  break;
	case 6165: case 7063: case 7650: case 8017: case 8157: case 8532: case 8621:  strValue = "bao";  break;
	case 5635: case 5873: case 5893: case 5993: case 6141: case 6703: case 7753: case 8039: case 8156: case 8645: case 8725:  strValue = "bei";  break;
	case 5946: case 5948: case 7458: case 7928:  strValue = "ben";  break;
	case 6452: case 7420:  strValue = "beng";  break;
	case 5616: case 5734: case 6074: case 6109: case 6221: case 6333: case 6357: case 6589: case 6656: case 6725: case 6868: case 6908: case 6986: case 6994: case 7030: case 7052: case 7221: case 7815: case 7873: case 7985: case 8152: case 8357: case 8375: case 8387: case 8416: case 8437: case 8547: case 8734:  strValue = "bi";  break;
	case 5650: case 5945: case 6048: case 6677: case 6774: case 7134: case 7614: case 7652: case 7730: case 7760: case 8125: case 8159: case 8289: case 8354: case 8693:  strValue = "bian";  break;
	case 7027: case 7084: case 7609: case 7613: case 7958: case 7980: case 8106: case 8149: case 8707: case 8752:  strValue = "biao";  break;
	case 8531:  strValue = "bie";  break;
	case 5747: case 6557: case 7145: case 7167: case 7336: case 7375: case 7587: case 7957: case 8738: case 8762:  strValue = "bin";  break;
	case 5787: case 5891: case 6280:  strValue = "bing";  break;
	case 5781: case 6403: case 6636: case 7362: case 7502: case 7771: case 7864: case 8030: case 8404: case 8543: case 8559:  strValue = "bo";  break;
	case 6318: case 6945: case 7419: case 7446: case 7848: case 7863: case 8519:  strValue = "bu";  break;
	case 6474: case 7769:  strValue = "ca";  break;
		strValue = "cai";  break;
	case 6978: case 7078: case 7218: case 8451: case 8785:  strValue = "can";  break;
	case 5687:  strValue = "cang";  break;
	case 6448: case 6878: case 8309: case 8429:  strValue = "cao";  break;
	case 6692:  strValue = "ce";  break;
	case 6515: case 6825:  strValue = "cen";  break;
	case 6465:  strValue = "ceng";  break;
	case 6639: case 6766: case 7017: case 7230: case 7311: case 7322: case 7363: case 7942: case 7979: case 8135:  strValue = "cha";  break;
	case 5713: case 7846: case 8091: case 8218:  strValue = "chai";  break;
	case 5770: case 5838: case 6159: case 6667: case 6893: case 6904: case 6981: case 7031: case 7086: case 7472: case 7688: case 7966: case 8324: case 8580:  strValue = "chan";  break;
	case 5686: case 5943: case 6041: case 6137: case 6660: case 6568: case 6749: case 7029: case 7047: case 7438: case 7509: case 8680:  strValue = "chang";  break;
	case 6687: case 7443: case 8173:  strValue = "chao";  break;
	case 5969: case 7726:  strValue = "che";  break;
	case 5840: case 5863: case 6251: case 6433: case 6923: case 7201: case 7320: case 7755: case 8619:  strValue = "chen";  break;
	case 5609: case 5984: case 7239: case 7263: case 7583: case 7810: case 7881: case 7905: case 8146: case 8241: case 8508:  strValue = "cheng";  break;
	case 5749: case 6015: case 6061: case 6319: case 6374: case 6420: case 6445: case 6633: case 7042: case 7523: case 7787: case 8023: case 8101: case 8161: case 8231: case 8304: case 8355: case 8388: case 8489: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值