#!/usr/bin/env perl use utf8; ############################################################################### # This program computes NFL passer ratings. # # Copyright © 2020 Richard Lesh. All rights reserved. ############################################################################### use Error::Simple; use Nice::Try; use Utils; use strict; use warnings; sub passer_rating_nfl { my ($attempts, $completions, $touchdown_passes, $interceptions, $passing_yards) = @_; my $a = ($completions / $attempts - 0.3) * 5.; my $b = ($passing_yards / $attempts - 3.) * 0.25; my $c = $touchdown_passes / $attempts * 20.; my $d = 2.375 - ($interceptions / $attempts * 25.); my $RATING = ($a + $b + $c + $d) / 6. * 100.; return $RATING; } sub passer_rating_ncaa { my ($attempts, $completions, $touchdown_passes, $interceptions, $passing_yards) = @_; my $a = 8.4 * $passing_yards; my $b = 330. * $touchdown_passes; my $c = 100. * $completions; my $d = 200. * $interceptions; my $RATING = ($a + $b + $c - $d) / $attempts; return $RATING; } MAIN: { binmode(STDOUT, ":utf8"); binmode(STDERR, ":utf8"); binmode(STDIN, ":utf8"); try { my $input_str; my $attempts; $input_str = Utils::prompt("Number of attempts: "); $attempts = Utils::stoi($input_str); my $completions; $input_str = Utils::prompt("Number of completions: "); $completions = Utils::stoi($input_str); my $touchdown_passes; $input_str = Utils::prompt("Number of touchdown passes: "); $touchdown_passes = Utils::stoi($input_str); my $interceptions; $input_str = Utils::prompt("Number of interceptions: "); $interceptions = Utils::stoi($input_str); my $passing_yards; $input_str = Utils::prompt("Number of passing yards: "); $passing_yards = Utils::stoi($input_str); my $which; $input_str = Utils::prompt("1 for NFL or 2 for NCAA: "); $which = Utils::stoi($input_str); if ($which == 1) { my $RATING = passer_rating_nfl($attempts, $completions, $touchdown_passes, $interceptions, $passing_yards); print Utils::messageFormat("NFL Passer rating: \{0:.1f\}", $RATING), "\n"; } else { my $RATING = passer_rating_ncaa($attempts, $completions, $touchdown_passes, $interceptions, $passing_yards); print Utils::messageFormat("NCAA Passer rating: \{0:.1f\}", $RATING), "\n"; } } catch (Error::NumberFormatException $ex) { print "Bad input! " . $ex, "\n"; } catch (Error::Simple $ex) { print "Something went wrong! " . $ex, "\n"; } }