Ohm's law calculator

For PC type software that runs under some PC opsys.

Ohm's law calculator

Postby Doug Coulter » Sun Dec 15, 2013 3:11 pm

Also posted under forumlas and data constants - since that's kind of what this is/does. Just answers Ohm's law questions.
Well, this isn't the purty version - it's text-only, cross platform (tested on linux) and in perl. I want to someday make a fancier GUI version that lets you just fill in the fields you know and then it fills in the unknown, but haven't bothered so far (some of the UI design elements are sort of challenging if I don't make you click a lot).

So here it is as perl code. Note, it looks a whole lot better looking at it in something that knows how to color-highlight perl code, like gedit, padre, geany...tons of other editors. But for inclusion on the board, at least for short stuff like this - it seems better to just let you cut/paste out of a "code" block into the editor of your choice, put it somewhere on your path, and mark it as executable so you can just type whatever you named it. I just called it "ohms" here. In windows, you might need to add .pl or something to make windows know it's a perl executable. Yes, there is perl everywhere (even android, sort of). You can get it for windows via getting padre which installs DWIM or strawberry perl along with itself - free. That first funny line is how we tell linux where to find the perl interpreter - windows will likely just ignore that. THIS purports to be a link to a windows-32 version of gedit, my editor of choice here on linux - YMMV, I've not tested it yet. It's for a much older version than I use, anyway - an entire major rev back (which if I recall correctly, actually wasn't that shabby). But padre is also just fine for perl - it's just that gedit works for them ALL. If you care about that, you're probably already running linux and know it anyway.

Code: Select all
#!/usr/bin/perl -w
use strict;
use Scalar::Util qw(looks_like_number); # this is a real pain otherwise..
# Extremely dumb, but I've wanted one for years.  I agonized over whether
# (and how to) make a GUI that would just let you type in all but one value,
# and then it would supply the unknown. I'll get that round-tuit someday.
# Till then, there's this.  Just tell it which is the unknown first, it'll
# ask for the rest and give an answer - including your inputs (validation).

#********************************* variables *********************************
my ($E,$I,$R,$unk); # the obvious variables we need, global due to my lazy
#*************************************** subroutine(s) ***********************
# Sub to ask user for and return a numeric value - pass in the "name"
# of what you want to ask for.
# Keeps asking till you put in some sort of valid numeric
sub getVal($)
{
do
{
  print "Input the $_[0] ->"; #Trick, kinda. saves a line and a variable
} until looks_like_number($_ = <>); # Seems to be a good test
chomp; # Remove newline...causes issues later
return $_;
}
############################ main ############################################
print "Ohm's law calculator - E = IR \nGive us which you want to calculate,
we'll ask for the rest and do it.\n";
do {
print "\nWhat's the unknown?  E,I,R? (E) ->";
$unk = lc (<>); # Make input case-insensitive
} until $unk =~ m/e|i|r/; # Try till valid unknown type

# OK, now compute the unknown, after asking for the knowns
$E = getVal("Volts")   unless $unk =~ m/e/i;
$I = getVal("Amps")   unless $unk =~ m/i/i;
$R = getVal("Ohms")   unless $unk =~ m/r/i;

# Older perls have no switch statement, so this'll do here
if ($unk =~ /e/) {$E = $I * $R;} # the most basic version
elsif ($unk =~ /i/) {$I = $E/$R;} # compute amps from volts and ohms
elsif ($unk =~ /r/) {$R = $E/$I;} # compute current from volts and resistance
print "Answer: Volts = $E, Amps = $I, Ohms = $R\n"; # we're done
##############################################################################

Ohms.png
Syntax highlighted

See how much nicer it looks in a real editor? In this case, gedit which is simply a smarter version of notepad.


And since this really is a code forum, here it is zipped for those who are actually cool enough to use things like this.
ohms.zip
zip of linux version
(1.05 KiB) Downloaded 265 times


I left in a "bug" for the education of any new perl programmers. By convention, asking for the unknown with that (E) in the ask line means E is the default, but I never implemented that feature. It's actually pretty easy if you've not already posted multiple copies around the place. Just chomp $unk, and if there's nothing left - make it equal 'e' before proceeding. I'd bet even a real beginner doesn't have to crack a book or look at a manpage to figure out how to do that, I just forgot.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: Ohm's law calculator

Postby Doug Coulter » Mon Dec 16, 2013 8:07 am

Here is a slightly niced-up version of the above. I realized I didn't need case in-sensitivity on my pattern matches, having lower-cased the input from the get-go, and that one comment was simply wrong (happens when you use comments and change code). And I've now checked it on windows 7 (32) under DWIM/strawberry perl and Padre - works as intended there too. I left in the "default bug"...because solving it is "interesting". One way to "fix" that is simply not to declare that there is one...

Code: Select all
#!/usr/bin/perl -w
use strict; # make perl complain more about my sloppiness - more than -w
use Scalar::Util qw(looks_like_number); # this is a real pain otherwise..
# Extremely dumb, but I've wanted one for years.  I agonized over whether
# (and how to) make a GUI that would just let you type in all but one value,
# and then it would supply the unknown. I'll get that round-tuit someday.
# Till then, there's this.  Just tell it which is the unknown first, it'll
# ask for the rest and give an answer - including your inputs (validation).

#********************************* variables *********************************
my ($E,$I,$R,$unk); # the obvious variables we need, global due to my lazy
#*************************************** subroutine(s) ***********************
# Sub to ask user for and return a numeric value - pass in the "name"
# of what you want to ask for.
# Keeps asking till you put in some sort of valid numeric
sub getVal($)
{
do
{
  print "Input the $_[0] ->"; #Trick, kinda. saves a line and a variable
} until looks_like_number($_ = <>); # Seems to be a good test
chomp; # Remove newline...causes issues later
return $_;
}
############################ main ############################################
print "Ohm's law calculator - E = IR \nGive us which you want to calculate,
we'll ask for the rest and do it.\n";
do {
print "\nWhat's the unknown?  E,I,R? (E) ->";
$unk = lc (<>); # Make input case-insensitive
} until $unk =~ m/e|i|r/; # Try till valid unknown type

# OK, ask for the known values
$E = getVal("Volts")   unless $unk =~ m/e/;
$I = getVal("Amps")   unless $unk =~ m/i/;
$R = getVal("Ohms")   unless $unk =~ m/r/;

#Compute unknown
# Older perls have no switch statement, so this'll do here
if ($unk =~ /e/) {$E = $I * $R;} # the most basic version
elsif ($unk =~ /i/) {$I = $E/$R;} # compute amps from volts and ohms
elsif ($unk =~ /r/) {$R = $E/$I;} # compute resistance from volts and amps
print "Answer: Volts = $E, Amps = $I, Ohms = $R\n"; # we're done
##############################################################################
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA


Return to PC

Who is online

Users browsing this forum: No registered users and 1 guest

cron