Nama: Richard Ryan
NRP: 5025211141
Kelas: PPB (G)
Tema: Calculator
Link Github : Github
Penjelasan Kode
Pertama buat variabel yang akan digunakan untuk menyimpan persamaan dan hasilnya
var equation by remember { mutableStateOf("") } var result by remember { mutableStateOf("") }
Fungsi calc() akan menggunakan Expression() dari library org.mariuszgromada.math.mxparser.Expression yang dapat digunakan untuk mendapatkan nilai suatu persamaan dari string ekspresi
Fungsi addToEq() digunakan untuk menambahkan karakter ke persamaan
Fungsi clearEq() digunakan untuk menghapus persamaan yang ada
Fungsi dropLast() digunakan untuk menghapus karakter terakhir dari fungsi
Fungsi equals() digunakan untuk memindahkan hasil perhitungan ke persamaan sehingga dapat mempermudah perhitungan berikutnya
Fungsi calc() akan dipanggil sebagai langkah pertama setiap fungsi tersebut, sehingga seketika button diklik, dapat dibuat preview hasil persamaan, namun kekurangannya adalah apabila persamaan belum valid (contoh: karakter terakhir adalah operasi +,-,*,/ atau tanda kurung belum ditutup) maka akan muncul Error
fun calc() { try { val exp = Expression(equation) val value = exp.calculate() result = if (value.isNaN()) { "= Error" } else { "= ${DecimalFormat("0.######").format(value)}" } } catch (e: Exception) { result = "= $e" } } fun addToEq(s: String) { equation += s calc() } fun clearEq() { equation = "" result = "" } fun dropLast() { if (equation.isNotEmpty()) { equation = equation.dropLast(1) calc() } } fun equals() { calc() if(result != "= Error") { equation = result.substring(2) result = "" } }
Column( modifier = Modifier .padding(horizontal = 12.dp, vertical = 16.dp) .fillMaxSize(), verticalArrangement = Arrangement.Bottom ) { Text( text = equation, fontSize = 40.sp, textAlign = TextAlign.End, fontWeight = FontWeight.SemiBold, modifier = Modifier .fillMaxWidth() .padding(bottom = 8.dp) ) Text( text = result, fontSize = 40.sp, textAlign = TextAlign.End, fontWeight = FontWeight.SemiBold, color = Color(0xFF706E6E), modifier = Modifier .fillMaxWidth() .padding(bottom = 16.dp) ) HorizontalDivider( modifier = Modifier.padding(vertical = 8.dp), thickness = 2.dp, color = Color.LightGray ) val buttonRows = listOf( listOf("C", "(", ")", "/"), listOf("7", "8", "9", "*"), listOf("4", "5", "6", "+"), listOf("1", "2", "3", "-"), listOf("AC", "0", ".", "=") ) buttonRows.forEach { row -> Row( modifier = Modifier .fillMaxWidth() .padding(vertical = 4.dp), horizontalArrangement = Arrangement.SpaceEvenly ) { row.forEach { label -> addButton( text = label, color = if(label >= "0" && label <= "9") Color.Black else Color(0xFFFF9800), action = { when (label) { "C" -> clearEq() "AC" -> dropLast() "=" -> equals() else -> addToEq(label) } } ) } } } }
@Composable fun addButton( text: String, color: Color, action: () -> Unit ) { Button( onClick = action, modifier = Modifier .padding(4.dp) .shadow(8.dp, shape = RoundedCornerShape(50)) .size(78.dp), // Circular buttons shape = RoundedCornerShape(50), colors = ButtonDefaults.buttonColors( containerColor = Color(0xFFFAFAFA), contentColor = color ), elevation = ButtonDefaults.elevatedButtonElevation(defaultElevation = 6.dp) ) { if (text == "AC") { Icon( imageVector = Icons.Default.Backspace, contentDescription = "Backspace", tint = color, modifier = Modifier.size(32.dp) ) } else { Text( text = text, fontSize = 32.sp ) } } }
Comments
Post a Comment