LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: KornLee

考考你:如果将数字转换为大写数字?

[复制链接]
 楼主| 发表于 2003-6-21 00:48:06 | 显示全部楼层
I 服了 YOU们 !!!
我已经将两位的大作置顶于[脚本欣赏]区!特此通知;)
发表于 2003-6-21 01:08:35 | 显示全部楼层
牛人啦
发表于 2003-6-21 21:49:09 | 显示全部楼层
读了一下penny的代码,重新整理了思路,把主要算法重写了,俺也把脚本升个级,现在这个脚本也没有数字限制,呵呵。不知还有没有新bug...
#!/usr/bin/perl -w

# Chinese count method
# wrote by Lyoo
# iamlyoo@163.com
# 2003/07/22


# match @unit = qw / 个 拾 佰 仟 万 拾万 佰万 仟万/
@unit = qw / A B C D E F G H / ;

############################################################################
#
# receive user's input
#
############################################################################

$count = 0;
while ( $count < 1 ) {
        print "lease input a number:";
        chomp ($number = <STDIN>);
        if ( $number =~ /^[-,+]?\d+\.?\d*$/ ) {
                $count += 1;
        } else {
                print "It's not a Number!\n";
                redo;
        }
}

############################################################################
#
# create a number_array
#
############################################################################

# add a number to the number_string,
# so that the while-loop can get the "0" in the tail of the number_string.
$number_9 = $number."9";

# convert the number to a array.
$dot = "no";
while ($number_9) {
        my $single = $number_9;
        $single =~ s/([\d,.,+,-]).*/$1/;
        $number_9 =~ s/[\d,.,+,-](.*)/$1/;
        push (@number_array,$single);
        $dot = "yes" if $single eq ".";
}

# delect the addition number.reverse the array.
pop @number_array;
@number_array = reverse @number_array;

# get number's sylobm.
$sylobm = "";
$sylobm = pop @number_array if $number_array[-1] =~ /[+,-]/;

# get the number_dot_string.
$number_dot_string = "";
if ($dot eq "yes") {
        while (@number_array) {
                $number_dot_string .= shift @number_array;
                last if $number_dot_string =~ /\d\./;
        };
        $number_dot_string = reverse $number_dot_string;
};


#############################################################################
#
# creat a number_unit_array
#
#############################################################################

$min_unit = 9;
$j = 0;
$i = 0;
$n = 0;

foreach (@number_array) {
        push (@number_unit_array,$unit[$i].$_);

        if ($i == 0) {
                $j++;
                $min_unit = "on";
                $switch = "on"
        };

        unless ($switch eq "off" || $_ eq "0") {
                $min_unit = $n;
        };

        unless ($switch eq "off" || $min_unit eq "on") {
                $number_unit_array[$min_unit] = ("Z" x ($j-1)).$number_unit_array[$min_unit];
                $switch = "off";
        }

        $i++;
        $n++;
        $i = $i % 8;
}

#############################################################################
#
# modify the number_unit_string
#
#############################################################################

foreach (@number_unit_array) {
        $number_unit_string .= $_;
}
$number_unit_string = reverse $number_unit_string;
$_ = $number_unit_string;
s/0[A-H]/0/g;
s/0+/0/g;
s/A//g;
s/0+$//;

#print "$_\n";

s/H(\d)G(\d)F(\d)E/D$1C$2B$3E/g;

s/H(\d)G(\d)F/D$1C$2F/g;
s/H(\d)G(\d)E/D$1C$2E/g;
s/H(\d)F(\d)E/D$1B$2E/g;
s/G(\d)F(\d)E/C$1B$2E/g;


s/H(\d)E/D$1E/g;
s/G(\d)E/C$1E/g;
s/F(\d)E/B$1E/g;

s/H(\d)F/D$1F/g;
s/G(\d)F/C$1F/g;

s/H(\d)G/D$1G/g;

$number_unit_string = "$sylobm"."$_"."$number_dot_string";

#############################################################################
#
# output the number_unit_string as a array
#
#############################################################################

# convert number_unit_string to array.
# it's ugly but without this action
# chinese can't output correct.
# I don't know why

while ($number_unit_string) {
        my $single = $number_unit_string;
        $single =~ s/([\w,.,+,-]).*/$1/;
        $number_unit_string =~ s/[\w,.,+,-](.*)/$1/;
        push (@number_unit_ok,$single);
}
#print "number_unit_ok is @number_unit_ok.\n";

foreach (@number_unit_ok) {
        &print_chinese;
}
print "\n";

sub print_chinese {
        if ($_ eq 0) {
        print "零";
        } elsif ($_ eq 1) {
                print "壹";
        } elsif ($_ eq 2) {
                print "贰";
        } elsif ($_ eq 3) {
                print "叁";
        } elsif ($_ eq 4) {
                print "肆";
        } elsif ($_ eq 5) {
                print "伍";
        } elsif ($_ eq 6) {
                print "陆";
        } elsif ($_ eq 7) {
                print "柒";
        } elsif ($_ eq 8) {
                print "捌";
        } elsif ($_ eq 9) {
                print "玖";
        } elsif ($_ eq A) {
                print "个";
        } elsif ($_ eq B) {
                print "拾";
        } elsif ($_ eq C) {
                print "佰";
        } elsif ($_ eq D) {
                print "仟";
        } elsif ($_ eq E) {
                print "万";
        } elsif ($_ eq F) {
                print "拾万";
        } elsif ($_ eq G) {
                print "佰万";
        } elsif ($_ eq H) {
                print "仟万";
        } elsif ($_ eq Z) {
                print "亿";
        } elsif ($_ eq "+") {
                print "<正>";
        } elsif ($_ eq "-") {
                print "<负>";
        } elsif ($_ eq ".") {
                print "<点>";
        }
}

#############################################################################
# the end of this script
#############################################################################


再次更新:
根据capture的提议和penny的想法,新的脚本可以读取正负数及小数了。
 楼主| 发表于 2003-6-22 01:57:28 | 显示全部楼层
牛人啦
发表于 2003-6-22 04:22:31 | 显示全部楼层
晕ing!!!
发表于 2003-6-22 08:47:21 | 显示全部楼层

的确利害,不过我要替你们把水再搅混一点!

楼主的问题里要求的是数字,而不是正整数,所以两位大牛似乎至少还应该考虑一下负数和小数的问题 :p
发表于 2003-6-22 09:48:06 | 显示全部楼层

回复: 的确利害,不过我要替你们把水再搅混一点!

最初由 capture 发表
楼主的问题里要求的是数字,而不是正整数,所以两位大牛似乎至少还应该考虑一下负数和小数的问题 :p


小数部分只要顺序读出就行,也不用考虑单位

so.......留给大家做思考题吧 P
发表于 2003-6-22 12:26:29 | 显示全部楼层
呵呵,好主意,俺又给更新版加了些代码。
javalee兄,请将“shell脚本欣赏区[展示你的作品的好去处]”一帖中的代码更新一下。:p
 楼主| 发表于 2003-6-22 13:22:20 | 显示全部楼层
遵命!;)
你写作的日期的一个"大BUG",我替你更正啦;)
发表于 2004-11-21 17:40:11 | 显示全部楼层
很好,学到的了e,
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表