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

View file

@ -14,8 +14,7 @@ fun Kaculate(src: String,ctx :Context) : String{
var _curnum = ""
var _immediatePop = false
for (i in 0..<src.length){
for (i in src.indices){
if (src[i] in '0'..'9' || src[i] == '.'){
_curnum += src[i]
}
@ -27,27 +26,7 @@ fun Kaculate(src: String,ctx :Context) : String{
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]
}
opStack += src[i]
}
else{
ToastHelper.ShowToast("Unknown input char:" + src[i].toString(),ctx);
@ -58,7 +37,6 @@ fun Kaculate(src: String,ctx :Context) : String{
if (_curnum.isNotEmpty()){
try {
numStack += _curnum.toFloat()
_curnum = ""
}catch (e:Exception){
ToastHelper.ShowToast(e.toString(),ctx)
return src
@ -70,35 +48,80 @@ fun Kaculate(src: String,ctx :Context) : String{
return src
}
var intStackCurr = 0
for (i in 0..<opStack.size){
if (opStack[i] == 'x'){
// ^
if ('^' in opStack){
var intStackCurr = 0
for (i in opStack.indices){
if (opStack[i]!='^'){
intStackCurr ++
continue
}
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 ++
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) {
if (i == '+') {
numStack[0] += numStack[1]
numStack.drop(1)
} else {
numStack = dropAtIdx(numStack,1)
} else if (i=='-') {
numStack[0] -= numStack[1]
numStack.drop(1)
numStack = dropAtIdx(numStack,1)
}
}
}
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 android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
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_9).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3BK).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3X).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3Plus).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3Minus).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.P3CE).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3ChengFang).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);
String s = tv.getText().toString();
String o = ((Button)v).getText().toString();
Button dot = findViewById(R.id.P3Dot);
switch (o){
case "退格":
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;
case "CE":
s = "";
dot.setEnabled(true);
break;
case "=":
calc();
s = tv.getText().toString();
if (!s.contains(".")){
dot.setEnabled(true);
}
setOpButton(true);
return;
case ".":
dot.setEnabled(false);
s+='.';
break;
default:
s += o;
if (o.matches("\\d")) { // If inputted a number
setOpButton(true);
}else{ // Else an operator
dot.setEnabled(true);
setOpButton(false);
}
}
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(){
TextView textView = findViewById(R.id.P3CalcResult);
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);
}
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_height="match_parent"
tools:context=".Page3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:orientation="vertical"
>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="35dp" />
<TextView
android:id="@+id/P3CalcName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/kaculate"
android:textSize="40sp"/>
<TextView
android:id="@+id/P3CalcResult"
android:layout_width="match_parent"
android:layout_height="400dp"
android:textSize="25sp"
android:text="" />
<GridLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:rowCount="5">
<Button
android:id="@+id/P3CE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:text="CE" />
<Button
android:id="@+id/P3Div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:text="/" />
<Button
android:id="@+id/P3X"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="2"
android:text="x" />
<Button
android:id="@+id/P3Nextpage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="3"
android:text="@string/next_page" />
<Button
android:id="@+id/P3_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:text="7" />
<Button
android:id="@+id/P3_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:text="8" />
<Button
android:id="@+id/P3_9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="2"
android:text="9" />
<Button
android:id="@+id/P3Plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="3"
android:text="+" />
<Button
android:id="@+id/P3_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="0"
android:text="4" />
<Button
android:id="@+id/P3_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="1"
android:text="5" />
<Button
android:id="@+id/P3_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="2"
android:text="6" />
<Button
android:id="@+id/P3Minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="3"
android:text="-" />
<Button
android:id="@+id/P3_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="0"
android:text="1" />
<Button
android:id="@+id/P3_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="1"
android:text="2" />
<Button
android:id="@+id/P3_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="2"
android:text="3" />
<Button
android:id="@+id/P3ChengFang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="3"
android:text="^" />
<Button
android:id="@+id/P3BK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="0"
android:text="@string/Backspace" />
<Button
android:id="@+id/P3_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="1"
android:text="0" />
<Button
android:id="@+id/P3Dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="2"
android:text="." />
<Button
android:id="@+id/P3EqualButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="3"
android:text="=" />
</GridLayout>
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/P3CE"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="CE" />
<Button
android:id="@+id/P3Div"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="/" />
<Button
android:id="@+id/P3X"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="x" />
<Button
android:id="@+id/P3Nextpage"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/next_page" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/P3_7"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="7" />
<Button
android:id="@+id/P3_8"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="8" />
<Button
android:id="@+id/P3_9"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="9" />
<Button
android:id="@+id/P3Plus"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/P3_4"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="4" />
<Button
android:id="@+id/P3_5"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="5" />
<Button
android:id="@+id/P3_6"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="6" />
<Button
android:id="@+id/P3Minus"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/P3_1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="1" />
<Button
android:id="@+id/P3_2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="2" />
<Button
android:id="@+id/P3_3"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="3" />
<Button
android:id="@+id/P3ChengFang"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="^" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/P3BK"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/Backspace" />
<Button
android:id="@+id/P3_0"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="@color/tfb"
android:text="0" />
<Button
android:id="@+id/P3Dot"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="." />
<Button
android:id="@+id/P3EqualButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="=" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</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="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="tfb">#5BCEFA</color>
<color name="tfp">#F5A9B8</color>
</resources>

View file

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