Skip to content

Commit

Permalink
feat(计算): 添加计算表达式
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaoyaning committed Aug 9, 2022
1 parent 79339f3 commit 50d3601
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies {
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.8.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
Expand All @@ -70,4 +71,8 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'

implementation "com.apkfuns.logutils:library:1.7.5"
/**
* https://github.com/uklimaschewski/EvalEx
*/
implementation "com.udojava:EvalEx:2.7"
}
17 changes: 15 additions & 2 deletions app/src/main/java/com/jyn/composecalculator/DateViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import android.app.Application
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.unit.dp
import androidx.lifecycle.AndroidViewModel
import com.udojava.evalex.Expression

class DateViewModel(application: Application) : AndroidViewModel(application) {
var idpPortrait = true
var textBoxHeight = 0.dp
var inputText = mutableStateOf("")
var resultText = mutableStateOf("")
Expand All @@ -22,9 +22,22 @@ class DateViewModel(application: Application) : AndroidViewModel(application) {

fun clear() {
inputText.value = ""
resultText.value = ""
}

fun calculate() {
resultText.value = "XXXXX"
val expr = inputText.value.replace('×', '*').replace('÷', '/')
val expressions = arrayOf('+', '-', '*', '/')
if (expr.isEmpty()) return
if (!expressions.any { expr.contains(it) }) {
return
}
if (expressions.any { expr.endsWith(it) }) {
resultText.value = ""
return
}
val eval = Expression(expr).eval()

resultText.value = eval.toString()
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
compose_version = '1.2.0-rc01'
kotlin_version = '1.7.1'
kotlin_version = '1.6.0'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
Expand Down

0 comments on commit 50d3601

Please sign in to comment.