Perl Programming/Exercise 5 Answers
Appearance
Answers
[edit | edit source]Answer 1 (entire script):
#!/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;
};