- 论坛徽章:
- 145
|
本帖最后由 jason680 于 2012-12-03 14:46 编辑
回复 1# sosflyer
How about this way, change $x to i, and equal "=" to subtraction "-"
$ perl -e 'use Math::Complex; $math=1+2+i-10;;print "Ans=" . -Re($math)/Im($math)'
Ans=7
$ perl -e 'use Math::Complex; $math=1+2+(2+3*i)*5-10;;print "Ans=" . -Re($math)/Im($math)'
Ans=-0.2
$ cat function.pl
use strict;
use warnings;
use Math::Complex;
while(<DATA>){
my $sAns = get_answer($_);
print "\$x = $sAns\n";
}
sub get_answer{
s/\$x/i/g;
s/=/-(/;
$_ = 'my $x=' . $_ . ');return -Re($x)/Im($x);';
eval "$_";
}
__DATA__
1 + 2 + $x = 10
1 + 2 + $x = 1+ 2* $x
1+2+(2+3*$x)*5 = 10
$ perl function.pl
$x = 7
$x = 2
$x = -0.2
|
|