fix #29, replace modulo with percent

This commit is contained in:
tibbi 2018-04-03 23:26:34 +02:00
parent d388cd0fc4
commit be0dea16ae
9 changed files with 68 additions and 75 deletions

View File

@ -102,13 +102,15 @@ public class MainActivityTest {
}
@Test
public void moduloTest() {
press(R.id.btn_7);
press(R.id.btn_modulo);
public void percentTest() {
press(R.id.btn_1);
press(R.id.btn_0);
press(R.id.btn_percent);
press(R.id.btn_2);
press(R.id.btn_0);
press(R.id.btn_equals);
checkResult("1");
checkFormula("7%2");
checkResult("2");
checkFormula("10%20");
}
@Test
@ -173,32 +175,25 @@ public class MainActivityTest {
checkResult("4");
checkFormula("7-3");
press(R.id.btn_5);
press(R.id.btn_1);
press(R.id.btn_0);
press(R.id.btn_divide);
checkResult("20");
checkFormula("4*5");
checkResult("40");
checkFormula("4*10");
press(R.id.btn_5);
press(R.id.btn_power);
checkResult("8");
checkFormula("40/5");
press(R.id.btn_2);
press(R.id.btn_modulo);
checkResult("10");
checkFormula("20/2");
press(R.id.btn_4);
press(R.id.btn_power);
checkResult("2");
checkFormula("10%4");
press(R.id.btn_8);
press(R.id.btn_modulo);
checkResult("256");
checkFormula("2^8");
press(R.id.btn_percent);
checkResult("64");
checkFormula("8^2");
press(R.id.btn_root);
checkResult("16");
checkFormula("√256");
press(R.id.btn_clear);
checkResult("1");
checkResult("8");
checkFormula("√64");
press(R.id.btn_clear);
checkResult("0");

View File

@ -38,7 +38,7 @@ class MainActivity : SimpleActivity(), Calculator {
btn_minus.setOnClickListener { calc.handleOperation(MINUS); checkHaptic(it) }
btn_multiply.setOnClickListener { calc.handleOperation(MULTIPLY); checkHaptic(it) }
btn_divide.setOnClickListener { calc.handleOperation(DIVIDE); checkHaptic(it) }
btn_modulo.setOnClickListener { calc.handleOperation(MODULO); checkHaptic(it) }
btn_percent.setOnClickListener { calc.handleOperation(PERCENT); checkHaptic(it) }
btn_power.setOnClickListener { calc.handleOperation(POWER); checkHaptic(it) }
btn_root.setOnClickListener { calc.handleOperation(ROOT); checkHaptic(it) }

View File

@ -107,7 +107,7 @@ class WidgetConfigureActivity : SimpleActivity() {
config_text_color.setBackgroundColor(mTextColor)
config_save.setTextColor(mTextColor)
val viewIds = intArrayOf(R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9, R.id.btn_modulo, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide, R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals)
val viewIds = intArrayOf(R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9, R.id.btn_percent, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide, R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals)
result.setTextColor(mTextColor)
formula.setTextColor(mTextColor)

View File

@ -112,8 +112,9 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
}
fun handleOperation(operation: String) {
if (lastKey == DIGIT && operation != ROOT)
if (lastKey == DIGIT && operation != ROOT) {
handleResult()
}
mResetValue = true
lastKey = operation
@ -175,8 +176,9 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
private fun zeroClicked() {
val value = displayedNumber
if (value != "0")
if (value != "0") {
addDigit(0)
}
}
private fun getSign(lastOperation: String?) = when (lastOperation) {
@ -184,7 +186,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
MINUS -> "-"
MULTIPLY -> "*"
DIVIDE -> "/"
MODULO -> "%"
PERCENT -> "%"
POWER -> "^"
ROOT -> ""
else -> ""

View File

@ -1,29 +1,26 @@
package com.simplemobiletools.calculator.helpers
val DIGIT = "digit"
val EQUALS = "equals"
val PLUS = "plus"
val MINUS = "minus"
val MULTIPLY = "multiply"
val DIVIDE = "divide"
val MODULO = "modulo"
val POWER = "power"
val ROOT = "root"
val DECIMAL = "decimal"
val CLEAR = "clear"
val RESET = "reset"
const val DIGIT = "digit"
const val EQUALS = "equals"
const val PLUS = "plus"
const val MINUS = "minus"
const val MULTIPLY = "multiply"
const val DIVIDE = "divide"
const val PERCENT = "percent"
const val POWER = "power"
const val ROOT = "root"
const val DECIMAL = "decimal"
const val CLEAR = "clear"
const val RESET = "reset"
val NAN = "NaN"
val ZERO = "zero"
val ONE = "one"
val TWO = "two"
val THREE = "three"
val FOUR = "four"
val FIVE = "five"
val SIX = "six"
val SEVEN = "seven"
val EIGHT = "eight"
val NINE = "nine"
// shared preferences
val VIBRATE_ON_BUTTON_PRESS = "vibrate_on_button_press"
const val NAN = "NaN"
const val ZERO = "zero"
const val ONE = "one"
const val TWO = "two"
const val THREE = "three"
const val FOUR = "four"
const val FIVE = "five"
const val SIX = "six"
const val SEVEN = "seven"
const val EIGHT = "eight"
const val NINE = "nine"

View File

@ -45,7 +45,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
setupIntent(context, views, MINUS, R.id.btn_minus)
setupIntent(context, views, MULTIPLY, R.id.btn_multiply)
setupIntent(context, views, DIVIDE, R.id.btn_divide)
setupIntent(context, views, MODULO, R.id.btn_modulo)
setupIntent(context, views, PERCENT, R.id.btn_percent)
setupIntent(context, views, POWER, R.id.btn_power)
setupIntent(context, views, ROOT, R.id.btn_root)
setupIntent(context, views, CLEAR, R.id.btn_clear)
@ -80,7 +80,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
private fun updateTextColors(views: RemoteViews, color: Int) {
val viewIds = intArrayOf(R.id.formula, R.id.result, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6,
R.id.btn_7, R.id.btn_8, R.id.btn_9, R.id.btn_modulo, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide,
R.id.btn_7, R.id.btn_8, R.id.btn_9, R.id.btn_percent, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide,
R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals)
for (i in viewIds) {
@ -91,7 +91,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
when (action) {
DECIMAL, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, EQUALS, CLEAR, RESET, PLUS, MINUS, MULTIPLY, DIVIDE, MODULO, POWER, ROOT -> myAction(action, context)
DECIMAL, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, EQUALS, CLEAR, RESET, PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT -> myAction(action, context)
else -> super.onReceive(context, intent)
}
}
@ -116,7 +116,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
EQUALS -> calc!!.handleEquals()
CLEAR -> calc!!.handleClear()
RESET -> calc!!.handleReset()
PLUS, MINUS, MULTIPLY, DIVIDE, MODULO, POWER, ROOT -> calc!!.handleOperation(action)
PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT -> calc!!.handleOperation(action)
}
}

View File

@ -6,15 +6,15 @@ import com.simplemobiletools.calculator.operation.base.Operation
object OperationFactory {
fun forId(id: String, baseValue: Double, secondValue: Double): Operation? {
when (id) {
PLUS -> return PlusOperation(baseValue, secondValue)
MINUS -> return MinusOperation(baseValue, secondValue)
DIVIDE -> return DivideOperation(baseValue, secondValue)
MULTIPLY -> return MultiplyOperation(baseValue, secondValue)
MODULO -> return ModuloOperation(baseValue, secondValue)
POWER -> return PowerOperation(baseValue, secondValue)
ROOT -> return RootOperation(baseValue)
else -> return null
return when (id) {
PLUS -> PlusOperation(baseValue, secondValue)
MINUS -> MinusOperation(baseValue, secondValue)
DIVIDE -> DivideOperation(baseValue, secondValue)
MULTIPLY -> MultiplyOperation(baseValue, secondValue)
PERCENT -> PercentOperation(baseValue, secondValue)
POWER -> PowerOperation(baseValue, secondValue)
ROOT -> RootOperation(baseValue)
else -> null
}
}
}

View File

@ -3,12 +3,12 @@ package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation
class ModuloOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
class PercentOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult(): Double {
var result = 0.0
if (secondValue != 0.0) {
result = baseValue % secondValue
result = (baseValue / 100) * secondValue
}
return result
}

View File

@ -41,13 +41,12 @@
android:orientation="horizontal">
<Button
android:id="@+id/btn_modulo"
android:id="@+id/btn_percent"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="mod"
android:textAllCaps="false"
android:text="%"
android:textSize="@dimen/extra_big_text_size"/>
<Button