Merge pull request #107 from dalampira/creation-of-factorial

Scientific Calculator
This commit is contained in:
Tibor Kaputa 2018-10-08 11:51:04 +02:00 committed by GitHub
commit ea42c550f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 394 additions and 5 deletions

View file

@ -24,6 +24,11 @@
<activity
android:name=".activities.MainActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".activities.ScientificActivity"
android:screenOrientation="portrait"
android:parentActivityName=".activities.MainActivity"
android:label="@string/app_launcher_name"/>
<activity
android:name=".activities.WidgetConfigureActivity"

View file

@ -91,6 +91,7 @@ class MainActivity : SimpleActivity(), Calculator {
when (item.itemId) {
R.id.settings -> launchSettings()
R.id.about -> launchAbout()
R.id.scientific -> launchScientific()
else -> return super.onOptionsItemSelected(item)
}
return true
@ -111,6 +112,9 @@ class MainActivity : SimpleActivity(), Calculator {
private fun launchSettings() {
startActivity(Intent(applicationContext, SettingsActivity::class.java))
}
private fun launchScientific() {
startActivity(Intent(applicationContext, ScientificActivity::class.java))
}
private fun launchAbout() {
val licenses = LICENSE_AUTOFITTEXTVIEW or LICENSE_ROBOLECTRIC or LICENSE_ESPRESSO

View file

@ -0,0 +1,19 @@
package com.simplemobiletools.calculator.activities
import android.os.Bundle
import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.extensions.updateViewColors
import kotlinx.android.synthetic.main.activity_main.*
import com.simplemobiletools.calculator.BuildConfig
import com.simplemobiletools.commons.extensions.*
class ScientificActivity : SimpleActivity() {
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.scientific_activity)
appLaunched(BuildConfig.APPLICATION_ID)
updateViewColors(calculator_holder, config.textColor)
}
}

View file

@ -111,7 +111,7 @@ class WidgetConfigureActivity : SimpleActivity() {
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_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_9, R.id.btn_percent, R.id.btn_power, R.id.btn_root, R.id.btn_factorial, 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

@ -58,6 +58,8 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
if (sign == "") {
setFormula(sign + first)
} else if (sign == "!"){
setFormula(first + sign)
} else if (!sign.isEmpty()) {
var formula = first + sign + second
if (mWasPercentLast) {
@ -102,6 +104,11 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
calculateResult()
}
private fun handleFactorial() {
mBaseValue = getDisplayedNumberAsDouble()
calculateResult()
}
private fun calculateResult() {
updateFormula()
if (mWasPercentLast) {
@ -118,7 +125,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
fun handleOperation(operation: String) {
mWasPercentLast = operation == PERCENT
if (lastKey == DIGIT && operation != ROOT) {
if (lastKey == DIGIT && operation != ROOT && operation!= FACTORIAL) {
handleResult()
}
@ -130,6 +137,10 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
handleRoot()
mResetValue = false
}
if (operation == FACTORIAL) {
handleFactorial()
mResetValue = false
}
}
fun handleClear() {
@ -195,6 +206,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
PERCENT -> "%"
POWER -> "^"
ROOT -> ""
FACTORIAL -> "!"
else -> ""
}

View file

@ -12,6 +12,7 @@ const val ROOT = "root"
const val DECIMAL = "decimal"
const val CLEAR = "clear"
const val RESET = "reset"
const val FACTORIAL = "factorial"
const val NAN = "NaN"
const val ZERO = "zero"

View file

@ -48,6 +48,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
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, FACTORIAL, R.id.btn_factorial )
setupIntent(context, views, CLEAR, R.id.btn_clear)
setupIntent(context, views, RESET, R.id.btn_reset)
@ -80,7 +81,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_percent, 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_factorial, 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 +92,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, PERCENT, 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, FACTORIAL -> myAction(action, context)
else -> super.onReceive(context, intent)
}
}
@ -116,7 +117,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
EQUALS -> calc!!.handleEquals()
CLEAR -> calc!!.handleClear()
RESET -> calc!!.handleReset()
PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT -> calc!!.handleOperation(action)
PLUS, MINUS, MULTIPLY, DIVIDE, PERCENT, POWER, ROOT, FACTORIAL -> calc!!.handleOperation(action)
}
}

View file

@ -0,0 +1,20 @@
package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.Operation
import com.simplemobiletools.calculator.operation.base.UnaryOperation
class FactorialOperation(value: Double) : UnaryOperation(value), Operation {
override fun getResult(): Double{
var result = 1.0
if (value==0.0 || value==1.0){
return result
}else{
var base = value.toInt()
for(i in 1..base){
result *= i
}
}
return result
}
}

View file

@ -14,6 +14,7 @@ object OperationFactory {
PERCENT -> PercentOperation(baseValue, secondValue)
POWER -> PowerOperation(baseValue, secondValue)
ROOT -> RootOperation(baseValue)
FACTORIAL -> FactorialOperation(baseValue)
else -> null
}
}

View file

@ -0,0 +1,320 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/calculator_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.MainActivity"
tools:ignore="HardcodedText">
<TextView
android:id="@+id/formula"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.1"
android:fontFamily="sans-serif-light"
android:gravity="right|bottom"
android:maxLines="1"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:textSize="@dimen/formula_text_size"/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.8"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical|right"
android:maxLines="1"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:textSize="@dimen/display_text_size"
android:text="0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<Button
android:id="@+id/btn_cos"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="cos"/>
<Button
android:id="@+id/btn_sin"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="sin"/>
<Button
android:id="@+id/btn_tan"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="tan"/>
<Button
android:id="@+id/btn_parenthesis1"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="("/>
<Button
android:id="@+id/btn_parenthesis2"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text=")"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<Button
android:id="@+id/btn_factorial"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="!"/>
<Button
android:id="@+id/btn_percent"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="%"/>
<Button
android:id="@+id/btn_power"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="^"/>
<Button
android:id="@+id/btn_root"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="√"/>
<Button
android:id="@+id/btn_clear"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"/>
<Button
android:id="@+id/btn_reset"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="AC"
android:visibility="gone"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<Button
android:id="@+id/log"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="log"/>
<Button
android:id="@+id/btn_7"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"/>
<Button
android:id="@+id/btn_8"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"/>
<Button
android:id="@+id/btn_9"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"/>
<Button
android:id="@+id/btn_divide"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="÷"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<Button
android:id="@+id/ln"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="ln"/>
<Button
android:id="@+id/btn_4"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"/>
<Button
android:id="@+id/btn_5"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"/>
<Button
android:id="@+id/btn_6"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"/>
<Button
android:id="@+id/btn_multiply"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="*"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<Button
android:id="@+id/btn_pi"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="π"/>
<Button
android:id="@+id/btn_1"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"/>
<Button
android:id="@+id/btn_2"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"/>
<Button
android:id="@+id/btn_3"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"/>
<Button
android:id="@+id/btn_minus"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<Button
android:id="@+id/btn_e_napier"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="e"/>
<Button
android:id="@+id/btn_0"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"/>
<Button
android:id="@+id/btn_decimal"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."/>
<Button
android:id="@+id/btn_equals"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="="/>
<Button
android:id="@+id/btn_plus"
style="@style/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"/>
</LinearLayout>
</LinearLayout>

View file

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/scientific"
android:title="Scientific Calculator"
app:showAsAction="never"/>
<item
android:id="@+id/settings"
android:title="@string/settings"

View file

@ -5,6 +5,7 @@
<item name="android:background">?android:attr/selectableItemBackgroundBorderless</item>
<item name="android:textSize">@dimen/button_text_size</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textAllCaps">false</item>
</style>
</resources>

View file

@ -6,6 +6,7 @@
<item name="android:background">@drawable/button</item>
<item name="android:textSize">@dimen/button_text_size</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textAllCaps">false</item>
</style>
</resources>