in reply to Constructing complex numbers using recursion
#!/usr/bin/perl -w use strict; use Math::Complex; # http://perldoc.perl.org/Math/Complex.html my $a = toComplex("1-4i"); my $b = toComplex("2+7j"); my $c = toComplex("3 _ 2"); my $d = toComplex("5 _ 0.707"); $\ = "\n"; print $a + $b; print $c * $d; sub toComplex { my $str = shift; $str =~ tr/j/i/; # convert j's to i's $str =~ s/(.*)_(.*)/[$1,$2]/; # convert "_" into something that # Math::Complex will like return Math::Complex->make($str); }
|
|---|