用PHP把阿拉伯数字转换为中文写法

今天在搜PHP算法的时候,看到PPC上的一个帖子,贴的是酷讯的笔试题,地址为: http://www.phpchina.com/bbs/viewthread.php?tid=43392&extra=&page=1
觉得第二题挺有意思的.
题目大意是这样的:

2、输入一个1~1亿的数字,打印中文,注意特殊情况
例如,输入7,打印七,123打印一百二十三

自己费了九牛二虎之力,终于解决了.
我的思路是把数字每4位分一组,然后再进行判断输出.

源码如下:

class timer {
var $StartTime = 0;
var $StopTime = 0;
var $TimeSpent = 0;
function start() {
$this->StartTime = microtime();
}
function stop() {
$this->StopTime = microtime();
}
function spent() {
if($this->TimeSpent) {
return $this->TimeSpent;
} else {
$StartMirco = substr( $this->StartTime,0,10);
$StartSecond = substr( $this->StartTime,11,10);
$StopMirco = substr( $this->StopTime,0,10);
$StopSecond = substr( $this->StopTime,11,10);
$start = doubleval( $StartMirco ) + $StartSecond;
$stop = doubleval( $StopMirco ) + $StopSecond;
$this->TimeSpent = $stop – $start;
return substr( $this->TimeSpent,0,8 ) .”秒”;
}
}
}

function groupTrans($numC,$type,$isFirst=’0′) {

//$shuzi = array(‘零’,’壹’,’贰’,’叁’,’肆’,’伍’,’陆’,’柒’,’捌’,’镹’);
$shuzi = array(‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’);
//4个数的单位
$danwei = array(”,’十’,’百’,’千’);
//每组数的单位
$quan = array(”,’万’,’亿’,’兆’);

$numLen = strlen($numC);
//echo ‘
‘.$numLen.’
‘;
$numLenL = 0;
$zero = 0;
$wei = 0;
$string = ”;

if(!$isFirst) {
if(intval($numC)==0) {
$string = $shuzi[0];
}
//长度大于等于3
else if($numLen>=3) {
if(strlen(intval($numC))<4) {
//$string .= ‘a’;
$string .= $shuzi[0];
$zero = 0;
$numLenL = strlen(intval($numC));
//echo $numLenL;
}
if(!$numLenL) {
$numLenL = $numLen;
}
//循环这组数字
for($i=$numLenL;$i>=0;$i–) {
//当前数字等于0
if($numC[$numLen-$i]==’0′) {
if(!$zero) {
$string .= $shuzi[0];
$zero = 1;
}
}
//当前数字不等于0
else {
$string .= $shuzi[$numC[$numLen-$i]].$danwei[$i-1];
//数字加单位
}
}
}
//长度小于等于2
else {
for($i=$numLen;$i>=0;$i–) {
$string .= $shuzi[$numC[$numLen-$i]].$danwei[$i-1];
}
}
}
else {
if($numLen>=3) {
//循环这组数字
for($i=$numLen;$i>=0;$i–) {
//当前数字等于0
if($numC[$numLen-$i]==’0′) {
if(!$zero) {
$string .= $shuzi[0];
$zero = 1;
}
}
//当前数字不等于0
else {
$string .= $shuzi[$numC[$numLen-$i]].$danwei[$i-1];
//数字加单位
}
}
}
//长度小于等于2
else {
for($i=$numLen;$i>=0;$i–) {
$string .= $shuzi[$numC[$numLen-$i]].$danwei[$i-1];
}
}
}

//最后再判断下末尾是否是零
//是零去掉
$numL = strlen($string);
//echo “numL : “.$numL.”
“;//exit;
//echo $string.”
“;
if($shuzi[0]==0) {
$charset = 1;
}
else {
$charset = 2;
}
$zero = substr($string,$numL-$charset,$numL);
if($zero==$shuzi[0]) {
$string = substr($string,0,$numL-$charset);
}
if($string) {
$string .= $quan[$type];
}

return $string;
}

function transform($number) {
//获取当前数字的长度
$num = strlen($number);
//每4个一组,获取组数
$group = ceil($num/4);

$naodai = ”;
//分组循环
for($j=$group;$j>=0;$j–){
//如果是最后的一组,千位至个位
if(strlen($number)==4) {
$naodai .= groupTrans($number,’0′);
break;
}
//如果数字未过万位
elseif(strlen($number)<=3) {
$naodai .= groupTrans($number,’0′,’1′);
}
//最开始的组
if($j==$group) {
$numH = substr($number,0,-(($j-1)*4));
$numS = substr($number,$num-($group-1)*4,($num-($group-$j)*4));
$number = $numS;
//把获取的组转化
$naodai .= groupTrans($numH,$j-1,’1′);
}
//除第一组后的组
else {
$numH = substr($number,0,4);
$numS = substr($number,4,strlen($number));
$number = $numS;
//把获取的组转化
$naodai .= groupTrans($numH,$j-1);
}

}
return $naodai;
}
$time = new timer();
$time->start();
echo transform(’20’);
$time->stop();
echo $time->spent();
/*echo ‘


‘;
echo ‘1000101
‘;
echo transform(‘1000101’);
echo ‘


‘;
echo ‘100101010
‘;
echo transform(‘100101010’);
echo ‘


‘;
echo ‘120
‘;
echo transform(‘120’);
echo ‘


‘;
echo ‘102
‘;
echo transform(‘102’);
echo ‘


‘;
echo ‘100000
‘;
echo transform(‘100000’);
echo ‘


‘;
echo ‘22002020
‘;
echo transform(‘22002020’);*/

?>

后来又测试网友贴出来的代码,
觉得这个最简洁了.

class timer {
var $StartTime = 0;
var $StopTime = 0;
var $TimeSpent = 0;
function start() {
$this->StartTime = microtime();
}
function stop() {
$this->StopTime = microtime();
}
function spent() {
if($this->TimeSpent) {
return $this->TimeSpent;
} else {
$StartMirco = substr( $this->StartTime,0,10);
$StartSecond = substr( $this->StartTime,11,10);
$StopMirco = substr( $this->StopTime,0,10);
$StopSecond = substr( $this->StopTime,11,10);
$start = doubleval( $StartMirco ) + $StartSecond;
$stop = doubleval( $StopMirco ) + $StopSecond;
$this->TimeSpent = $stop – $start;
return substr( $this->TimeSpent,0,8 ) .”秒”;
}
}
}

function num2CNnum($num)
{
$小=array(‘零’,’一’,’二’,’三’,’四’,’五’,’六’,’七’,’八’,’九’);
$大=array(‘十’,’百’,’千’,’万’,’亿’,’兆’,’万兆’,’亿兆’,’兆兆’);
for($i=0,$z=0,$strlen=strlen($num),$j=$strlen;$i<$strlen;$i++,$j–)
{
$get=substr($num,$i,1);
$get==0&&$j>4?$z++:$z=0;
$数字[$i]=($get==0&&(substr($num,$i+1,1)==0||substr($num,0,$i)<1))||($j%4==2&&$get==1)?”:$小[$get];
if($j%4==1&&$get==0&&$j>4&&$z<4&&substr($num,0,$i)>0)
$数字[$i]=$大[($j-1)/4+2].$数字[$i];
$数字[$i].=$j%4!=1&&$get?($j%4?$大[$j%4-2]:$大[2]):($get&&$j>1?$大[($j-1)/4+2]:”);
}
return join(”,$数字);
}
$time = new timer();
$time->start();
echo num2CNnum(’10’).”
“;
$time->stop();
echo $time->spent();
?>
我运行了下,对比发现,自己写的东西好烂啊!
自己写的运行时间是别人写的4倍.
唉!
平时多练练吧!
如果朋友们有兴趣的话,也可以自己试着写个玩哈!

发表评论

返回顶部