Skip to content

Commit

Permalink
Fix #1
Browse files Browse the repository at this point in the history
all the operations are working!
but we still can't concatenate operations properly
  • Loading branch information
Stalkerfish committed Dec 14, 2022
1 parent d09eeec commit 80808fd
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/src/main/java/com/stalkerfish/thecalculator/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stalkerfish.thecalculator

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
Expand Down Expand Up @@ -52,6 +53,7 @@ class MainActivity : AppCompatActivity() {
}
}

@SuppressLint("SetTextI18n")
fun onEqual(view: View){
if (lastWasNumeric){ // checks if the last digit is a number, so it can perform the operation

Expand Down Expand Up @@ -79,6 +81,45 @@ class MainActivity : AppCompatActivity() {

tvInput?.text = (one.toDouble() - two.toDouble()).toString() // convert the strings to Double, do the operation,
// convert back to strings, and set to tvInput, all at the same time
} else if (tvValue.contains("+")){

val splitValue = tvValue.split("+") // Split the string using the operator sign as a delimiter

var one = splitValue[0] // the split string comes in arrays
var two = splitValue[1]

if(prefix.isNotEmpty()){
one = prefix + one
}

tvInput?.text = (one.toDouble() + two.toDouble()).toString() // convert the strings to Double, do the operation,
// convert back to strings, and set to tvInput, all at the same time
} else if (tvValue.contains("/")){

val splitValue = tvValue.split("/") // Split the string using the operator sign as a delimiter

var one = splitValue[0] // the split string comes in arrays
var two = splitValue[1]

if(prefix.isNotEmpty()){
one = prefix + one
}

tvInput?.text = (one.toDouble() / two.toDouble()).toString() // convert the strings to Double, do the operation,
// convert back to strings, and set to tvInput, all at the same time
} else if (tvValue.contains("*")){

val splitValue = tvValue.split("*") // Split the string using the operator sign as a delimiter

var one = splitValue[0] // the split string comes in arrays
var two = splitValue[1]

if(prefix.isNotEmpty()){
one = prefix + one
}

tvInput?.text = (one.toDouble() * two.toDouble()).toString() // convert the strings to Double, do the operation,
// convert back to strings, and set to tvInput, all at the same time
}

}catch (e: ArithmeticException){
Expand Down

0 comments on commit 80808fd

Please sign in to comment.