Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

A Gear Inch Calculator for Cyclists

by harley_frog (Novice)
on Aug 12, 2003 at 13:17 UTC ( #283169=CUFP: print w/ replies, xml ) Need Help??

Last night, I was doing the exercises in Chapter 2 of The Llama Book and decided to try some "extra credit" coding when I came up with this idea. I used to do a lot of bicycling when I was in college and currently looking to buying a recumbent. With anywhere from 24 to 27 speeds, it's nice to know what the gear inches are for each speed. A gear inch, by the way, is a calculation based on the gear ratios and the size of the drive wheel. It described how "big" your drive wheel is (and in this case bigger means faster). I am hoping to modify the code later to give the entire range of gears, but that's for Chapter 3. ;)

Thoughts, opinions, hints, etc. gladly welcomed.

Frog

#!/usr/bin/perl -w print "Enter the number of teeth on the chainring: "; chomp ($chainring = <STDIN>); print "Enter the number of teeth on the cog: "; chomp ($cog = <STDIN>); if ($cog <= 0) { print "Error: The gear inches cannot be calculated using $cog as a c +og value. Please enter another value."; } else { print "Is your rear wheel metric? (y/n): "; chomp ($_ = <STDIN>); if ($_ eq "y") { print "Enter your metric wheel size in millimeters: "; chomp ($metric = <STDIN>); $wheel = $metric * .039; $gear_inches = ($chainring / $cog) * $wheel; print "The gear inches for a chainring of $chainring teeth and a c +og of $cog teeth and a wheel of $metric millimeters is $gear_inches g +ear inches.\n"; } else { print "Enter your wheel size in inches: "; chomp ($wheel = <STDIN>); $gear_inches = ($chainring / $cog) * $wheel; print "The gear inches for a chainring of $chainring teeth and a c +og of $cog teeth for a wheel $wheel inches in diameter is $gear_inche +s gear inches.\n"; } }

Comment on A Gear Inch Calculator for Cyclists
Download Code
Re: A Gear Inch Calculator for Cyclists
by Abigail-II (Bishop) on Aug 12, 2003 at 13:59 UTC
    Why do beginners programs have the tendency to be overly chatty? Your program prints a line, and read something from input for each chainwheel, whether or not the wheels are metric, and for the size of the wheel.

    Could you imagine what life would be if every program acted that way? Just imagine a 'tar' asking you for each option whether you want to include that, and each file to be included to be typed in separatedly? There's a reason many programs take command line arguments, and that programs that read from stdin and write to stdout are often called filters. Programs like yours are much harder to chain together.

    I would also perform some form of input checking, and round of the final value.

    #!/usr/bin/perl use strict; use warnings; die "Usage: $0 <chainring> <cog> <wheelsize>\n" unless @ARGV == 3; my ($chainring, $cog, $wheelsize) = @ARGV; $wheelsize = $1 * .39 if $wheelsize =~ /^(\d+)m$/; die "Chainring needs to be a positive integer\n" unless $chainring =~ +/^\d+$/; die "Cog needs to be a positive integer\n" unless $cog =~ /^\d+$ +/; die "Wheelsize needs to be a positive number\n" unless $wheelsize =~ /^\d+(?:[.]\d*)?$/; my $gearinches = sprintf "%.2f" => $wheelsize * $chainring / $cog; print <<"--"; The gear inches for a chainring of $chainring teeth and a cog of $cog teeth, and a wheel of $wheelsize inches is $gearinches gear inches. -- __END__

    Abigail

      Why do beginners programs have the tendency to be overly chatty?

      I think it has a lot to do with insecurity. Beginners want to be continually assured that the program is actually doing what they think they told the program to do.

      Once you are more experienced, you are self assured about your programming capabilities, and you "know" that the program is doing what you told it to do. So you don't need the continuous assurances anymore.

      Liz

Re: A Gear Inch Calculator for Cyclists
by CountZero (Bishop) on Aug 13, 2003 at 06:19 UTC

    To cater for a user who types 'Y' instead of 'y', it is considered better to normalize the user input to the same case as your test (unless you want the program to react differently between 'Y' and 'y'):

    print "Is your rear wheel metric? (y/n): "; chomp ($_ = <STDIN>); if (lc($_) eq "y")

    As lc by default works on $_, you could use lc instead of lc($_), but that is perhaps unnecessarily obfuscated (Are there necessary obfuscations?)

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://283169]
Approved by phydeauxarff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (18)
As of 2014-08-27 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (252 votes), past polls