跳至内容

Perl 编程/练习 5 答案

来自维基文库,开放世界的开放书籍

答案 1(整个脚本)

#!/usr/bin/perl
#Author:(LTW)
#18/06/15
#Temperature Converter

use strict;
use warnings;

print "What temperature would you like to convert?\n";
my $input = readline STDIN;
chomp($input);
my $var = substr($input, length($input)-1);
if($var eq "F"){
	print $input , " is equivelent to ", int(convert_to_celsius($input)), "C\n",
	(convert_to_celsius($input) < 10 ? "That's cold!" : 
	convert_to_celsius($input) > 30 ? "That's really hot!" : "That's ok!"), "\n";
}
elsif($var eq "C"){
	print $input , " is equivelent to ", int(convert_to_fahrenheit($input)), "F\n",
	(convert_to_fahrenheit($input) < 41 ? "That's cold!" : 
		convert_to_fahrenheit($input) > 90 ? "That's really hot!" : "That's ok!"), "\n";
}
else{
	print "Invalid temperature conversion request.\n";
};
sub convert_to_celsius {
	return (substr($input, 0, -1) - 32) * 0.555555556;
};
sub convert_to_fahrenheit{
	return (substr($input, 0, -1)*9)/5+32;
};
华夏公益教科书