|

楼主 |
发表于 2003-6-21 00:59:43
|
显示全部楼层
阿拉伯数字转换为大写数字的脚本[perl版本]
感谢作者 Y00兄
[code]
#!/usr/bin/perl -w
# Chinese count method
# wrote by Lyoo
# iamlyoo@163.com
# 2003/06/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
############################################################################ |
|