ya.. i'm way to green to read regular expressions :( :(
here is my constructComplex method in it's entirety, for extreme clarity
sub constructComplex {
my $value1 = $_[0];
my $value2 = $_[1];
my ($real1, $real2, $complex1, $complex2, $magnitude1, $magnitude2
+, $angle1, $angle2, @constructed, @constructedvalue1Polar, @construc
+tedvalue1Rect, @constructedvalue2Polar, @constructedvalue2Rect, $valu
+e1isRectangular, $value2isRectangular);
#if value1 is in rectangular form
if(rindex($value1, "_") < 0)
{
$value1isRectangular = 1;
}
#if value1 is in polar form
if(rindex($value1, "_") > 0)
{
$value1isRectangular = 0;
$magnitude1 = substr($value1, 0, index($value1, " "));
$angle1 =substr($value1, (rindex($value1, " ") + 1), length($v
+alue1));
#@constructedvalue1Polar = &constructComplex(&PolarToRect($val
+ue1));
#$real1 = $constructedvalue1Polar[0];
#$complex1 = $constructedvalue1Polar[2];
}
#if value2 is in rectangular form
if(rindex($value2, "_") < 0)
{
$value2isRectangular = 1;
}
#if value2 is in polar form
if(rindex($value2, "_") > 0)
{
$value2isRectangular = 0;
$magnitude2 = substr($value2, 0, index($value2, " "));
$angle2 =substr($value2, (rindex($value2, " ") + 1), length($v
+alue2));
}
##############
if($value1isRectangular)
{
#lowercases everything
$value1 = lc($value1);
#if a j is found, turns it into an i
if(rindex($value1, "j") > 0)
{
substr ($value1, rindex($value1, "j"), 1, "i");
}
#if there is no space, and there is no i, then there must ONLY
+ be a real part
if(index($value1, " ") < 0 && index($value1, "i") < 0)
{
$real1 = substr($value1, 0, length($value1));
}
#if there is no space, but there is an i, then there is no rea
+l part, real is 0
elsif(index($value1, " ") < 0 && index($value1, "i") > 0)
{
$real1 = "0";
}
#else, there is a space, meaning both real and complex
else
{
$real1 = substr($value1, 0, index($value1, " "));
}
#"7 + i"
if((index($value1, " ") > 0) && (index($value1, "i") > 0) && (
+(rindex($value1, " ") + 1) == index($value1, "i")))
{
$complex1 = "1";
}
#"7 + -i" and "-i"
#elsif((index($value1, " ") > 0) && (index($value1, "i") > 0)
+&& (index($value1, "-") > 0) && ((rindex($value1, " ") + 1) == rindex
+($value1, "-")) && ((rindex($value1, "-") + 1) == rindex($value1, "i"
+)))
elsif(index($value1, "-i") != -1)
{
$complex1 = "-1";
}
#"i"
elsif((index($value1, "i") == 0))
{
$complex1 = "1";
}
#"-Bi"
elsif((index($value1, " ") < 0) && (index($value1, "i") > 0) &
+& (index($value1, "-") != -1) && (index($value1, "i") != 0) && ((inde
+x($value1, "-") +1) != index($value1, "i")))
{
$complex1 = substr($value1, 0, index($value1, "i"));
}
#"Bi"
elsif((index($value1, " ") < 0) && (index($value1, "i") > 0) &
+& (index($value1, "-") < 0) && (index($value1, "i") != 0))
{
$complex1 = substr($value1, 0, index($value1, "i"));
}
#"30"
elsif(index($value1, "i") < 0)
{
$complex1 = 0
}
else
{
$complex1 = substr($value1, (rindex($value1, " ") + 1), in
+dex($value1, "i"));
}
#if negative by sign, changes element to negative
if(index($value1, " - ") > 0)
{
$complex1 = -$complex1;
}
}
if($value2isRectangular)
{
#lowercases everything
$value2 = lc($value2);
#if a j is found, turns it into an i
if(rindex($value2, "j") > 0)
{
substr ($value2, rindex($value2, "j"), 1, "i");
}
#if there is no space, and there is no i, then there must ONLY
+ be a real part
if(index($value2, " ") < 0 && index($value2, "i") < 0)
{
$real2 = substr($value2, 0, length($value2));
}
#if there is no space, but there is an i, then there is no rea
+l part, real is 0
elsif(index($value2, " ") < 0 && index($value2, "i") > 0)
{
$real2 = "0";
}
#else, there is a space, meaning both real and complex
else
{
$real2 = substr($value2, 0, index($value2, " "));
}
#"7 + i"
if((index($value2, " ") > 0) && (index($value2, "i") > 0) && (
+(rindex($value2, " ") + 1) == index($value2, "i")))
{
$complex2 = "1";
}
#"7 + -i" and "-i"
#elsif((index($value2, " ") > 0) && (index($value2, "i") > 0)
+&& (index($value2, "-") > 0) && ((rindex($value2, " ") + 1) == rindex
+($value2, "-")) && ((rindex($value2, "-") + 1) == rindex($value2, "i"
+)))
elsif(index($value2, "-i") != -1)
{
$complex2 = "-1";
}
#"i"
elsif((index($value2, "i") == 0))
{
$complex2 = "1";
}
#"-Bi"
elsif((index($value2, " ") < 0) && (index($value2, "i") > 0) &
+& (index($value2, "-") != -1) && (index($value2, "i") != 0) && ((inde
+x($value2, "-") +1) != index($value2, "i")))
{
$complex2 = substr($value2, 0, index($value2, "i"));
}
#"Bi"
elsif((index($value2, " ") < 0) && (index($value2, "i") > 0) &
+& (index($value2, "-") < 0) && (index($value2, "i") != 0))
{
$complex2 = substr($value2, 0, index($value2, "i"));
}
#"30"
elsif(index($value2, "i") < 0)
{
$complex2 = 0
}
else
{
$complex2 = substr($value2, (rindex($value2, " ") + 1), in
+dex($value2, "i"));
}
#if negative by sign, changes element to negative
if(index($value2, " - ") > 0)
{
$complex2 = -$complex2;
}
}
#if($real1 ||= 0) {$real1 = 0};
#if($real2 ||= 0) {$real2 = 0};
#if($complex1 ||= 0) {$complex1 = 0};
#if($complex2 ||= 0) {$complex2 = 0};
$constructed[0] = $real1;
$constructed[1] = $real2;
$constructed[2] = $complex1;
$constructed[3] = $complex2;
$constructed[4] = $magnitude1;
$constructed[5] = $magnitude2;
$constructed[6] = $angle1;
$constructed[7] = $angle2;
#return "R1: " . $real1 . " C1: " . $complex1 . " R2: " . $real2 . " C
+2: " . $complex2 . " M1: " . $magnitude1 . " A1: " . $angle1 . " M2:
+" . $magnitude2 . " A2: " . $angle2;
return @constructed;
}
sub PolarToRect {
my ($value1, $real1, $complex1, $magnitude1, $angle1, $complex, @const
+ruct);
$value1 = $_[0];
@construct = &constructComplex($value1);
$magnitude1 = $construct[4];
$angle1 = $construct[6];
#return "M: " . $magnitude1 . " A: " . $angle1;
$real1 = $magnitude1*cos($angle1*($pi/180));
$complex1 = $magnitude1*sin($angle1*($pi/180));
#return "R: " . $real1 . " C: " . $complex1;
if($complex1 < 0)
{
$complex = $real1 . " - " . -$complex1 . "i";
}
else
{
$complex = $real1 . " + " . $complex1 . "i";
}
}
my $z = "5 _ 36.87";
my $y = "6 _ 45.67";
print PolarToRect($z) . "\n";
|