Created
May 29, 2022 14:32
-
-
Save robsongomes/16b3aad503557b80e07fd6ac69ee1699 to your computer and use it in GitHub Desktop.
Faça um programa que calcula a área de qualquer forma geométrica. Use os conceitos aprendidos até aqui com structs e interfaces.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
type forma interface { | |
area() float64 | |
} | |
type quadrado struct { | |
lado int | |
} | |
type triangulo struct { | |
base, altura int | |
} | |
type circulo struct { | |
raio float64 | |
} | |
func (q quadrado) area() float64 { | |
return float64(q.lado * q.lado) | |
} | |
func (q quadrado) String() string { | |
return "Quadrado" | |
} | |
func (t triangulo) area() float64 { | |
return float64(t.base * t.altura) / 2.0 | |
} | |
func (t triangulo) String() string { | |
return "Triângulo" | |
} | |
func (c circulo) area() float64 { | |
return math.Pi * math.Pow(c.raio, 2) | |
} | |
func (c circulo) String() string { | |
return "Círculo" | |
} | |
func calculaArea(f forma) { | |
fmt.Printf("A área da %v é %.2f\n", f, f.area()) | |
} | |
func main() { | |
formas := []forma{ | |
quadrado{5}, | |
triangulo{4, 5}, | |
circulo{3.5}, | |
} | |
for _, f := range formas { | |
calculaArea(f) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment