Temperature Conversion

By Ziddykins on May 15, 2014

More programming challenge stuff.
Converts temperatures in 6 different ways.

#!/usr/bin/perl
use warnings; use strict;

print "1 - Fahrenheit to Celcius\n2 - Celcius to Fahrenheit\n" .
      "3 - Celcius to Kelvin\n4 - Kelvin to Celcius\n" .
      "5 - Fahrenheit to Kelvin\n6 - Kelvin to Fahrenheit\nChoice: ";

chomp(my $choice = <STDIN>);
print "Temperature: ";
chomp(my $temp = <STDIN>);
$temp =~ s/[^0-9]+//g;

if ($choice eq 1) { $temp = ftc($temp); } elsif
   ($choice eq 2) { $temp = ctf($temp); } elsif
   ($choice eq 3) { $temp = ctk($temp); } elsif
   ($choice eq 4) { $temp = ktc($temp); } elsif
   ($choice eq 5) { $temp = ftk($temp); } elsif
   ($choice eq 6) { $temp = ktf($temp); } else {
   print "Invalid choice\n";
}

print "Result: $temp\n";

sub ftc {
    my $temp = $_[0];
    my $rvalue = (($temp - 32) * 5) / 9;
    return $rvalue;
}

sub ctf {
    my $temp = $_[0];
    my $rvalue = (($temp * 9) / 5) + 32;
    return $rvalue;
}

sub ctk {
    my $temp = $_[0];
    my $rvalue = ($temp + 273.15);
    return $rvalue;
}

sub ktc {
    my $temp = $_[0];
    my $rvalue = ($temp - 273.15);
    return $rvalue;
}

sub ftk {
    my $temp = $_[0];
    $temp = ftc($temp);
    $temp = ctk($temp);
    return $temp;
}

sub ktf {
    my $temp = $_[0];
    $temp = ktc($temp);
    $temp = ctf($temp);
    return $temp;
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.