line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Math::Tutor::Cmd::Unit::Cmd::Cast; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20646
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
4
|
use vars qw(@ISA $VERSION); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
67
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
App::Math::Tutor::Cmd::Unit::Cmd::Cast - Plugin for casting of numbers with units |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=cut |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.005'; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
4
|
use Moo; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
17
|
1
|
|
|
1
|
|
301
|
use MooX::Cmd; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
18
|
1
|
|
|
1
|
|
1593
|
use MooX::Options; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
1142
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
55
|
|
21
|
1
|
|
|
1
|
|
215
|
use File::ShareDir (); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use Template (); |
23
|
|
|
|
|
|
|
use Scalar::Util qw(looks_like_number); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has template_filename => ( |
26
|
|
|
|
|
|
|
is => "ro", |
27
|
|
|
|
|
|
|
default => "twocols" |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
with "App::Math::Tutor::Role::UnitExercise", "App::Math::Tutor::Role::DecFracExercise"; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _get_castable_numbers |
33
|
|
|
|
|
|
|
{ |
34
|
|
|
|
|
|
|
my ( $self, $quantity ) = @_; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my @result; |
37
|
|
|
|
|
|
|
while ( $quantity-- ) |
38
|
|
|
|
|
|
|
{ |
39
|
|
|
|
|
|
|
my ( $un, $base, $ut ); |
40
|
|
|
|
|
|
|
do |
41
|
|
|
|
|
|
|
{ |
42
|
|
|
|
|
|
|
($un) = $self->get_unit_numbers( 1, $ut ); |
43
|
|
|
|
|
|
|
my $rng = $un->end - $un->begin; |
44
|
|
|
|
|
|
|
$base = int( rand($rng) ) + $un->begin; |
45
|
|
|
|
|
|
|
defined $ut or $ut = $un->type; |
46
|
|
|
|
|
|
|
} while ( !$self->_check_decimal_fraction( $un->_numify($base) ) ); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
push @result, |
49
|
|
|
|
|
|
|
[ |
50
|
|
|
|
|
|
|
$un, |
51
|
|
|
|
|
|
|
Unit->new( |
52
|
|
|
|
|
|
|
parts => [ $un->_numify($base) ], |
53
|
|
|
|
|
|
|
begin => $base, |
54
|
|
|
|
|
|
|
end => $base, |
55
|
|
|
|
|
|
|
type => $un->type |
56
|
|
|
|
|
|
|
) |
57
|
|
|
|
|
|
|
]; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
@result; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _build_exercises |
64
|
|
|
|
|
|
|
{ |
65
|
|
|
|
|
|
|
my ($self) = @_; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my (@tasks); |
68
|
|
|
|
|
|
|
foreach my $i ( 1 .. $self->quantity ) |
69
|
|
|
|
|
|
|
{ |
70
|
|
|
|
|
|
|
my @line; |
71
|
|
|
|
|
|
|
foreach my $j ( 0 .. 1 ) |
72
|
|
|
|
|
|
|
{ |
73
|
|
|
|
|
|
|
push @line, $self->_get_castable_numbers(1); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
push @tasks, \@line; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $exercises = { |
79
|
|
|
|
|
|
|
section => "Unit casting", |
80
|
|
|
|
|
|
|
caption => 'Units', |
81
|
|
|
|
|
|
|
label => 'unit_castings', |
82
|
|
|
|
|
|
|
header => [ [ 'Multiple entity => Single entity', 'Single entity => Multiple entity' ] ], |
83
|
|
|
|
|
|
|
solutions => [], |
84
|
|
|
|
|
|
|
challenges => [], |
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $digits = $self->digits; |
88
|
|
|
|
|
|
|
foreach my $line (@tasks) |
89
|
|
|
|
|
|
|
{ |
90
|
|
|
|
|
|
|
my ( @solution, @challenge ); |
91
|
|
|
|
|
|
|
# cast unit with multiple entities to unit with one entity |
92
|
|
|
|
|
|
|
my ( $unit, $based ) = @{ $line->[0] }; |
93
|
|
|
|
|
|
|
my $base = $based->begin; |
94
|
|
|
|
|
|
|
my $base_name = $unit->type->{spectrum}->[$base]->{unit}; |
95
|
|
|
|
|
|
|
push @challenge, sprintf( '$ %s = %s\\text{%s} $', $unit, "\\ldots" x int( length($based) / 3 ), $base_name ); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my @way; # remember Frank Sinatra :) |
98
|
|
|
|
|
|
|
push @way, "" . $unit; |
99
|
|
|
|
|
|
|
push @way, "" . $based; |
100
|
|
|
|
|
|
|
push( @solution, '$ ' . join( " = ", @way ) . ' $' ); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# cast decimal to vulgar fraction |
103
|
|
|
|
|
|
|
@way = (); |
104
|
|
|
|
|
|
|
( $unit, $based ) = @{ $line->[1] }; |
105
|
|
|
|
|
|
|
$base_name = $unit->type->{spectrum}->[$base]->{unit}; |
106
|
|
|
|
|
|
|
$base = $based->begin; |
107
|
|
|
|
|
|
|
push @challenge, sprintf( '$ %s = %s $', $based, "\\ldots" x int( length($unit) / 3 ) ); |
108
|
|
|
|
|
|
|
push @way, "" . $based; |
109
|
|
|
|
|
|
|
push @way, "" . $unit; |
110
|
|
|
|
|
|
|
push( @solution, '$ ' . join( " = ", @way ) . ' $' ); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
push( @{ $exercises->{solutions} }, \@solution ); |
113
|
|
|
|
|
|
|
push( @{ $exercises->{challenges} }, \@challenge ); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
$exercises; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Copyright 2010-2014 Jens Rehsack. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
125
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
126
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
1; |