-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (79 loc) · 1.72 KB
/
Copy pathmain.go
File metadata and controls
93 lines (79 loc) · 1.72 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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
var (
reader *bufio.Reader
)
func main() {
//inicializa o reader
reader = bufio.NewReader(os.Stdin)
for {
fmt.Println("********************************")
fmt.Println("* Seja bem vindo à Calculadora *")
fmt.Println("********************************")
fmt.Println("\nSelecione uma operação:")
fmt.Println("1-Adição\n2-Subtração\n3-Multiplicação\n4-Divisão\n5-Sair")
option := readLineString()
if option == "5" {
fmt.Println("Fim :)")
os.Exit(0)
}
fmt.Println("Insira o primeiro valor:")
value1, error := strconv.ParseFloat(readLineString(), 64)
if error != nil {
log.Printf("Ocorreu um erro ao capturar o primeiro valor")
os.Exit(1)
}
fmt.Println("Insira o segundo valor:")
value2, error := strconv.ParseFloat(readLineString(), 64)
if error != nil {
log.Printf("Ocorreu um erro ao capturar o segundo valor")
os.Exit(1)
}
var result float64
switch option {
case "1":
{
println("Adicao:")
result = value1 + value2
}
case "2":
{
println("Subtração:")
result = value1 - value2
}
case "3":
{
println("Multiplicação:")
result = value1 * value2
}
default:
{
println("Divisão:")
if value2 == 0 {
fmt.Println("Divisão por 0, não permitida")
continue
}
result = value1 / value2
}
}
fmt.Println("Resultado", result)
fmt.Println("\nPrecione 'ENTER' para continuar... :)")
readLineString()
}
fmt.Println("Fim :)")
}
func readLineString() string {
line, error := reader.ReadString('\n')
if error != nil {
log.Printf("Ocorreu um erro ao capturar opção")
os.Exit(1)
}
return strings.Replace(line, "\n", "", 1)
}