P3 finished

This commit is contained in:
icewithcola 2024-04-09 15:22:52 +08:00
parent f59f4fb783
commit a348085743
8 changed files with 383 additions and 218 deletions

View file

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
@ -12,6 +14,9 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.Android101" android:theme="@style/Theme.Android101"
tools:targetApi="31"> tools:targetApi="31">
<activity
android:name=".Page4"
android:exported="false" />
<activity <activity
android:name=".Page3" android:name=".Page3"
android:exported="false" /> android:exported="false" />

View file

@ -14,8 +14,7 @@ fun Kaculate(src: String,ctx :Context) : String{
var _curnum = "" var _curnum = ""
var _immediatePop = false for (i in src.indices){
for (i in 0..<src.length){
if (src[i] in '0'..'9' || src[i] == '.'){ if (src[i] in '0'..'9' || src[i] == '.'){
_curnum += src[i] _curnum += src[i]
} }
@ -27,28 +26,8 @@ fun Kaculate(src: String,ctx :Context) : String{
ToastHelper.ShowToast(e.toString(),ctx) ToastHelper.ShowToast(e.toString(),ctx)
return src 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] opStack += src[i]
} }
}
else{ else{
ToastHelper.ShowToast("Unknown input char:" + src[i].toString(),ctx); ToastHelper.ShowToast("Unknown input char:" + src[i].toString(),ctx);
return src return src
@ -58,7 +37,6 @@ fun Kaculate(src: String,ctx :Context) : String{
if (_curnum.isNotEmpty()){ if (_curnum.isNotEmpty()){
try { try {
numStack += _curnum.toFloat() numStack += _curnum.toFloat()
_curnum = ""
}catch (e:Exception){ }catch (e:Exception){
ToastHelper.ShowToast(e.toString(),ctx) ToastHelper.ShowToast(e.toString(),ctx)
return src return src
@ -70,35 +48,80 @@ fun Kaculate(src: String,ctx :Context) : String{
return src return src
} }
// ^
if ('^' in opStack){
var intStackCurr = 0 var intStackCurr = 0
for (i in 0..<opStack.size){ for (i in opStack.indices){
if (opStack[i] == 'x'){ if (opStack[i]!='^'){
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 ++ intStackCurr ++
continue
}
val a = numStack[intStackCurr]
val b = numStack[intStackCurr+1]
numStack[intStackCurr] = a.pow(b)
numStack = dropAtIdx(numStack,intStackCurr+1)
} }
} }
if (opStack.size!=0) { // x and /
var intStackCurr = 0
for (i in opStack.indices){
if (opStack[i]!='x'&&opStack[i]!='/'){
intStackCurr ++
continue
}
val a = numStack[intStackCurr]
val b = numStack[intStackCurr+1]
if (opStack[i] == 'x'){
numStack[intStackCurr] = a*b
}else{
numStack[intStackCurr] = a/b
}
numStack = dropAtIdx(numStack,intStackCurr+1)
}
// + and -
if (opStack.isNotEmpty()) {
for (i in opStack) { for (i in opStack) {
if (i == '+') { if (i == '+') {
numStack[0] += numStack[1] numStack[0] += numStack[1]
numStack.drop(1) numStack = dropAtIdx(numStack,1)
} else { } else if (i=='-') {
numStack[0] -= numStack[1] numStack[0] -= numStack[1]
numStack.drop(1) numStack = dropAtIdx(numStack,1)
} }
} }
} }
return numStack[0].toString() return numStack[0].toString()
} }
private fun dropAtIdx(array: FloatArray, idx:Int): FloatArray{
if (idx>array.size){
throw IndexOutOfBoundsException()
}
var newArray = floatArrayOf()
for (i in array.indices){
if (i!=idx){
newArray += array[i]
}
}
return newArray
}
private fun dropAtIdx(array: CharArray, idx:Int): CharArray{
if (idx>array.size){
throw IndexOutOfBoundsException()
}
var newArray = charArrayOf()
for (i in array.indices){
if (i!=idx){
newArray += array[i]
}
}
return newArray
}

View file

@ -2,12 +2,14 @@ package uk.kagurach.android101;
import static uk.kagurach.android101.KaculateKt.Kaculate; import static uk.kagurach.android101.KaculateKt.Kaculate;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import androidx.activity.EdgeToEdge; import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets; import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
@ -35,14 +37,20 @@ public class Page3 extends AppCompatActivity {
findViewById(R.id.P3_8).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3_8).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3_9).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3_9).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3BK).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3BK).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3X).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3X).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3Plus).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3Plus).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3Minus).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3Minus).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3Div).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3Div).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3ChengFang).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3Dot).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3Dot).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3CE).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3CE).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3ChengFang).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3EqualButton).setOnClickListener(new CalculateOnClickListener()); findViewById(R.id.P3EqualButton).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3Nextpage).setOnClickListener(new PageButtonHandler());
findViewById(R.id.P3Nextpage).setOnLongClickListener(new LongClickHandler());
} }
@ -53,28 +61,124 @@ public class Page3 extends AppCompatActivity {
TextView tv = findViewById(R.id.P3CalcResult); TextView tv = findViewById(R.id.P3CalcResult);
String s = tv.getText().toString(); String s = tv.getText().toString();
String o = ((Button)v).getText().toString(); String o = ((Button)v).getText().toString();
Button dot = findViewById(R.id.P3Dot);
switch (o){ switch (o){
case "退格": case "退格":
if (!s.isEmpty()){ if (!s.isEmpty()){
s = s.substring(0,s.length()-1); if (s.length()==1){
s = "";
dot.setEnabled(true);
setOpButton(true);
}else {
s = s.substring(0, s.length() - 1);
dot.setEnabled(!isNumberWithDot(s));
setOpButton(s.substring(s.length()-1).matches("\\d|\\."));
}
} }
break; break;
case "CE": case "CE":
s = ""; s = "";
dot.setEnabled(true);
break; break;
case "=": case "=":
calc(); calc();
s = tv.getText().toString();
if (!s.contains(".")){
dot.setEnabled(true);
}
setOpButton(true);
return; return;
case ".":
dot.setEnabled(false);
s+='.';
break;
default: default:
s += o; s += o;
if (o.matches("\\d")) { // If inputted a number
setOpButton(true);
}else{ // Else an operator
dot.setEnabled(true);
setOpButton(false);
}
} }
tv.setText(s); tv.setText(s);
} }
} }
private void setOpButton(boolean state){
findViewById(R.id.P3X).setEnabled(state);
findViewById(R.id.P3Plus).setEnabled(state);
findViewById(R.id.P3Minus).setEnabled(state);
findViewById(R.id.P3Div).setEnabled(state);
findViewById(R.id.P3ChengFang).setEnabled(state);
}
private boolean isNumberWithDot(@NonNull String s){
if (!s.contains(".")){
return false;
}
boolean hasDot = false;
for (int i = s.length()-1; i >= 0; i--) {
char c = s.charAt(i);
if (c=='+'||c=='-'||c=='x'||c=='/'||c=='^'){
return hasDot;
}else if (c=='.'){
hasDot = true;
}
}
return hasDot;
}
private void calc(){ private void calc(){
TextView textView = findViewById(R.id.P3CalcResult); TextView textView = findViewById(R.id.P3CalcResult);
String result = Kaculate(textView.getText().toString(),this); String result = Kaculate(textView.getText().toString(),this);
if (result.contains(".")) { // In case of Infinity or NaN as a result
int i = 0;
while (result.charAt(i) != '.') {
i++;
}
int j = i;
boolean notZero = false;
for (i++; i < result.length(); i++) {
if (result.charAt(i) != '0') {
notZero = true;
}
}
if (!notZero) {
result = result.substring(0, j);
}
}
textView.setText(result); textView.setText(result);
} }
public void goNext() {
Intent myIntent = new Intent(this, Page4.class);
startActivity(myIntent);
}
public void goPrev() {
Intent myIntent = new Intent(this, MainActivity2.class);
startActivity(myIntent);
}
final class PageButtonHandler implements View.OnClickListener
{
@Override
public void onClick(View v){
goNext();
}
}
final class LongClickHandler implements View.OnLongClickListener
{
@Override
public boolean onLongClick(View v){
goPrev();
return false;
}
}
} }

View file

@ -0,0 +1,24 @@
package uk.kagurach.android101;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class Page4 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_page4);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}

View file

@ -6,196 +6,192 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".Page3"> tools:context=".Page3">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical"
>
<View <View
android:id="@+id/view" android:id="@+id/view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="35dp" /> android:layout_height="35dp" />
<TextView <TextView
android:id="@+id/P3CalcName" android:id="@+id/P3CalcName"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
android:text="@string/kaculate" android:text="@string/kaculate"
android:textSize="40sp"/> android:textSize="40sp"/>
<TextView <TextView
android:id="@+id/P3CalcResult" android:id="@+id/P3CalcResult"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="400dp" android:layout_height="400dp"
android:textSize="25sp"
android:text="" /> android:text="" />
<LinearLayout
<GridLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="0dp"
android:columnCount="4" android:layout_weight="1"
android:rowCount="5"> android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button <Button
android:id="@+id/P3CE" android:id="@+id/P3CE"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="0"
android:layout_column="0"
android:text="CE" /> android:text="CE" />
<Button <Button
android:id="@+id/P3Div" android:id="@+id/P3Div"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="0"
android:layout_column="1"
android:text="/" /> android:text="/" />
<Button <Button
android:id="@+id/P3X" android:id="@+id/P3X"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="0"
android:layout_column="2"
android:text="x" /> android:text="x" />
<Button <Button
android:id="@+id/P3Nextpage" android:id="@+id/P3Nextpage"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="0"
android:layout_column="3"
android:text="@string/next_page" /> android:text="@string/next_page" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button <Button
android:id="@+id/P3_7" android:id="@+id/P3_7"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="1" android:backgroundTint="@color/tfb"
android:layout_column="0"
android:text="7" /> android:text="7" />
<Button <Button
android:id="@+id/P3_8" android:id="@+id/P3_8"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="1" android:backgroundTint="@color/tfb"
android:layout_column="1"
android:text="8" /> android:text="8" />
<Button <Button
android:id="@+id/P3_9" android:id="@+id/P3_9"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="1" android:backgroundTint="@color/tfb"
android:layout_column="2"
android:text="9" /> android:text="9" />
<Button <Button
android:id="@+id/P3Plus" android:id="@+id/P3Plus"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="1"
android:layout_column="3"
android:text="+" /> android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button <Button
android:id="@+id/P3_4" android:id="@+id/P3_4"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="2" android:backgroundTint="@color/tfb"
android:layout_column="0"
android:text="4" /> android:text="4" />
<Button <Button
android:id="@+id/P3_5" android:id="@+id/P3_5"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="2" android:backgroundTint="@color/tfb"
android:layout_column="1"
android:text="5" /> android:text="5" />
<Button <Button
android:id="@+id/P3_6" android:id="@+id/P3_6"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="2" android:backgroundTint="@color/tfb"
android:layout_column="2"
android:text="6" /> android:text="6" />
<Button <Button
android:id="@+id/P3Minus" android:id="@+id/P3Minus"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="2"
android:layout_column="3"
android:text="-" /> android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button <Button
android:id="@+id/P3_1" android:id="@+id/P3_1"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="3" android:backgroundTint="@color/tfb"
android:layout_column="0"
android:text="1" /> android:text="1" />
<Button <Button
android:id="@+id/P3_2" android:id="@+id/P3_2"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="3" android:backgroundTint="@color/tfb"
android:layout_column="1"
android:text="2" /> android:text="2" />
<Button <Button
android:id="@+id/P3_3" android:id="@+id/P3_3"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="3" android:backgroundTint="@color/tfb"
android:layout_column="2"
android:text="3" /> android:text="3" />
<Button <Button
android:id="@+id/P3ChengFang" android:id="@+id/P3ChengFang"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="3"
android:layout_column="3"
android:text="^" /> android:text="^" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button <Button
android:id="@+id/P3BK" android:id="@+id/P3BK"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="4"
android:layout_column="0"
android:text="@string/Backspace" /> android:text="@string/Backspace" />
<Button <Button
android:id="@+id/P3_0" android:id="@+id/P3_0"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="4" android:backgroundTint="@color/tfb"
android:layout_column="1"
android:text="0" /> android:text="0" />
<Button <Button
android:id="@+id/P3Dot" android:id="@+id/P3Dot"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="4"
android:layout_column="2"
android:text="." /> android:text="." />
<Button <Button
android:id="@+id/P3EqualButton" android:id="@+id/P3EqualButton"
android:layout_weight="1"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_row="4"
android:layout_column="3"
android:text="=" /> android:text="=" />
</GridLayout> </LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Page4">
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -7,4 +7,7 @@
<color name="teal_700">#FF018786</color> <color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="tfb">#5BCEFA</color>
<color name="tfp">#F5A9B8</color>
</resources> </resources>

View file

@ -7,6 +7,6 @@
<string name="set_color">设置颜色</string> <string name="set_color">设置颜色</string>
<string name="set">设置</string> <string name="set">设置</string>
<string name="setText">设置文本</string> <string name="setText">设置文本</string>
<string name="kaculate">Kaculate</string> <string name="kaculate">弱智计算器</string>
<string name="Backspace">退格</string> <string name="Backspace">退格</string>
</resources> </resources>