Finish Calc

This commit is contained in:
icewithcola 2024-04-09 12:53:00 +08:00
parent 940552ab62
commit f59f4fb783
4 changed files with 102 additions and 6 deletions

View file

@ -1,7 +1,104 @@
package uk.kagurach.android101
import android.content.Context
import kotlin.math.pow
fun Kaculate(src: String,ctx :Context) : String{
return ""
if (src.isBlank()){
return src
}
val OpList = charArrayOf('+','-','x','/','^')
var numStack = floatArrayOf()
var opStack = charArrayOf()
var _curnum = ""
var _immediatePop = false
for (i in 0..<src.length){
if (src[i] in '0'..'9' || src[i] == '.'){
_curnum += src[i]
}
else if (src[i] in OpList){
try {
numStack += _curnum.toFloat()
_curnum = ""
}catch (e:Exception){
ToastHelper.ShowToast(e.toString(),ctx)
return src
}
if (_immediatePop){
try {
val exp = numStack.last()
numStack.dropLast(1)
val num = numStack.last()
numStack.dropLast(1)
numStack += num.pow(exp)
_immediatePop = false
}catch (e:Exception){
ToastHelper.ShowToast(e.toString(),ctx)
return src
}
}
if (src[i] == '^'){
_immediatePop = true
}else{
opStack += src[i]
}
}
else{
ToastHelper.ShowToast("Unknown input char:" + src[i].toString(),ctx);
return src
}
}
if (_curnum.isNotEmpty()){
try {
numStack += _curnum.toFloat()
_curnum = ""
}catch (e:Exception){
ToastHelper.ShowToast(e.toString(),ctx)
return src
}
}
if (numStack.size != opStack.size+1 ){
ToastHelper.ShowToast("Ops and nums Not Match!",ctx)
return src
}
var intStackCurr = 0
for (i in 0..<opStack.size){
if (opStack[i] == 'x'){
val a = numStack[intStackCurr]
val b = numStack[intStackCurr+1]
numStack[intStackCurr] = a*b
numStack.drop(intStackCurr+1)
opStack.drop(i)
}else if (opStack[i] == '/'){
val a = numStack[intStackCurr]
val b = numStack[intStackCurr+1]
numStack[intStackCurr] = a/b
numStack.drop(intStackCurr+1)
opStack.drop(i)
}else{
intStackCurr ++
}
}
if (opStack.size!=0) {
for (i in opStack) {
if (i == '+') {
numStack[0] += numStack[1]
numStack.drop(1)
} else {
numStack[0] -= numStack[1]
numStack.drop(1)
}
}
}
return numStack[0].toString()
}

View file

@ -38,7 +38,7 @@ public class MainActivity extends AppCompatActivity {
super.onResume();
TextView tv = findViewById(R.id.tv_hello);
textViewAppendString(tv,"Starting Kagura Init");
textViewAppendString(tv,"Starting Initialize");
if (Build.VERSION.SDK_INT >= 33) {
textViewAppendString(tv,"Find api > 33, checking permission");
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {

View file

@ -13,7 +13,6 @@ import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import uk.kagurach.android101.KaculateKt;
public class Page3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -43,7 +42,7 @@ public class Page3 extends AppCompatActivity {
findViewById(R.id.P3Dot).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3CE).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3ChengFang).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3Result).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3EqualButton).setOnClickListener(new CalculateOnClickListener());
}
@ -74,7 +73,7 @@ public class Page3 extends AppCompatActivity {
}
private void calc(){
TextView textView = findViewById(R.id.P3Result);
TextView textView = findViewById(R.id.P3CalcResult);
String result = Kaculate(textView.getText().toString(),this);
textView.setText(result);
}

View file

@ -189,7 +189,7 @@
android:text="." />
<Button
android:id="@+id/P3Result"
android:id="@+id/P3EqualButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"