#!/usr/bin/perl -w use strict; my %ohmsLaw; #i,v,r print "This is an Ohm's law program (V=I*R).\n", "enter statements like: \n", " Enter I,V,R equation> i=2.3 or 3.2 mA or 3.2 ma\n", " Enter I,V,R equation> R=4\n", " Enter I,V,R equation> v=? **asks for v to be printed**\n", " prints: v=9.2 ** 9.2= (2.3 x 4) **\n", "basically I calculate the value that you ask for\n", " based upon the previous entries for the other two\n", " variables\n", "enter q|Q|quit to quit program\n"; my $line; while( (print "Enter I,V,R equation>"), ($line=), $line !~ /^\s*q(uit)?\s*$/i) { next if $line =~ /^\s*$/; # simple re-prompt on blank line # # set an I,V,R variable... # if (my ($vir_letter, $number, $flag_ma) = $line =~ /^\s*([ivrIVR])\s*\=\s*([-+]?\d*\.?\d+)\s*(ma|mA)?\s*$/) { if ($vir_letter =~ /I/i) { $ohmsLaw{i} = $number; $ohmsLaw{i} /= 1000 if defined $flag_ma; } $ohmsLaw{v}=$number if $vir_letter =~ /V/i; $ohmsLaw{r}=$number if $vir_letter =~ /R/i; } # # output a calculated I,V,R value # elsif (my ($printValue) = $line =~ /\s*(i|v|r)\s*\=\s*\?\s*$/i) { foreach my $ltr (grep{$_ ne $printValue} keys %ohmsLaw) { print " $ltr=$ohmsLaw{$ltr}\n"; #list other values! } print "v=",$ohmsLaw{v}=$ohmsLaw{i}*$ohmsLaw{r},"\n" if $printValue =~ /V/i; print "i=",$ohmsLaw{i}=$ohmsLaw{v}/$ohmsLaw{r},"\n" if $printValue =~ /I/i; print "r=",$ohmsLaw{r}=$ohmsLaw{v}/$ohmsLaw{i},"\n" if $printValue =~ /R/i; } else { print "illegal syntax!\n"; } }