iSumer 发表于 2005-6-10 10:59:40

请教:一个阿拉伯数字转换为大写数字脚本的理解(内含详细源码)

以下是一个将阿拉伯数字转换为大写数字的shell脚本(bash),但我对它的实现方法基本看不懂,请各位帮忙看下,给我讲解下思路,谢谢!


阿拉伯数字转换为大写数字的脚本
#!/bin/bash

cconvert(){

declare -a cnum;
declare -a cmag;

cnum="壹"
cnum="贰"
cnum="叁"
cnum="肆"
cnum="伍"
cnum="陆"
cnum="柒"
cnum="拔"
cnum="玖"
cnum="零"

cmag=""
cmag="拾"
cmag="佰"
cmag="仟"
cmag="万"
cmag="拾"
cmag="百"
cmag="千"


tempalpha="$1";
ctempmag=$2;

if [ $tempalpha == "00000000" ] ; then
        CSTR="";
        return 0;
fi
let templength="${#tempalpha}";

CSTR="";
for ((m=0;m<templength;m++))
do
        tempi=${tempalpha:m:1};
        let tempj="$templength-$m-1";

        if ((( tempi == 0 )) && (( tempj ==4 ))); then
                CSTR=$CSTR"万";
        elif (( tempi == 0 )); then
                CSTR=$CSTR${cnum};
        else
                CSTR=$CSTR${cnum[$tempi]}${cmag[$tempj]};
        fi

done

CSTR=$(echo $CSTR | sed -e 's/零零*/零/g' -e 's/零$//g' -e 's/零零零万//g');
CMAG="";
for ((m=0;m<ctempmag;m++))
do
        CMAG=$CMAG"亿";
done

CSTR=$CSTR$CMAG;
}


alpha=$1;
length=${#alpha};
let k="$length/8";
let modl="$length%8";
MYSTR="";

tempstr=${alpha:0:$modl};
if ((modl>0)); then
        cconvert $tempstr $k;
fi

MYSTR=$MYSTR$CSTR;
for ((i=0;i<k;i++))
do
        let pos="$i*8+modl";
        tempstr=${alpha:$pos:8};
        let tempmag="$k-$i-1";
        cconvert $tempstr $tempmag;
        MYSTR=$MYSTR$CSTR;
done

echo $MYSTR | sed -e 's/亿零万/亿零/g'-e 's/零万/万/g' -e 's/零亿/亿/g' -e 's/零零*/零/g' -e 's/零$//g'
页: [1]
查看完整版本: 请教:一个阿拉伯数字转换为大写数字脚本的理解(内含详细源码)