- 论坛徽章:
- 0
|
想要做一个复数的加减乘除求模等等等等运算的包,写完后,发现包获取的值不对- package My_complex;
- #!/usr/bin/perl -w
- use strict;
- use overload
- '+' => \&plus,
- '-' => \&subtraction,
- '*' => \&multi,
- '/' => \&div,
- abs => \&abs,
- '""' => \&print,
- '=' => \&set,
- '==' => \&equal;
- sub get
- {
- $_=shift;
- print "$_\n";
- my @num=/[0-9]+/g;
- my ($i,$r);
- if(/^i$/)
- {$r=0;$i=1;print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^\-i$/)
- {$r=0;$i=-1;print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^[0-9]+$/)
- {$r=$num[0] ;$i=0;print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^\-[0-9]+$/)
- {$r= -$num[0] ;$i=0;print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^[0-9]+i$/)
- {$r=0 ;$i=$num[0];print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^\-[0-9]+i$/)
- {$r=0 ;$i= -$num[0];print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^[0-9]+\+[0-9]+i$/)
- {$r=$num[0] ;$i=$num[1];print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^\-[0-9]+\+[0-9]+i$/)
- {$r= -$num[0] ;$i=$num[1];print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^[0-9]+\+i$/)
- {$r=$num[0] ;$i=1;print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^\-[0-9]+\+i$/)
- {$r= -$num[0];$i=1;print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^[0-9]+\-[0-9]+i$/)
- {$r= $num[0];$i= -$num[1];print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^[0-9]+\-i$/)
- {$r= $num[0];$i= -1;print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^\-[0-9]+\-[0-9]+i$/)
- {$r= -$num[0] ;$i= -$num[1];print "\$r is ",$r," \$i is ",$i,"\n"}
- if(/^\-[0-9]+\-i$/)
- {$r= -$num[0] ;$i=-1;print "\$r is ",$r," \$i is ",$i,"\n"}
- my %self=("real"=>$r,"image"=>$i);
- print "$r\n$i";
- return bless \%self;
- }
- sub plus
- {
- my($x,$y)=@_;
- my $xr=$x{"real"};
- my $xi=$x{"image"};
- my $yr=$y{"real"};
- my $yi=$y{"image"};
- my $realvalue=$xr+$yr;
- my $imagevalue=$xi+$yi;
- my %self=("real"=>$realvalue,"image"=>$imagevalue);
- return bless \%self;
- }
- sub subtraction
- {
- my($x,$y)=@_;
- my $xr=$x{"real"};
- my $xi=$x{"image"};
- my $yr=$y{"real"};
- my $yi=$y{"image"};
- my $realvalue=$xr-$yr;
- my $imagevalue=$xi-$yi;
- my %self=("real"=>$realvalue,"image"=>$imagevalue);
- return bless \%self;
- }
- sub multi
- {
- my($x,$y)=@_;
- my $xr=$x{"real"};
- my $xi=$x{"image"};
- my $yr=$y{"real"};
- my $yi=$y{"image"};
- my $realvalue=$xr*$yr-$xi*$yi;
- my $imagevalue=$xi*$yr+$yi*$xr;
- my %self=("real"=>$realvalue,"image"=>$imagevalue);
- return bless \%self;
- }
- sub div
- {
- my($x,$y)=@_;
- my $xr=$x{"real"};
- my $xi=$x{"image"};
- my $yr=$y{"real"};
- my $yi=$y{"image"};
- my $abs=$y->abs;
- my $realvalue=($xr*$yr+$xi*$yi)/$abs;
- my $imagevalue=($xi*$yr-$yi*$xr)/$abs;
- my %self=("real"=>$realvalue,"image"=>$imagevalue);
- return bless \%self;
- }
-
- sub abs
- {
- my $this=shift;
- my $r=$this{"real"};
- my $i=$this{"image"};
- return $r*$r+$i*$i;
- }
- sub print
- {
- my $this=@_;
- my ($key,$value,$result);
- while(($key,$value)=each %$this)
- {
- $result .="$key=>$value\n";
- }
- return $result;
- }
- sub set
- {
- my($x,$y)=@_;
- my %self;
- my $yr=$y{"real"};
- my $yi=$y{"image"};
- $self{"real"}=$yr;
- $self{"image"}=$yi;
- return bless \%self;
- }
- sub equal
- {
- my($x,$y)=@_;
- my $xr=$x{"real"};
- my $xi=$x{"image"};
- my $yr=$y{"real"};
- my $yi=$y{"image"};
- return (($xr==$yr)and($xi==$yi));
- }
- 1;
复制代码 主程序代码如下- #!/usr/local/bin/perl -w
- use strict;
- use My_complex;
- my $a="i";
- my $b="-i";
- my $byte1=My_complex->get($a);
- my $byte2=My_complex->get($b);
-
- my $byte3=$byte1 + $byte2;
- print $byte3{"real"},"\n";
- print $byte3{"image"},"\n";
复制代码 输出完后发现get方法获得的是My_complex。。。。 |
|