You may find this an interesting starting point:
use strict; use warnings; my $x = "3 _ 4"; #in polar form.. 3 is mag and 4 is angle my $y = "1i2"; #in rectangular form print join ", ", constructComplex ($x); print "\n" . join ", ", constructComplex ($y); sub constructComplex { my $value1 = shift; my @construct; my ($a, $Form, $b) = $value1 =~ /([-+e.\d]+)\s*([i_])\s*([-+e.\d]+)/; return undef if ! defined $b; $value1 = $_[0]; if ($Form eq '_') { #uses $value1 to pick out the magnitude and angle my ($magnitude, $angle) = ($a, $b); $construct[2] = $magnitude; $construct[3] = $angle; # Do da polar to rect conversion } else { my ($real, $complex) = ($a, $b); $construct[0] = $real; $construct[1] = $complex; # Do da rect to polar conversion } return @construct; }
Ignoring undef related warnings it prints:
, , 3, 4 1, 2
In reply to Re: Constructing complex numbers using recursion
by GrandFather
in thread Constructing complex numbers using recursion
by moltar512
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |