| 1 | package Math::Abacus; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use v5.10.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use feature 'say'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | my $len = 10; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 0 | ▶ | sub num_of_digit { | ||||||||||||||||||
|
Subroutine
num_of_digit: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 10 | 0 | ▶ | $len = $_[1] || $len; | ||||||||||||||||||
|
Condition0%0/3
$_[1] || $len
MC/DC0%0/2
$_[1] || $len
$_[1]
$len
Truth table0%0/3
A || B
A= $_[1]B= $len
| |||||||||||||||||||||
| 11 | 0 | return $len; | |||||||||||||||||||
| 12 | } | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 15 | 0 | my ($class) = @_; | |||||||||||||||||||
| 16 | 0 | my ($val) = ($_[1] =~ /(\d*)/); | |||||||||||||||||||
| 17 | 0 | ▶ | carp "Value extracted: $val.\n" if $_[1] !~ /^\d+$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | 0 | ▶ | carp "The input value is smaller than the maximum length of the abacus.\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | if $val >= 10**$len; | ||||||||||||||||||||
| 20 | 0 | bless { | |||||||||||||||||||
| 21 | _value => $val, | ||||||||||||||||||||
| 22 | }, $class; | ||||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 0 | ▶ | sub value { | ||||||||||||||||||
|
Subroutine
value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 26 | 0 | return $_[0]->{_value}; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | ▶ | sub add { | ||||||||||||||||||
|
Subroutine
add: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 30 | 0 | my ($self, $v_add) = @_; | |||||||||||||||||||
| 31 | 0 | ▶ | croak "Only non-negative integer operation.\n" if $v_add !~ /^\d+$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 0 | $self->{_value} += $v_add; | |||||||||||||||||||
| 33 | 0 | ▶ | carp "The input value is smaller than the maximum length of the abacus.\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | if $self->value >= 10**$len; | ||||||||||||||||||||
| 35 | 0 | return $self; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | ▶ | sub subtract { | ||||||||||||||||||
|
Subroutine
subtract: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 39 | 0 | my ($self, $v_sub) = @_; | |||||||||||||||||||
| 40 | 0 | ▶ | croak "Only non-negative integer operation.\n" if $v_sub !~ /^\d+$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | $self->{_value} -= $v_sub; | |||||||||||||||||||
| 42 | 0 | ▶ | carp "The current value of the abacus is negative.\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | if $self->value < 0; | ||||||||||||||||||||
| 44 | 0 | return $self; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | sub show { | ||||||||||||||||||
|
Subroutine
show: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 48 | 0 | my ($self) = @_; | |||||||||||||||||||
| 49 | 0 | $v = $self->value; | |||||||||||||||||||
| 50 | 0 | ▶ | _show( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | $v <= 0 ? | ||||||||||||||||||||
| 52 | 0 : | ||||||||||||||||||||
| 53 | $v % (10**$len) | ||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | ▶ | sub _show { | ||||||||||||||||||
|
Subroutine
_show: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 58 | 0 | my $value = $_[0]; | |||||||||||||||||||
| 59 | 0 | my @digits = split "", $value; | |||||||||||||||||||
| 60 | 0 | unshift @digits, (0) x ($len - scalar @digits); | |||||||||||||||||||
| 61 | 0 | my @mod5dgts = map {$_ % 5} @digits; | |||||||||||||||||||
| 62 | 0 | my $cross_line = join '-', ('+') x ($len+2); | |||||||||||||||||||
| 63 | 0 | say $cross_line; | |||||||||||||||||||
| 64 | 0 | say join " ", '|' , ('x') x $len, '|'; | |||||||||||||||||||
| 65 | 0 | ▶ | say join " ", '|' , (map {$_ < 5 ? 'x' : '|' } @digits), '|'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 0 | ▶ | say join " ", '|' , (map {$_ < 5 ? '|' : 'x' } @digits), '|'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | say $cross_line; | |||||||||||||||||||
| 68 | 0 | for my $o (0..5) { | |||||||||||||||||||
| 69 | 0 | ▶ | say join " ", '|' , (map {$_ == $o ? '|' : 'x' } @mod5dgts), '|'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | 0 | say $cross_line; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =head1 NAME | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | Math::Abacus - A toy model of Chinese abacus | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 VERSION | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Version 0.05 | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 1 | our $VERSION = '0.05'; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | use Math::Abacus; | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | Math::Abacus->num_of_digits(4); | ||||||||||||||||||||
| 91 | my $abacus = Math::Abacus->new(460); | ||||||||||||||||||||
| 92 | $abacus->add(1); | ||||||||||||||||||||
| 93 | $abacus->subtract(5); | ||||||||||||||||||||
| 94 | $abacus->show(); | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | +-+-+-+-+-+ | ||||||||||||||||||||
| 98 | | x x x x | | ||||||||||||||||||||
| 99 | | x x | | | | ||||||||||||||||||||
| 100 | | | | x x | | ||||||||||||||||||||
| 101 | +-+-+-+-+-+ | ||||||||||||||||||||
| 102 | | | x | x | | ||||||||||||||||||||
| 103 | | x x x | | | ||||||||||||||||||||
| 104 | | x x x x | | ||||||||||||||||||||
| 105 | | x x x x | | ||||||||||||||||||||
| 106 | | x | x x | | ||||||||||||||||||||
| 107 | | x x x x | | ||||||||||||||||||||
| 108 | +-+-+-+-+-+ | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =head1 METHODS | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =head2 show | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head2 add | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head2 subtract | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =head1 AUTHOR | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | Cheok-Yin Fung, C<< <fungcheokyin at gmail.com> >> | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =head1 SUPPORT | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | perldoc Math::Abacus | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | You can also look for information at: | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =over 4 | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Abacus> | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =item * CPAN Ratings | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | L<https://cpanratings.perl.org/d/Math-Abacus> | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =item * Search CPAN | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | L<https://metacpan.org/release/Math-Abacus> | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =back | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | This software is Copyright (c) 2022 by Cheok-Yin Fung. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | This is free software, licensed under: | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | MIT License (GPL Compatible) | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 1 | 1; # End of Math::Abacus | |||||||||||||||||||