mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-04-08 07:21:50 +02:00
fix #29, replace modulo with percent
This commit is contained in:
parent
d388cd0fc4
commit
be0dea16ae
@ -102,13 +102,15 @@ public class MainActivityTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void moduloTest() {
|
public void percentTest() {
|
||||||
press(R.id.btn_7);
|
press(R.id.btn_1);
|
||||||
press(R.id.btn_modulo);
|
press(R.id.btn_0);
|
||||||
|
press(R.id.btn_percent);
|
||||||
press(R.id.btn_2);
|
press(R.id.btn_2);
|
||||||
|
press(R.id.btn_0);
|
||||||
press(R.id.btn_equals);
|
press(R.id.btn_equals);
|
||||||
checkResult("1");
|
checkResult("2");
|
||||||
checkFormula("7%2");
|
checkFormula("10%20");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -173,32 +175,25 @@ public class MainActivityTest {
|
|||||||
checkResult("4");
|
checkResult("4");
|
||||||
checkFormula("7-3");
|
checkFormula("7-3");
|
||||||
|
|
||||||
press(R.id.btn_5);
|
press(R.id.btn_1);
|
||||||
|
press(R.id.btn_0);
|
||||||
press(R.id.btn_divide);
|
press(R.id.btn_divide);
|
||||||
checkResult("20");
|
checkResult("40");
|
||||||
checkFormula("4*5");
|
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_2);
|
||||||
press(R.id.btn_modulo);
|
press(R.id.btn_percent);
|
||||||
checkResult("10");
|
checkResult("64");
|
||||||
checkFormula("20/2");
|
checkFormula("8^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_root);
|
press(R.id.btn_root);
|
||||||
checkResult("16");
|
checkResult("8");
|
||||||
checkFormula("√256");
|
checkFormula("√64");
|
||||||
|
|
||||||
press(R.id.btn_clear);
|
|
||||||
checkResult("1");
|
|
||||||
|
|
||||||
press(R.id.btn_clear);
|
press(R.id.btn_clear);
|
||||||
checkResult("0");
|
checkResult("0");
|
||||||
|
@ -38,7 +38,7 @@ class MainActivity : SimpleActivity(), Calculator {
|
|||||||
btn_minus.setOnClickListener { calc.handleOperation(MINUS); checkHaptic(it) }
|
btn_minus.setOnClickListener { calc.handleOperation(MINUS); checkHaptic(it) }
|
||||||
btn_multiply.setOnClickListener { calc.handleOperation(MULTIPLY); checkHaptic(it) }
|
btn_multiply.setOnClickListener { calc.handleOperation(MULTIPLY); checkHaptic(it) }
|
||||||
btn_divide.setOnClickListener { calc.handleOperation(DIVIDE); 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_power.setOnClickListener { calc.handleOperation(POWER); checkHaptic(it) }
|
||||||
btn_root.setOnClickListener { calc.handleOperation(ROOT); checkHaptic(it) }
|
btn_root.setOnClickListener { calc.handleOperation(ROOT); checkHaptic(it) }
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ class WidgetConfigureActivity : SimpleActivity() {
|
|||||||
config_text_color.setBackgroundColor(mTextColor)
|
config_text_color.setBackgroundColor(mTextColor)
|
||||||
config_save.setTextColor(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)
|
result.setTextColor(mTextColor)
|
||||||
formula.setTextColor(mTextColor)
|
formula.setTextColor(mTextColor)
|
||||||
|
|
||||||
|
@ -112,8 +112,9 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun handleOperation(operation: String) {
|
fun handleOperation(operation: String) {
|
||||||
if (lastKey == DIGIT && operation != ROOT)
|
if (lastKey == DIGIT && operation != ROOT) {
|
||||||
handleResult()
|
handleResult()
|
||||||
|
}
|
||||||
|
|
||||||
mResetValue = true
|
mResetValue = true
|
||||||
lastKey = operation
|
lastKey = operation
|
||||||
@ -175,8 +176,9 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
|
|
||||||
private fun zeroClicked() {
|
private fun zeroClicked() {
|
||||||
val value = displayedNumber
|
val value = displayedNumber
|
||||||
if (value != "0")
|
if (value != "0") {
|
||||||
addDigit(0)
|
addDigit(0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSign(lastOperation: String?) = when (lastOperation) {
|
private fun getSign(lastOperation: String?) = when (lastOperation) {
|
||||||
@ -184,7 +186,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||||||
MINUS -> "-"
|
MINUS -> "-"
|
||||||
MULTIPLY -> "*"
|
MULTIPLY -> "*"
|
||||||
DIVIDE -> "/"
|
DIVIDE -> "/"
|
||||||
MODULO -> "%"
|
PERCENT -> "%"
|
||||||
POWER -> "^"
|
POWER -> "^"
|
||||||
ROOT -> "√"
|
ROOT -> "√"
|
||||||
else -> ""
|
else -> ""
|
||||||
|
@ -1,29 +1,26 @@
|
|||||||
package com.simplemobiletools.calculator.helpers
|
package com.simplemobiletools.calculator.helpers
|
||||||
|
|
||||||
val DIGIT = "digit"
|
const val DIGIT = "digit"
|
||||||
val EQUALS = "equals"
|
const val EQUALS = "equals"
|
||||||
val PLUS = "plus"
|
const val PLUS = "plus"
|
||||||
val MINUS = "minus"
|
const val MINUS = "minus"
|
||||||
val MULTIPLY = "multiply"
|
const val MULTIPLY = "multiply"
|
||||||
val DIVIDE = "divide"
|
const val DIVIDE = "divide"
|
||||||
val MODULO = "modulo"
|
const val PERCENT = "percent"
|
||||||
val POWER = "power"
|
const val POWER = "power"
|
||||||
val ROOT = "root"
|
const val ROOT = "root"
|
||||||
val DECIMAL = "decimal"
|
const val DECIMAL = "decimal"
|
||||||
val CLEAR = "clear"
|
const val CLEAR = "clear"
|
||||||
val RESET = "reset"
|
const val RESET = "reset"
|
||||||
|
|
||||||
val NAN = "NaN"
|
const val NAN = "NaN"
|
||||||
val ZERO = "zero"
|
const val ZERO = "zero"
|
||||||
val ONE = "one"
|
const val ONE = "one"
|
||||||
val TWO = "two"
|
const val TWO = "two"
|
||||||
val THREE = "three"
|
const val THREE = "three"
|
||||||
val FOUR = "four"
|
const val FOUR = "four"
|
||||||
val FIVE = "five"
|
const val FIVE = "five"
|
||||||
val SIX = "six"
|
const val SIX = "six"
|
||||||
val SEVEN = "seven"
|
const val SEVEN = "seven"
|
||||||
val EIGHT = "eight"
|
const val EIGHT = "eight"
|
||||||
val NINE = "nine"
|
const val NINE = "nine"
|
||||||
|
|
||||||
// shared preferences
|
|
||||||
val VIBRATE_ON_BUTTON_PRESS = "vibrate_on_button_press"
|
|
||||||
|
@ -45,7 +45,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
|
|||||||
setupIntent(context, views, MINUS, R.id.btn_minus)
|
setupIntent(context, views, MINUS, R.id.btn_minus)
|
||||||
setupIntent(context, views, MULTIPLY, R.id.btn_multiply)
|
setupIntent(context, views, MULTIPLY, R.id.btn_multiply)
|
||||||
setupIntent(context, views, DIVIDE, R.id.btn_divide)
|
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, POWER, R.id.btn_power)
|
||||||
setupIntent(context, views, ROOT, R.id.btn_root)
|
setupIntent(context, views, ROOT, R.id.btn_root)
|
||||||
setupIntent(context, views, CLEAR, R.id.btn_clear)
|
setupIntent(context, views, CLEAR, R.id.btn_clear)
|
||||||
@ -80,7 +80,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
|
|||||||
|
|
||||||
private fun updateTextColors(views: RemoteViews, color: Int) {
|
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,
|
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)
|
R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals)
|
||||||
|
|
||||||
for (i in viewIds) {
|
for (i in viewIds) {
|
||||||
@ -91,7 +91,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
|
|||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
val action = intent.action
|
val action = intent.action
|
||||||
when (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)
|
else -> super.onReceive(context, intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +116,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
|
|||||||
EQUALS -> calc!!.handleEquals()
|
EQUALS -> calc!!.handleEquals()
|
||||||
CLEAR -> calc!!.handleClear()
|
CLEAR -> calc!!.handleClear()
|
||||||
RESET -> calc!!.handleReset()
|
RESET -> calc!!.handleReset()
|
||||||
PLUS, MINUS, MULTIPLY, DIVIDE, MODULO, POWER, ROOT -> calc!!.handleOperation(action)
|
PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT -> calc!!.handleOperation(action)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,15 +6,15 @@ import com.simplemobiletools.calculator.operation.base.Operation
|
|||||||
object OperationFactory {
|
object OperationFactory {
|
||||||
|
|
||||||
fun forId(id: String, baseValue: Double, secondValue: Double): Operation? {
|
fun forId(id: String, baseValue: Double, secondValue: Double): Operation? {
|
||||||
when (id) {
|
return when (id) {
|
||||||
PLUS -> return PlusOperation(baseValue, secondValue)
|
PLUS -> PlusOperation(baseValue, secondValue)
|
||||||
MINUS -> return MinusOperation(baseValue, secondValue)
|
MINUS -> MinusOperation(baseValue, secondValue)
|
||||||
DIVIDE -> return DivideOperation(baseValue, secondValue)
|
DIVIDE -> DivideOperation(baseValue, secondValue)
|
||||||
MULTIPLY -> return MultiplyOperation(baseValue, secondValue)
|
MULTIPLY -> MultiplyOperation(baseValue, secondValue)
|
||||||
MODULO -> return ModuloOperation(baseValue, secondValue)
|
PERCENT -> PercentOperation(baseValue, secondValue)
|
||||||
POWER -> return PowerOperation(baseValue, secondValue)
|
POWER -> PowerOperation(baseValue, secondValue)
|
||||||
ROOT -> return RootOperation(baseValue)
|
ROOT -> RootOperation(baseValue)
|
||||||
else -> return null
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,12 @@ package com.simplemobiletools.calculator.operation
|
|||||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
||||||
import com.simplemobiletools.calculator.operation.base.Operation
|
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 {
|
override fun getResult(): Double {
|
||||||
var result = 0.0
|
var result = 0.0
|
||||||
if (secondValue != 0.0) {
|
if (secondValue != 0.0) {
|
||||||
result = baseValue % secondValue
|
result = (baseValue / 100) * secondValue
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
@ -41,13 +41,12 @@
|
|||||||
android:orientation="horizontal">
|
android:orientation="horizontal">
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_modulo"
|
android:id="@+id/btn_percent"
|
||||||
style="@style/MyButton"
|
style="@style/MyButton"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:text="mod"
|
android:text="%"
|
||||||
android:textAllCaps="false"
|
|
||||||
android:textSize="@dimen/extra_big_text_size"/>
|
android:textSize="@dimen/extra_big_text_size"/>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
|
Loading…
x
Reference in New Issue
Block a user