-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculadora.kt
179 lines (161 loc) · 4.71 KB
/
Calculadora.kt
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import kotlin.math.pow
const val size = 40
fun main() {
var exit:Boolean = false
while(!exit) {
println("=".repeat(size))
println(" ".repeat(((size - 9)/2).toInt()) + "CALCULADORA")
println("=".repeat(size))
println(" ".repeat(((size - 21)/2).toInt()) + "Selecione a operação:")
println(" 1 -> Soma")
println(" 2 -> Subtração")
println(" 3 -> Multiplicação")
println(" 4 -> Divisão")
println(" 5 -> Potenciação")
println(" 6 -> Raiz")
println(" 7 -> Fatoração")
println(" 8 -> Fatorial")
println(" 9 -> Porcentagem")
println("=".repeat(size))
print(" >>> ")
val operation = readLine()!!.trim().toString()
exit = when (operation){
"1" -> {question("somar")
soma()}
"2" -> {question("subtrair")
subtracao()}
"3" -> {question("multiplicar")
multiplicacao()}
"4" -> {question("dividir")
divisao()}
"5" -> {question("a potencia")
potenciacao()}
"6" -> {question("a raiz")
raiz()}
"7" -> {question("fatorar")
fatoracao()}
"8" -> {question("o fatorial")
fatorial()}
"9" -> {question("a porcentagem")
porcentagem()}
else -> {println("Valor inválido, tente novamente!")
true}
}
}
println("=".repeat(size))
println(" ".repeat(((size - 32) / 2).toInt()) + "Obrigado por usar nossa Calculadora!")
println("=".repeat(size))
}
fun question(t:String){
println("-".repeat(size))
if (t in listOf("fatorar", "a potencia", "a porcentagem")) {
println(" ".repeat(((size - (23 + t.length))/2).toInt()) + "Valor que você deseja $t?")
} else {
println(" ".repeat(((size - (25 + t.length))/2).toInt()) + "Valores que você deseja $t?")
}
println("-".repeat(size))
}
fun value(sig:String = " "):Double{
var v:Double?
while(true){
print(" >>> $sig ")
try {
v = readLine()!!.toDouble()
break
} catch(e: Exception) {
println((" ".repeat(((size - 37)/2).toInt()) + "Valor inválido, tente novamente [0.0]"))
}
}
return v ?: 0.0
}
fun valueInt(sig:String = " "):Int{
var v:Int?
while(true){
print(" >>> $sig ")
try {
v = readLine()!!.toInt()
break
} catch(e: Exception) {
println((" ".repeat(((size - 37)/2).toInt()) + "Valor inválido, tente novamente [0.0]"))
}
}
return v ?: 0
}
fun exit():Boolean {
println("\n" + "-".repeat(size))
println(" ".repeat(((size - 33)/2).toInt()) + "Se deseja fazer uma nova operação")
println(" ".repeat(((size - 12)/2).toInt()) + "Digite: sim")
print(" >>> ")
val resp = readLine()!!.trim().toString()
return if (resp == "sim") false else true
}
fun soma():Boolean {
val v1:Double = value()
val v2:Double = value("+")
println("\n -> $v1 + $v2 = ${v1 + v2}")
return exit()
}
fun subtracao():Boolean {
val v1:Double = value()
val v2:Double = value("-")
println("\n -> $v1 - $v2 = ${v1 - v2}")
return exit()
}
fun multiplicacao():Boolean {
val v1:Double = value()
val v2:Double = value("*")
println("\n -> $v1 * $v2 = ${v1 * v2}")
return exit()
}
fun divisao():Boolean {
val v1:Double = value()
val v2:Double = value("/")
println("\n -> $v1 / $v2 = ${v1 / v2}")
return exit()
}
fun potenciacao():Boolean {
val v1:Double = value()
println(" Potência de:")
val v2:Double = value()
println("\n -> $v1 ^ $v2 = ${v1.pow(v2)}")
return exit()
}
fun raiz():Boolean {
val v1:Double = value()
println(" Raiz de:")
val v2:Double = value()
println("\n -> $v1 ^ $v2 = ${v1.pow(1.0/v2)}")
return exit()
}
fun fatoracao():Boolean {
var v1:Int = valueInt()
print("\n -> $v1 = ")
for (c in 2..v1.toDouble().pow(1.0/2.0).toInt()) {
while(v1 % c == 0) {
v1 /= c
if (v1 != 1) {
print("$c * ")
} else {
println("$c")
}
}
}
if(v1 > 1) println("$v1")
return exit()
}
fun fatorial():Boolean {
var v1:Int = valueInt()
print("\n -> $v1! = ")
for (c in (v1 - 1) downTo 2) {
v1 *= c
}
println(v1)
return exit()
}
fun porcentagem():Boolean {
val v1:Double = value()
println(" Qual a porcentagem?")
val v2:Double = value()
println("\n -> $v2% de $v1 = ${v1 * (v2/100)}")
return exit()
}