有没有中文数字转阿拉伯数字的代码? 刚搜了搜都是阿拉伯数字转中文的,没有反过来的。不知道为什么。实在没有就自己写了,不过懒啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 hehe ...写好了 给我一份 嘎嘎...在这里留个信 呵呵 前几天刚好看到,在这儿http://community.csdn.net/Expert/topic/5676/5676060.xml?temp=.8997461 //适合十万以下的整数,也很容易扩展成更大的数int ChineseDigitToNum(CString strText){ struct NumInfo { int nTenThousand; int nThousand; int nHundred; int nTen; int nOne; }tNumInfo; tNumInfo.nTenThousand = 0; tNumInfo.nThousand = 0; tNumInfo.nHundred = 0; tNumInfo.nTen = 0; tNumInfo.nOne = 0; CString strWord; int nCnt = strText.GetLength(); for(int i=0; i<nCnt; i+=2) { strWord = strText.Mid(i,2); if("万" == strWord) { tNumInfo.nTenThousand = tNumInfo.nOne; tNumInfo.nOne = 0; } else if("千" == strWord) { tNumInfo.nThousand = tNumInfo.nOne; tNumInfo.nOne = 0; } else if("百" == strWord) { tNumInfo.nHundred = tNumInfo.nOne; tNumInfo.nOne = 0; } else if("十" == strWord) { tNumInfo.nTen = tNumInfo.nOne; if(0 == tNumInfo.nTen) //eg."十八"="一十八" tNumInfo.nTen = 1; tNumInfo.nOne = 0; } else if("九" == strWord) tNumInfo.nOne = 9; else if("八" == strWord) tNumInfo.nOne = 8; else if("七" == strWord) tNumInfo.nOne = 7; else if("六" == strWord) tNumInfo.nOne = 6; else if("五" == strWord) tNumInfo.nOne = 5; else if("四" == strWord) tNumInfo.nOne = 4; else if("三" == strWord) tNumInfo.nOne = 3; else if("二" == strWord) tNumInfo.nOne = 2; else if("一" == strWord) tNumInfo.nOne = 1; else if("零" == strWord) tNumInfo.nOne = 0; } return tNumInfo.nTenThousand*10000 + tNumInfo.nThousand*1000 + tNumInfo.nHundred*100 + tNumInfo.nTen*10 + tNumInfo.nOne;} 自己动手,丰衣足食.#include <hash_map>#include <string>using namespace std;//author: mok/*处理了大部分汉字数字转回阿拉伯数字的情况,并包括“二零零六”这样的纯数字。受unsigned int所限,最大支持21亿多的数字*/class MokConvertChineseNumber{public: ConvertChineseNumber() { const char szNum1[]="零一二三四五六七八九"; const char szNum2[]="〇壹贰叁肆伍陆柒捌玖"; char temp[3]={0}; for(int i=0;i<strlen(szNum1);i+=2) { strncpy(temp,szNum1+i,2); m_num[temp]=i/2; strncpy(temp,szNum2+i,2); m_num[temp]=i/2; } m_level["十"]=10; m_level["百"]=100; m_level["千"]=1000; m_level["万"]=10000; m_level["亿"]=100000000; m_level["拾"]=10; m_level["佰"]=100; m_level["仟"]=1000; m_level["萬"]=10000; m_level["億"]=100000000; } //对于一些异常的情况返回-1 //传进来的应该是中文数字 inline unsigned int Chinese2Int(const char * szInput) { if(szInput==NULL || *szInput==0 ) return -1; unsigned int yi=0,wan=0,ge=0,temp=0; const char * p =szInput; char buf[3]={0}; // 处理"二零零六" 这样的数字 // bLevel表示是否出现十百千万这样的字 // 不能通过检测是否只含有数字的方式来判断,因为有:一千 零六 百 这样连续的数字 bool bLastNum=false,bLevel=false; char numbuf[32]={0}; while(p && *p) { if(*p>0) return -1; if(*(p+1)==0) return -1; //遇到了半个汉字 strncpy(buf,p,2); if(m_level.find(buf)!=m_level.end()) //是十百千万亿 { bLevel=true; bLastNum=false; unsigned int nMul=m_level[buf]; if(nMul==100000000) { ge+=temp; temp=0; yi=wan*10000+ge; wan=0; ge=0; } else if(nMul==10000) //万 { ge+=temp; wan=ge; ge=0; temp=0; }else{ if(nMul==10 && temp==0) ge+=10; else ge+=temp*nMul; temp=0; } }else if(m_num.find(buf)!=m_num.end()) { if(bLastNum) //发现两个连续的数字了 { temp=temp*10+m_num[buf]; }else{ bLastNum=true; temp+=m_num[buf]; } } p+=2; } return bLevel?yi*100000000+wan*10000+ge+temp:temp; }private: hash_map<string,int> m_num; hash_map<string,int> m_level;};测试代码: MokConvertChineseNumber ccn; while(1) { char buf[1024]; printf("enter chinese number: \n"); gets(buf); int len=strlen(buf); if(buf[len-1]=='\r' || buf[len-1]=='\n') buf[len-1]=0; printf("%d\n", ccn.Chinese2Int(buf)); }测试结果:enter chinese number:二零零六2006enter chinese number:二零零七零八200708enter chinese number:贰拾壹亿叁仟零贰拾万零叁仟零肆拾2130203040enter chinese number:贰壹叁零贰零叁零肆213020304 请问不调用OnDraw()的话,能清楚窗口的内容吗? 如何去掉用polyline 画的线 忍不住了,我要提一个问题。关于VC++的类,关了VC后再打开就没有了 求助多线程网络编程(有酬谢) 怎么改变CProgressCtrl进度条的颜色? 无奈,IIS,DLLHOST,窗口 release中AfxBeginThread编译错误c2665 请问哪里有关于DES加密的dll或者源程序下载? VS2008创建win32程序使用Dialog,icon资源报错这些资源的ID未定义 文件加载的MFC程序中 IE,API注入问题 MDI 按钮 没有响应
http://community.csdn.net/Expert/topic/5676/5676060.xml?temp=.8997461
int ChineseDigitToNum(CString strText)
{
struct NumInfo
{
int nTenThousand;
int nThousand;
int nHundred;
int nTen;
int nOne;
}tNumInfo; tNumInfo.nTenThousand = 0;
tNumInfo.nThousand = 0;
tNumInfo.nHundred = 0;
tNumInfo.nTen = 0;
tNumInfo.nOne = 0; CString strWord;
int nCnt = strText.GetLength();
for(int i=0; i<nCnt; i+=2)
{
strWord = strText.Mid(i,2);
if("万" == strWord)
{
tNumInfo.nTenThousand = tNumInfo.nOne;
tNumInfo.nOne = 0;
}
else if("千" == strWord)
{
tNumInfo.nThousand = tNumInfo.nOne;
tNumInfo.nOne = 0;
}
else if("百" == strWord)
{
tNumInfo.nHundred = tNumInfo.nOne;
tNumInfo.nOne = 0;
}
else if("十" == strWord)
{
tNumInfo.nTen = tNumInfo.nOne;
if(0 == tNumInfo.nTen) //eg."十八"="一十八"
tNumInfo.nTen = 1;
tNumInfo.nOne = 0;
}
else if("九" == strWord)
tNumInfo.nOne = 9;
else if("八" == strWord)
tNumInfo.nOne = 8;
else if("七" == strWord)
tNumInfo.nOne = 7;
else if("六" == strWord)
tNumInfo.nOne = 6;
else if("五" == strWord)
tNumInfo.nOne = 5;
else if("四" == strWord)
tNumInfo.nOne = 4;
else if("三" == strWord)
tNumInfo.nOne = 3;
else if("二" == strWord)
tNumInfo.nOne = 2;
else if("一" == strWord)
tNumInfo.nOne = 1;
else if("零" == strWord)
tNumInfo.nOne = 0;
} return tNumInfo.nTenThousand*10000 + tNumInfo.nThousand*1000 +
tNumInfo.nHundred*100 + tNumInfo.nTen*10 + tNumInfo.nOne;
}
自己动手,丰衣足食.#include <hash_map>
#include <string>
using namespace std;//author: mok
/*
处理了大部分汉字数字转回阿拉伯数字的情况,并包括“二零零六”这样的纯数字。
受unsigned int所限,最大支持21亿多的数字
*/
class MokConvertChineseNumber
{
public:
ConvertChineseNumber()
{
const char szNum1[]="零一二三四五六七八九";
const char szNum2[]="〇壹贰叁肆伍陆柒捌玖";
char temp[3]={0};
for(int i=0;i<strlen(szNum1);i+=2)
{
strncpy(temp,szNum1+i,2);
m_num[temp]=i/2;
strncpy(temp,szNum2+i,2);
m_num[temp]=i/2;
}
m_level["十"]=10;
m_level["百"]=100;
m_level["千"]=1000;
m_level["万"]=10000;
m_level["亿"]=100000000; m_level["拾"]=10;
m_level["佰"]=100;
m_level["仟"]=1000;
m_level["萬"]=10000;
m_level["億"]=100000000;
}
//对于一些异常的情况返回-1
//传进来的应该是中文数字
inline unsigned int Chinese2Int(const char * szInput)
{
if(szInput==NULL || *szInput==0 ) return -1; unsigned int yi=0,wan=0,ge=0,temp=0;
const char * p =szInput;
char buf[3]={0}; // 处理"二零零六" 这样的数字
// bLevel表示是否出现十百千万这样的字
// 不能通过检测是否只含有数字的方式来判断,因为有:一千 零六 百 这样连续的数字
bool bLastNum=false,bLevel=false;
char numbuf[32]={0}; while(p && *p)
{
if(*p>0) return -1;
if(*(p+1)==0) return -1; //遇到了半个汉字
strncpy(buf,p,2);
if(m_level.find(buf)!=m_level.end()) //是十百千万亿
{
bLevel=true;
bLastNum=false;
unsigned int nMul=m_level[buf];
if(nMul==100000000)
{
ge+=temp; temp=0; yi=wan*10000+ge; wan=0; ge=0;
}
else if(nMul==10000) //万
{
ge+=temp; wan=ge; ge=0; temp=0;
}else{
if(nMul==10 && temp==0)
ge+=10;
else
ge+=temp*nMul;
temp=0;
}
}else if(m_num.find(buf)!=m_num.end())
{
if(bLastNum) //发现两个连续的数字了
{
temp=temp*10+m_num[buf];
}else{
bLastNum=true;
temp+=m_num[buf];
}
}
p+=2;
}
return bLevel?yi*100000000+wan*10000+ge+temp:temp;
}private:
hash_map<string,int> m_num;
hash_map<string,int> m_level;
};
测试代码:
MokConvertChineseNumber ccn;
while(1)
{
char buf[1024];
printf("enter chinese number: \n");
gets(buf);
int len=strlen(buf);
if(buf[len-1]=='\r' || buf[len-1]=='\n')
buf[len-1]=0;
printf("%d\n", ccn.Chinese2Int(buf));
}测试结果:
enter chinese number:
二零零六
2006
enter chinese number:
二零零七零八
200708
enter chinese number:
贰拾壹亿叁仟零贰拾万零叁仟零肆拾
2130203040
enter chinese number:
贰壹叁零贰零叁零肆
213020304