|
发表于 2004-3-9 23:48:07
|
显示全部楼层
我的,大概测试了一下,还行,不保证没有错误
- #! /usr/bin/perl -w
- while(2){
- if(!defined($begin = getInput("begin"))){
- next;
- }
- if(!defined($end = getInput("end"))){
- next;
- }
- if(!defined($step = getInput("step"))){
- next;
- }
- if($begin > -459.69 && $begin < $end && $step > 0){
- last;
- }else{
- print "error\n";
- }
- }
- print " No. Fahrenheit Celsius\n";
- for($i = 1, $f = $begin; $f <= $end; $f += $step, $i++){
- $c = ($f - 32) / 1.8;
- write;
- }
- sub getInput{
- my $name = $_[0];
- print "${name}: ";
- my $num = <>;
- if(!defined($num)){ [color=red]# Ctrl+d 退出[/color]
- print "\n";
- exit;
- }
- chomp $num;
- if(!isNumber($num)){
- print "error\n";
- undef $num;
- }
- return $num;
- }
- sub isNumber{
- my $n = $_[0];
- if($n =~ /^[+-.]+$/){ [color=red]# 如果只包含+-.则返回假[/color]
- return 0;
- }
- [color=red]# 判断是否是数字,可接受的形式有-2、.3、-.6、3.7、+50等等[/color]
- if($n =~ /^[+-]?\d*\.?\d*$/){
- return 1;
- }
- return 0;
- }
- format STDOUT=
- @#### @######.## @####.##
- $i, $f, $c
- .
复制代码 |
|