From 87c0b520a6e97e9f1ce552ef6bde6aa0b7a1f581 Mon Sep 17 00:00:00 2001 From: Jay Barria Date: Thu, 24 Sep 2020 07:14:43 +0800 Subject: [PATCH] percent support --- build.gradle | 6 +- .../calculators/BasicMdasCalculator.kt | 2 +- .../calculators/BasicNonMdasCalculator.kt | 103 ++++++++++++++---- 3 files changed, 87 insertions(+), 24 deletions(-) diff --git a/build.gradle b/build.gradle index d1f5b9c..e90d0f3 100644 --- a/build.gradle +++ b/build.gradle @@ -8,8 +8,8 @@ plugins { * Use plugin 'kotlin' if importing this project into Android Studio * Use plugin 'org.jetbrains.kotlin.jvm' version '1.3.61' to build library */ - id 'kotlin' -// id 'org.jetbrains.kotlin.jvm' version '1.4.10' +// id 'kotlin' + id 'org.jetbrains.kotlin.jvm' version '1.4.10' } allprojects { @@ -19,7 +19,7 @@ allprojects { } group 'com.github.jairrab.calc' -version "2.4.0" +version "3.0.0" java { diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/mathutils/calculators/BasicMdasCalculator.kt b/src/main/kotlin/com/github/jairrab/calc/lib/mathutils/calculators/BasicMdasCalculator.kt index 40ceb1c..835408b 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/mathutils/calculators/BasicMdasCalculator.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/mathutils/calculators/BasicMdasCalculator.kt @@ -45,7 +45,7 @@ class BasicMdasCalculator : EntriesCalculator { a += b } DIVISION.tag -> { - b /= entries[i].toDouble() + b /= getEntryWithPercentFactor(entries[i]) a += b } else -> throw IllegalStateException("Error solving last entry") diff --git a/src/main/kotlin/com/github/jairrab/calc/lib/mathutils/calculators/BasicNonMdasCalculator.kt b/src/main/kotlin/com/github/jairrab/calc/lib/mathutils/calculators/BasicNonMdasCalculator.kt index 3ebb3c3..4e281ef 100644 --- a/src/main/kotlin/com/github/jairrab/calc/lib/mathutils/calculators/BasicNonMdasCalculator.kt +++ b/src/main/kotlin/com/github/jairrab/calc/lib/mathutils/calculators/BasicNonMdasCalculator.kt @@ -2,35 +2,98 @@ package com.github.jairrab.calc.lib.mathutils.calculators -import com.github.jairrab.calc.CalculatorButton +import com.github.jairrab.calc.CalculatorButton.* import com.github.jairrab.calc.lib.mathutils.EntriesCalculator -import com.github.jairrab.calc.lib.mathutils.OperatorUtils +import com.github.jairrab.calc.lib.mathutils.OperatorUtils.isOperator +import com.github.jairrab.calc.lib.utils.trimEndChar class BasicNonMdasCalculator : EntriesCalculator { override fun solve(entries: List): Double { - return scan(entries) + val entriesToProcess = entries.toMutableList().apply { + if (isOperator(last())) removeLast() + } + + return calculate(entriesToProcess) } - private fun scan(entries: List): Double { - var result = 0.0 - var lastOperator = CalculatorButton.PLUS - for (entry in entries) { - when { - OperatorUtils.isNumber(entry) -> { - result = when (lastOperator) { - CalculatorButton.PLUS -> result + entry.toInt() - CalculatorButton.MINUS -> result - entry.toInt() - CalculatorButton.DIVISION -> result / entry.toInt() - CalculatorButton.MULTIPLY -> result * entry.toInt() - else -> throw IllegalStateException("Invalid last operator") - } + private fun calculate(entries: List): Double { + var a = 0.0 + + if (entries.size == 1) { + return getEntryWithPercentFactor(entries[0]) + } + + for (i in entries.indices) { + if (isOperator(entries[i])) continue + + if (i == 0) { + a = when (entries[i + 1]) { + PLUS.tag, MINUS.tag -> getEntryWithPercentFactor(entries[0]) + MULTIPLY.tag, DIVISION.tag -> getEntryWithPercentFactor(entries[0]) + else -> throw IllegalStateException("Error solving first entry") + } + continue + } + + if (i == entries.lastIndex) { + when (entries[i - 1]) { + PLUS.tag -> a += getEntryWithPercentFactor(entries[i], a) + MINUS.tag -> a -= getEntryWithPercentFactor(entries[i], a) + MULTIPLY.tag -> a *= getEntryWithPercentFactor(entries[i]) + DIVISION.tag -> a /= getEntryWithPercentFactor(entries[i]) + else -> throw IllegalStateException("Error solving last entry") + } + return a + } + + when (entries[i + 1]) { + PLUS.tag -> when (entries[i - 1]) { + PLUS.tag -> a += getEntryWithPercentFactor(entries[i], a) + MINUS.tag -> a -= getEntryWithPercentFactor(entries[i], a) + MULTIPLY.tag -> a *= getEntryWithPercentFactor(entries[i]) + DIVISION.tag -> a /= getEntryWithPercentFactor(entries[i]) + else -> throw IllegalStateException("Error checking plus tag") + } + MINUS.tag -> when (entries[i - 1]) { + PLUS.tag -> a += getEntryWithPercentFactor(entries[i], a) + MINUS.tag -> a -= getEntryWithPercentFactor(entries[i], a) + MULTIPLY.tag -> a *= getEntryWithPercentFactor(entries[i]) + DIVISION.tag -> a /= getEntryWithPercentFactor(entries[i]) + else -> throw IllegalStateException("Error checking minus tag") } - OperatorUtils.isOperator(entry) -> { - lastOperator = OperatorUtils.getOperator(entry) + MULTIPLY.tag -> when (entries[i - 1]) { + PLUS.tag -> a += getEntryWithPercentFactor(entries[i]) + MINUS.tag -> a -= getEntryWithPercentFactor(entries[i]) + MULTIPLY.tag -> a *= getEntryWithPercentFactor(entries[i]) + DIVISION.tag -> a /= getEntryWithPercentFactor(entries[i]) + else -> throw IllegalStateException("Error checking multiply tag") } - else -> throw IllegalStateException("Invalid set of entries") + DIVISION.tag -> when (entries[i - 1]) { + PLUS.tag -> a += getEntryWithPercentFactor(entries[i]) + MINUS.tag -> a -= getEntryWithPercentFactor(entries[i]) + MULTIPLY.tag -> a *= getEntryWithPercentFactor(entries[i]) + DIVISION.tag -> a /= getEntryWithPercentFactor(entries[i]) + else -> throw IllegalStateException("Error checking division tag") + } + else -> throw IllegalStateException("Error checking next operator tag") } } - return result + throw IllegalStateException("Error solving equation") + } + + private fun getEntryWithPercentFactor(entry: String, baseNumber: Double): Double { + return if (entry.endsWith(PERCENT.tag)) { + baseNumber * entry.trimEndChar().toDouble() / 100.0 + } else { + entry.toDouble() + } + } + + private fun getEntryWithPercentFactor(entry:String): Double { + return if (entry.endsWith(PERCENT.tag)) { + entry.trimEndChar().toDouble() / 100.0 + } else { + entry.toDouble() + } } } \ No newline at end of file