我需要创建一个伪代码,它会以24小时格式提示时间,将其转换为12小时格式,以及它是AM还是PM。它必须重复,直到输入9999的哨兵时间。以下是我发布的内容,是否正确?
输入此循环:
DOWHILE (time != 9999)
IF(时间<0000或时间> 2400或(时间%100> = 60))
Display 'Invalid time'
ELSE
IF(时间&gt; = 1200和时间&lt; = 2400)
format = 'pm'
hours = (time/100) - 12
mins = time%100
Display 'The new time is ', hours, '.', mins, format
ELSE format ='am'
IF (time/100 = 00 )
hours = (time/100) + 12
mins = time%100
Display 'The time is ', hours, '.', mins, format
ELSE
hours = time/100
mins = time%100
Display ' 'The time is ', hours, '.', mins, format
ENDIF
ENDIF
ENDIF
ENDO
答案 0 :(得分:0)
Perl:
#!/usr/bin/env perl
use strict;
use warnings;
sub time_12h_to_24h
{
my($t12) = @_;
my($hh,$mm,$ampm) = $t12 =~ m/^(\d\d?):(\d\d?)\s*([AP]M?)/i;
$hh = ($hh % 12) + (($ampm =~ m/AM?/i) ? 0 : 12);
return sprintf("%.2d:%.2d", $hh, $mm);
}
sub time_24h_to_12h
{
my($t24) = @_;
my($hh,$mm) = $t24 =~ m/^(\d\d?):(\d\d?)/;
my($ampm) = ($hh <12) ? "am" : "pm";
$hh %= 12;
$hh += 12 if $hh == 0;
return sprintf("%d:%.2d %s", $hh, $mm, $ampm);
}
while (<>)
{
chomp;
my($input, $entry) = split / - /;
my $time_24h = time_12h_to_24h($input);
my $time_12h = time_24h_to_12h($time_24h);
print "$input - $time_24h : $time_12h - $entry\n";
#printf "%8s - %5s : %8s - %s\n", $input, $time_24h, $time_12h, $entry;
}
示例数据:
12:00 AM - 0000
12:01 AM - 0001
12:59 AM - 0059
01:00 AM - 0100
11:00 AM - 1100
11:59 AM - 1159
12:00 PM - 1200
12:59 PM - 1259
01:00 PM - 1300
11:59 PM - 2359
12:00 AM - 2400
示例输出:
12:00 AM - 00:00 : 12:00 am - 0000
12:01 AM - 00:01 : 12:01 am - 0001
12:59 AM - 00:59 : 12:59 am - 0059
01:00 AM - 01:00 : 1:00 am - 0100
11:00 AM - 11:00 : 11:00 am - 1100
11:59 AM - 11:59 : 11:59 am - 1159
12:00 PM - 12:00 : 12:00 pm - 1200
12:59 PM - 12:59 : 12:59 pm - 1259
01:00 PM - 13:00 : 1:00 pm - 1300
11:59 PM - 23:59 : 11:59 pm - 2359
12:00 AM - 00:00 : 12:00 am - 2400