veket官方网站论坛

veket

 找回密码
 要注册
搜索
查看: 2001|回复: 1

将数字转换为大写数字的脚本

[复制链接]
发表于 2010-4-21 22:45 | 显示全部楼层 |阅读模式
在linuxsir看到的二个脚本,转出来学习下:
原作者为:lucida
bash
  1. #!/bin/bash
  2. #penny_ccf@hotmail.com

  3. #it's ugly, but it works :)


  4. cconvert(){

  5. declare -a cnum;
  6. declare -a cmag;

  7. cnum[1]="壹"
  8. cnum[2]="贰"
  9. cnum[3]="叁"
  10. cnum[4]="肆"
  11. cnum[5]="伍"
  12. cnum[6]="陆"
  13. cnum[7]="柒"
  14. cnum[8]="拔"
  15. cnum[9]="玖"
  16. cnum[0]="零"

  17. cmag[0]=""
  18. cmag[1]="拾"
  19. cmag[2]="佰"
  20. cmag[3]="仟"
  21. cmag[4]="万"
  22. cmag[5]="拾"
  23. cmag[6]="百"
  24. cmag[7]="千"


  25. tempalpha="$1";
  26. ctempmag=$2;
  27. if [ $tempalpha == "00000000" ] ; then
  28. CSTR="";
  29. return 0;
  30. fi
  31. let templength="${#tempalpha}";

  32. CSTR="";
  33. for ((m=0;m<templength;m++))
  34. do
  35. tempi=${tempalpha:m:1};
  36. let tempj="$templength-$m-1";

  37. if ((( tempi == 0 )) && (( tempj ==4 ))); then
  38. CSTR=$CSTR"万";
  39. elif (( tempi == 0 )); then
  40. CSTR=$CSTR${cnum[0]};
  41. else
  42. CSTR=$CSTR${cnum[$tempi]}${cmag[$tempj]};
  43. fi

  44. done

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

  51. CSTR=$CSTR$CMAG;
  52. }

  53. alpha=$1;
  54. length=${#alpha};
  55. let k="$length/8";
  56. let modl="$length%8";
  57. MYSTR="";

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

  60. MYSTR=$MYSTR$CSTR;
  61. for ((i=0;i<k;i++))
  62. do
  63.   let pos="$i*8+modl";
  64.   tempstr=${alpha:$pos:8};
  65.   let tempmag="$k-$i-1";
  66.   cconvert $tempstr $tempmag;
  67.   MYSTR=$MYSTR$CSTR;
  68. done  

  69. echo $MYSTR | sed -e 's/亿零万/亿零/g'  -e 's/零万/万/g' -e 's/零亿/亿/g' -e 's/零零*/零/g' -e 's/零$//g';
复制代码
 楼主| 发表于 2010-4-21 22:46 | 显示全部楼层
原作者为:LYOO
perl脚本:
  1. #!/usr/bin/perl -w

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


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

  8. ############################################################################
  9. #
  10. # receive user's input
  11. #
  12. ############################################################################

  13. $count = 0;
  14. while ( $count < 1 ) {
  15. print "Please input a number:";
  16. chomp ($number = <STDIN>);
  17. if ( $number =~ /^[-,+]?\d+\.?\d*$/ ) {
  18. $count += 1;
  19. } else {
  20. print "It's not a Number!\n";
  21. redo;
  22. }
  23. }

  24. ############################################################################
  25. #
  26. # create a number_array
  27. #
  28. ############################################################################

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

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

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

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

  47. # get the number_dot_string.
  48. $number_dot_string = "";
  49. if ($dot eq "yes") {
  50. while (@number_array) {
  51. $number_dot_string .= shift @number_array;
  52. last if $number_dot_string =~ /\d\./;
  53. };
  54. $number_dot_string = reverse $number_dot_string;
  55. };


  56. #############################################################################
  57. #
  58. # creat a number_unit_array
  59. #
  60. #############################################################################

  61. $min_unit = 9;
  62. $j = 0;
  63. $i = 0;
  64. $n = 0;

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

  67. if ($i == 0) {
  68. $j++;
  69. $min_unit = "on";
  70. $switch = "on"
  71. };

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

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

  79. $i++;
  80. $n++;
  81. $i = $i % 8;
  82. }

  83. #############################################################################
  84. #
  85. # modify the number_unit_string
  86. #
  87. #############################################################################

  88. foreach (@number_unit_array) {
  89. $number_unit_string .= $_;
  90. }
  91. $number_unit_string = reverse $number_unit_string;
  92. $_ = $number_unit_string;
  93. s/0[A-H]/0/g;
  94. s/0+/0/g;
  95. s/A//g;
  96. s/0+$//;

  97. #print "$_\n";

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

  99. s/H(\d)G(\d)F/D$1C$2F/g;
  100. s/H(\d)G(\d)E/D$1C$2E/g;
  101. s/H(\d)F(\d)E/D$1B$2E/g;
  102. s/G(\d)F(\d)E/C$1B$2E/g;


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

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

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

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

  110. #############################################################################
  111. #
  112. # output the number_unit_string as a array
  113. #
  114. #############################################################################

  115. # convert number_unit_string to array.
  116. # it's ugly but without this action
  117. # chinese can't output correct.
  118. # I don't know why :(

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

  126. foreach (@number_unit_ok) {
  127. &print_chinese;
  128. }
  129. print "\n";

  130. sub print_chinese {
  131. if ($_ eq 0) {
  132. print "零";
  133. } elsif ($_ eq 1) {
  134. print "壹";
  135. } elsif ($_ eq 2) {
  136. print "贰";
  137. } elsif ($_ eq 3) {
  138. print "叁";
  139. } elsif ($_ eq 4) {
  140. print "肆";
  141. } elsif ($_ eq 5) {
  142. print "伍";
  143. } elsif ($_ eq 6) {
  144. print "陆";
  145. } elsif ($_ eq 7) {
  146. print "柒";
  147. } elsif ($_ eq 8) {
  148. print "捌";
  149. } elsif ($_ eq 9) {
  150. print "玖";
  151. } elsif ($_ eq A) {
  152. print "个";
  153. } elsif ($_ eq B) {
  154. print "拾";
  155. } elsif ($_ eq C) {
  156. print "佰";
  157. } elsif ($_ eq D) {
  158. print "仟";
  159. } elsif ($_ eq E) {
  160. print "万";
  161. } elsif ($_ eq F) {
  162. print "拾万";
  163. } elsif ($_ eq G) {
  164. print "佰万";
  165. } elsif ($_ eq H) {
  166. print "仟万";
  167. } elsif ($_ eq Z) {
  168. print "亿";
  169. } elsif ($_ eq "+") {
  170. print "<正>";
  171. } elsif ($_ eq "-") {
  172. print "<负>";
  173. } elsif ($_ eq ".") {
  174. print "<点>";
  175. }
  176. }

  177. #############################################################################
  178. # the end of this script
  179. ##############################################################################
复制代码
您需要登录后才可以回帖 登录 | 要注册

本版积分规则

QQ|手机版|Archiver|veket官方网站论坛 ( 粤ICP备 11052856 )

GMT+8, 2024-5-5 13:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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