forked from GolangUnited/golang-united-school-homework-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.go
40 lines (33 loc) · 986 Bytes
/
solution.go
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
package square
import (
"math"
)
// Define custom int type to hold sides number and update CalcSquare signature by replacing #yourTypeNameHere#
// Define constants to represent 0, 3 and 4 sides. Test uses mnemos: SidesTriangle(==3), SidesSquare(==4), SidesCircle(==0)
// it's like:
// CalcSquare(10.0, SidesTriangle)
// CalcSquare(10.0, SidesSquare)
// CalcSquare(10.0, SidesCircle)
//func CalcSquare(sideLen float64, sidesNum #yourTypeNameHere#) float64 {
//}
type smartType int
const (
SidesCircle smartType = 0
SidesTriangle = 3
SidesSquare = 4
)
func CalcSquare(sideLen float64, sidesNum smartType) float64 {
switch sidesNum {
case 0:
return math.Pi * (math.Pow(sideLen, 2))
case 3:
thirdSideLen := sideLen
perimeter := (sideLen*2 + thirdSideLen) / 2
area := math.Sqrt(perimeter * ((perimeter - sideLen) * (perimeter - sideLen) * (perimeter - thirdSideLen)))
return area
case 4:
return sideLen * sideLen
default:
return 0
}
}