-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshapes_ex
More file actions
executable file
·98 lines (77 loc) · 1.99 KB
/
shapes_ex
File metadata and controls
executable file
·98 lines (77 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
#
# An example that illustrates `methods`. The example introduces
# `structs` for Circle, Square, and Rectangle, as well as a
# `methods` for each of them for computing area.
readonly DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. ${DIR}/../gobash
function Circle() {
[ $# -ne 1 ] && return $EC
local -r r="${1}"
shift 1
make_ $FUNCNAME \
"r" "${r}"
# We do not need to return explicitly (because the
# default output is the result of the last command, but
# being explicit is good in some cases).
# return $?
}
function Circle_area() {
local -r c="${1}"
shift 1
local result
result=$(echo "$MATH_PI * $($c r) * $($c r)" | bc)
echo ${result}
return 0
}
function Square() {
[ $# -ne 1 ] && return $EC
local -r a="${1}"
shift 1
make_ $FUNCNAME \
"a" "${a}"
}
function Square_area() {
local -r obj="${1}"
shift 1
local result
result=$(( $($obj "a") * $($obj "a") ))
echo ${result}
return 0
}
function Rectangle() {
[ $# -ne 2 ] && return $EC
local -r a="${1}"
local -r b="${2}"
shift 2
make_ $FUNCNAME \
"a" "${a}" \
"b" "${b}"
}
function Rectangle_area() {
local -r obj="${1}"
shift 1
local result
result=$(( $($obj "a") * $($obj "b") ))
echo ${result}
return 0
}
function main() {
lst=$(List)
c=$(Circle 4)
$lst add "$c"
s=$(Square 4)
$lst add "$s"
r=$(Rectangle 2 2)
$lst add "$r"
assert_eq $($lst len) 3
local total=0
local i
for (( i=0; i<$($lst len); i++ )); do
local el=$($lst get ${i})
total=$(echo "$($el area) + ${total}" | bc -l)
done
printf "Total area: %g\n" ${total}
assert_has_prefix "${total}" "70"
}
main