LinuxSir.Org  
| 网站首页 | 注册账号 | 论坛帮助 |

欢迎来到LinuxSir.Org!
您还未登录,请登录后查看论坛,或者点击论坛上方的注册链接注册新账号。


发表新主题 回复
精华主题  
主题工具
旧 03-06-19, 21:32 第 1 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

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


比如:
输入12,如何转换成壹拾贰/拾贰?输入1020,转成壹千零贰贰等......???当然输入的数字是任意的!

此帖于 03-06-19 21:34 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-06-20, 17:53 第 2 帖
LYOO
 
LYOO 的头像
 
 
注册会员  
  注册日期: Jan 2003
  帖子: 782
  精华: 37
 

标题: 初学perl,写的代码比较拖沓冗杂,与大家交流。


JavaLee兄出的这题,看似容易,真写起来才感到难度不小,俺花了一整天的时间才完成这个脚本,脚本对数字取值限制在999,999,999,999,999,999,99以内(不过可以通过修改脚本来扩大取值范围,俺实在是懒得想怎样写一个自动的),虽然我测试了不少数字,估计bug还是会有,大家如果有兴趣,也来测试一下。

lyoo@lsd:~/shellsample$ ./convert.pl
Please input a number:132345670000357
壹佰叁拾贰亿亿叁仟肆佰伍拾陆亿柒仟万零叁佰伍拾柒

代码:
#!/usr/bin/perl -w # Chinese count method # wrote by Lyoo # iamlyoo@163.com # 2003/07/20 # match @unit = qw / 个 拾 佰 仟 / @unit = qw / A B C D / ; # match @unit_max = qw / 万 亿 万亿 亿亿 / @unit_max = qw / E F G H / ; $count = 0; while ( $count < 1 ) { print "Please input a number:"; chomp ($number = <STDIN>); if ( $number =~ /^\d+$/ && $number <= 99999999999999999999 ) { $count += 1; } else { print "It's not a Number or a GOOD number!\n"; print "Note: 0 < number < 999,999,999,999,999,999,99!\n"; redo; } } # 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. while ($number_9) { my $single = $number_9; $single =~ s/(\d).*/$1/; $number_9 =~ s/\d(.*)/$1/; push (@number_array,$single); } # delect the addion number.reverse the array. pop @number_array; @number_array = reverse @number_array; #print "the number_array is @number_array"; # deal with unit. $i = 0; foreach (@number_array) { $number_unit .= $unit[$i].$_; $i++; $i= $i % 4; } #print "the number_unit is $number_unit\n"; # deal with unit_max. $_ = $number_unit; s/A//; s/A/$unit_max[0]/; s/A/$unit_max[1]/; s/A/$unit_max[2]/; s/A/$unit_max[3]/; # delete the superfluous unit. s/[A-D]0/0/g; #print "number_unit_max now is $_\n"; # delete superfluous 0. s/0+/0/g; s/([E-H])0/$1/g; #print "number_unit_max now is $_\n"; s/EF/F/g; s/FG/G/g; s/GH/H/g; $number_unit_max = reverse $_; #print "the number_unit_max is $number_unit_max\n"; # convert number_unit_max to array. while ($number_unit_max) { my $single = $number_unit_max; $single =~ s/(\w).*/$1/; $number_unit_max =~ s/\w(.*)/$1/; push (@number_unit_max_array,$single); } #print "number_unit_max_array is @number_unit_max_array.\n"; foreach (@number_unit_max_array) { &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 "亿亿"; } }







__________________
http://211.92.88.40/~lyoo/bookmark/bookmark.html

此帖于 03-06-20 22:57 被 LYOO 编辑.
  LYOO 当前离线   回复时引用此帖
旧 03-06-20, 18:48 第 3 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

精彩!!~
不过,我认为用shell一样可以作得到的!可惜,我还没写出来呢!:(
LY兄的perl水平的进步真快呀~佩服

此帖于 03-06-20 19:05 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-06-20, 22:05 第 4 帖
lucida
 
 
 
注册会员  
  注册日期: Oct 2002
  我的住址: .:DRL:.
  帖子: 2,492
  精华: 9
 

标题: bash版本


代码:
#!/bin/bash #penny_ccf@hotmail.com #it's ugly, but it works :) cconvert(){ declare -a cnum; declare -a cmag; cnum[1]="壹" cnum[2]="贰" cnum[3]="叁" cnum[4]="肆" cnum[5]="伍" cnum[6]="陆" cnum[7]="柒" cnum[8]="拔" cnum[9]="玖" cnum[0]="零" cmag[0]="" cmag[1]="拾" cmag[2]="佰" cmag[3]="仟" cmag[4]="万" cmag[5]="拾" cmag[6]="百" cmag[7]="千" 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[0]}; 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';
$ ./convert.sh 1000222299999999999000000000099990000000002222200000
壹仟亿亿亿亿亿亿贰千贰百贰拾贰万玖仟玖佰玖拾玖亿亿亿亿亿玖千玖百玖拾玖万玖仟玖佰玖拾亿亿亿亿零亿亿亿零玖百玖拾玖万玖仟亿亿零贰拾贰亿贰千贰百贰拾万


转换任意长度
用到了bash的function和array,目前看起来还能用,不过写的很不好看

感谢LYOO兄的建议,改了两处bug,请继续测试

此帖于 03-06-20 23:30 被 lucida 编辑.
  lucida 当前离线   回复时引用此帖
旧 03-06-20, 22:30 第 5 帖
LYOO
 
LYOO 的头像
 
 
注册会员  
  注册日期: Jan 2003
  帖子: 782
  精华: 37
 

妙,penny的脚本短小精悍!

不过超过一千万后的读数还是有问题:

lyoo@lsd:~$ ./pennyconvert.sh 10000000
壹壹千万
lyoo@lsd:~$ ./pennyconvert.sh 100000000
壹亿零万
lyoo@lsd:~$ ./pennyconvert.sh 1000000000
壹拾亿零万
lyoo@lsd:~$ ./pennyconvert.sh 10000000001
壹佰亿零万零壹
  LYOO 当前离线   回复时引用此帖
旧 03-06-20, 22:36 第 6 帖
lucida
 
 
 
注册会员  
  注册日期: Oct 2002
  我的住址: .:DRL:.
  帖子: 2,492
  精华: 9
 

呵呵,我在22:24分编辑了这个帖子
你在22:30发贴

你再copy一次?第一个壹壹千万还是有问题,后面几个倒是没问题了

这程序实在是写的难看,最后一堆sed修修补补,看来还是有漏掉的。。^_^

edit1: lyoo兄提出的bug都改掉了,又发现新bug。。调试中...







__________________
E6300@3.2G/P5B-D WiFi/2G RAM/1TB HDD/3540A/7900GT/E-MU 0404
Logitech S 510/MX Revolution/2407WFP/LaserJet 1020
go wild, go Gentoo

此帖于 03-06-20 22:54 被 lucida 编辑.
  lucida 当前离线   回复时引用此帖
旧 03-06-20, 23:13 第 7 帖
LYOO
 
LYOO 的头像
 
 
注册会员  
  注册日期: Jan 2003
  帖子: 782
  精华: 37
 

呵呵,penny效率神速呀
引用:
这程序实在是写的难看,最后一堆sed修修补补,看来还是有漏掉的。。^_^
我也是一样,狂用s///g
我的脚本有个毛病就是,超过万亿后,万亿和亿之间单位有重复,调试中...
看看这个1111111111100000,咱们俩的脚本都有毛病:
lyoo@lsd:~$ ./lyoo_convert.pl
Please input a number:1111111111100000
壹仟壹佰壹拾壹万亿壹仟壹佰壹拾壹亿壹仟壹佰壹拾万
lyoo@lsd:~$ ./penny_convert.sh 1111111111100000
贰壹千壹百壹拾壹万壹仟壹佰壹拾壹亿壹千壹百壹拾万
  LYOO 当前离线   回复时引用此帖
旧 03-06-20, 23:20 第 8 帖
LYOO
 
LYOO 的头像
 
 
注册会员  
  注册日期: Jan 2003
  帖子: 782
  精华: 37
 

哈哈,又升级了,嗯,试试这个10000000000000000001 。
  LYOO 当前离线   回复时引用此帖
旧 03-06-20, 23:23 第 9 帖
lucida
 
 
 
注册会员  
  注册日期: Oct 2002
  我的住址: .:DRL:.
  帖子: 2,492
  精华: 9
 

^_^,一分钟以前又升级了一次

$./convert.sh 10000000000000000001
壹仟亿亿零壹

./convert.sh 1111111111100000
壹千壹百壹拾壹万壹仟壹佰壹拾壹亿壹千壹百壹拾万

至少这两个都没问题了

此帖于 03-06-20 23:27 被 lucida 编辑.
  lucida 当前离线   回复时引用此帖
旧 03-06-20, 23:51 第 10 帖
LYOO
 
LYOO 的头像
 
 
注册会员  
  注册日期: Jan 2003
  帖子: 782
  精华: 37
 

怪,我重新从上面拷贝了你的代码,可执行起来好象没什么改变。
...我还是先读读你的代码,希望能借点好东西,补补我的脚本
  LYOO 当前离线   回复时引用此帖
旧 03-06-21, 00:48 第 11 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

I 服了 YOU们 !!!
我已经将两位的大作置顶于[脚本欣赏]区!特此通知

此帖于 03-06-21 01:05 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-06-21, 01:08 第 12 帖
pupilzeng 帅哥
 
 
 
注册会员  
  注册日期: Jun 2002
  我的住址: 云游四海
  帖子: 2,280
  精华: 11
 

牛人啦







__________________
Simplicity is beauty!
  pupilzeng 当前离线   回复时引用此帖
旧 03-06-21, 21:49 第 13 帖
LYOO
 
LYOO 的头像
 
 
注册会员  
  注册日期: Jan 2003
  帖子: 782
  精华: 37
 

读了一下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 "Please 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的想法,新的脚本可以读取正负数及小数了。

此帖于 03-06-22 12:20 被 LYOO 编辑.
  LYOO 当前离线   回复时引用此帖
旧 03-06-22, 01:57 第 14 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

引用:
牛人啦
  KornLee 当前离线   回复时引用此帖
旧 03-06-22, 04:22 第 15 帖
hzqe
 
 
 
注册会员  
  注册日期: Sep 2002
  帖子: 53
  精华: 1
 

晕ing!!!
  hzqe 当前离线   回复时引用此帖
发表新主题 回复


主题工具

发帖规则
您 [不可以] 发表新主题
您 [不可以] 回复主题
您 [不可以] 上传附件
您 [不可以] 编辑您的帖子

已 [启用] BB 代码
已 [启用] 表情符号
已 [启用] IMG 代码
已 [禁用] HTML 代码
[论坛跳转…]


所有时间均为[北京时间]。现在的时间是 12:34


Powered by vBulletin 版本 3.6.8
版权所有 ©2000 - 2010, Jelsoft Enterprises Ltd.
官方中文技术支持: vBulletin 中文
版权所有 ©2002 - 2009, LinuxSir.Org