Mastering Regular Expression 代碼介紹 - 攝氏度 華氏度 轉換

#! /usr/bin/perl

# 輸入格式 32F 華氏度,100C 攝氏度 ,注意字母爲大寫


print "Enter a temperature (e.g., 32F, 100C):\n";  
$input = <STDIN>; # This reads one line from the user.
chomp($input); # This removes the ending newline from $input.
if ($input =~ m/^([-+]?[0-9]+)([CF])$/)
{
    # If we get in here, we had a match. $1 is the number, $2 is "C" or "F".
    $InputNum = $1; # Save to named variables to make the ...
    $type = $2; # ... rest of the program easier to read.
    if ($type eq "C") {  # `eq' tests if two strings are equal
        # The input was Celsius, so calculate Fahrenheit
        $celsius = $InputNum;
        $fahrenheit = ($celsius × 9 / 5) + 32;
    } else {
        # If not "C", it must be an "F", so calculate Celsius
        $fahrenheit = $InputNum;
        $celsius = ($fahrenheit - 32) × 5 / 9;
    }
    # At this point we have both temperatures, so display the results:
    printf "%.2f C is %.2f F\n", $celsius, $fahrenheit;
} else {
    # The initial regex did not match, so issue a warning.
    print "Expecting a number followed by \"C\" or \"F\",\n";
    print "so I don't understand \"$input\".\n";
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章