简单UI设计
中缀转后缀,后缀表达式计算。
android打包APK
android:gravity:
这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置。例如,在一个Button按钮控件中设置如下两个属性,
android:gravity="left"和android:text=“提交”,这时Button上的文字“提交”将会位于Button的左部。
android:layout_gravity:
这个是针对控件本身而言,用来控制该控件在包含该控件的父控件中的位置。同样,当我们在Button按钮控件中设置android:layout_gravity="left"属性时,表示该Button按钮将位于界面的左部。\
在layout_width設置為mathch_parent的時候,layout_weight所代表的是你的控件要優先盡可能的大,但這個大是有限度的,即mathch_parent.
在layout_width設置為wrap_content的時候,layout_weight所代表的是你的控件要優先盡可能的小,但這個小是有限度的,即wrap_content.
说明
规则:从左到右遍历中缀表达式的每个数字和符号,若是数字就输出,即成为后缀表达式的一部分;若是符号,则判断其与栈顶符号的优先级,是右括号或优先级低于找顶符号(乘除优先加减)则栈顶元素依次出找并输出,并将当前符号进栈,一直到最终输出后缀表达式为止。
从刚才的推导中你会发现,要想让计算机具有处理我们通常的标准(中缀)表达式的能力,最重要的就是两步:
1)初始化两个栈:运算符栈s1和储存中间结果的栈s2;
2)从左至右扫描中缀表达式;
3)遇到操作数时,将其压s2;
4)遇到运算符时,比较其与s1栈顶运算符的优先级:
1.如果s1为空,或栈顶运算符为左括号“(",则直接将此运算符入栈;
2.否则,若优先级比栈顶运算符的高,也将运算符压入s1;
3.否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(4.1)与s1中新的栈顶运算
符相比较;
5)遇到括号时:
(1)如果是左括号“(”,则直接压入s1
(2)如果是右括号时)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为
止,此时将这一对括号 丢弃
6)重复步骤2至5,直到表达式的最右边
7)将s1中剩余的运算符依次弹出并压入s2
8)依次弹出s2中的元素并输出,栈弹出的结果的逆序即为中缀表达式对应的后缀表达式
1.通过一个index值(索引),来遍历我们的表达式2.如果我们发现是一个数字,就直接入数栈
3.如果发现扫描到是一个符号,就分如下情况3.1如果发现当前的符号栈为空,就直接入核
3.2如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或者等于栈中的操作符,就需要从数栈中pop出两个数,在从符号栈中pop出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈,如果当前的操作符的优先级大于栈中的操作符,就直接入符号栈.
4.当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,并运行.5.最后在数栈只有—个数字,就是表达式的结果
验证:3+2*6-2 =13
上面是TextView,下面是用View直线划分的按键
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_30"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#dedede"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/guideline_30"
android:gravity="bottom">
<TextView
android:id="@+id/history_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#888888"
android:gravity="right"
android:autoSizeTextType="uniform"
android:textSize="28sp"/>
<TextView
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="39sp"
android:autoSizeTextType="uniform"
android:textColor="@color/black"/>
</LinearLayout>
<View
android:id="@+id/horizontal_divider_44"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.44"/>
<View
android:id="@+id/horizontal_divider_58"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.58"/>
<View
android:id="@+id/horizontal_divider_72"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.72"/>
<View
android:id="@+id/horizontal_divider_86"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.86"/>
<View
android:id="@+id/horizontal_divider_100"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="1"/>
<View
android:id="@+id/vertical_divider_0"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"/>
<View
android:id="@+id/vertical_divider_25"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.25"/>
<View
android:id="@+id/vertical_divider_50"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"/>
<View
android:id="@+id/vertical_divider_75"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.75"/>
<View
android:id="@+id/vertical_divider_100"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_clear"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="C"
android:textColor="#ffb6c1"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/guideline_30"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_44"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_back"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="←"
android:textColor="@android:color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_86"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_100"
app:layout_constraintStart_toStartOf="@id/vertical_divider_0"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_divi"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="/"
android:textColor="@android:color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_72"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_86"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_multiply"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="*"
android:textColor="@android:color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/guideline_30"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_44"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_7"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="7"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_44"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_58"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_8"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="8"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_44"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_58"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_9"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="9"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_44"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_58"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_minus"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="-"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_44"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_58"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_4"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="4"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_58"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_72"
app:layout_constraintStart_toStartOf="@id/vertical_divider_0"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_5"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="5"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_58"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_72"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_6"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="6"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_58"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_72"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_plus"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="+"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_58"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_72"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="1"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_72"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_86"
app:layout_constraintStart_toStartOf="@id/vertical_divider_0"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="2"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_72"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_86"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_3"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="3"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_72"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_86"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
<TextView
style="@style/button_equals_Theme"
android:id="@+id/text_equals"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="="
android:textColor="#ffb6c1"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_86"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_100"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_leftbracket"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="("
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/guideline_30"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_44"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_0"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="0"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_86"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_100"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_point"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="."
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_86"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_100"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_rightbracket"
android:layout_width="0dp"
android:layout_height="0dp"
android:text=")"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/guideline_30"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_44"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/figure_recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintBottom_toBottomOf="@id/guideline_20"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/result_text"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@id/guideline_20"
app:layout_constraintBottom_toBottomOf="@id/guideline_30"
android:gravity="end"
android:textColor="@color/black"
android:textSize="39sp"/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_30"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3"
/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_20"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2"
/>
<View
android:id="@+id/horizontal_divider_44"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.44"/>
<View
android:id="@+id/horizontal_divider_58"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.58"/>
<View
android:id="@+id/horizontal_divider_72"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.72"/>
<View
android:id="@+id/horizontal_divider_86"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.86"/>
<View
android:id="@+id/horizontal_divider_100"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#dedede"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="1"/>
<View
android:id="@+id/vertical_divider_0"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"/>
<View
android:id="@+id/vertical_divider_25"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.25"/>
<View
android:id="@+id/vertical_divider_50"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"/>
<View
android:id="@+id/vertical_divider_75"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.75"/>
<View
android:id="@+id/vertical_divider_100"
android:layout_width="1px"
android:layout_height="0dp"
android:background="#dedede"
app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_clear"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="C"
android:textColor="#ffb6c1"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/guideline_30"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_44"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_back"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="←"
android:textColor="@android:color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_86"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_100"
app:layout_constraintStart_toStartOf="@id/vertical_divider_0"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_divi"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="/"
android:textColor="@android:color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_72"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_86"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_multiply"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="*"
android:textColor="@android:color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/guideline_30"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_44"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_7"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="7"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_44"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_58"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_8"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="8"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_44"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_58"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_9"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="9"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_44"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_58"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_minus"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="-"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_44"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_58"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_4"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="4"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_58"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_72"
app:layout_constraintStart_toStartOf="@id/vertical_divider_0"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_5"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="5"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_58"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_72"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_6"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="6"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_58"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_72"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_plus"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="+"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_58"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_72"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="1"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_72"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_86"
app:layout_constraintStart_toStartOf="@id/vertical_divider_0"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_25"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="2"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_72"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_86"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_3"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="3"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_72"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_86"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
<TextView
style="@style/button_equals_Theme"
android:id="@+id/text_equals"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="="
android:textColor="#ffb6c1"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_86"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_100"
app:layout_constraintStart_toStartOf="@id/vertical_divider_75"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_100"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_leftbracket"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="("
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/guideline_30"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_44"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_0"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="0"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_86"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_100"
app:layout_constraintStart_toStartOf="@id/vertical_divider_25"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_50"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_point"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="."
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/horizontal_divider_86"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_100"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
<TextView
style="@style/buttonTheme"
android:id="@+id/text_rightbracket"
android:layout_width="0dp"
android:layout_height="0dp"
android:text=")"
android:textColor="@color/black"
android:gravity="center"
android:textSize="36sp"
app:layout_constraintTop_toBottomOf="@id/guideline_30"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider_44"
app:layout_constraintStart_toStartOf="@id/vertical_divider_50"
app:layout_constraintEnd_toStartOf="@id/vertical_divider_75"/>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.calculator1;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
import static com.example.calculator1.Core_Algorithm.isNumber;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView text_clear;
private TextView text_back;
private TextView text_divi;
private TextView text_multiply;
private TextView text_minus;
private TextView text_plus;
private TextView text_equals;
private TextView text_point;
private TextView text_0;
private TextView text_1;
private TextView text_2;
private TextView text_3;
private TextView text_4;
private TextView text_5;
private TextView text_6;
private TextView text_7;
private TextView text_8;
private TextView text_9;
private TextView history_text;
private TextView result_text;
private TextView text_leftbracket;
private TextView text_rightbracket;
private StringBuilder history=new StringBuilder("0");
private StringBuilder result=new StringBuilder("");
private String sign="+-*/";
private List<String> ans= new ArrayList<>(11111);
private int resulttemp;
String temp;
private List<String> mlist = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.text_clear:
result.delete(0, result.length());
result_text.setText("0");
break;
case R.id.text_back:
//长度不为0就可以删
if (result.length() != 0) {
result.delete(result.length() - 1, result.length());
result_text.setText(result);
//删没了显示0
if (result.length() == 0)
result_text.setText("0");
break;
} else {//没东西删长度为0就直接显示0
result_text.setText("0");
break;
}
case R.id.text_divi:
if(resulttemp == -1){
result = new StringBuilder("0/");
break;
}
if(result.length() == 0){
result.append("0/");
}
// else if(sign.contains(result.subSequence(result.length()-1,result.length())) && result.length()==1)
// result.replace(result.length()-1,result.length(),"0/");
//
else if (sign.contains(result.subSequence(result.length() - 1, result.length())))//前一位是加减乘除就替换,左闭右开
result.replace(result.length() - 1, result.length(), "/");
else {
result.append("/");
}
result_text.setText(result.toString());
break;
case R.id.text_multiply:
if(resulttemp == -1){
result = new StringBuilder("0*");
break;
}
if(result.length() == 0){
result.append("0*");
}else if(sign.contains(result.subSequence(result.length()-1,result.length())) && result.length()==1)
result.replace(result.length()-1,result.length(),"0*");
else if (sign.contains(result.subSequence(result.length() - 1, result.length())))
result.replace(result.length() - 1, result.length(), "*");
else {
result.append("*");
}
result_text.setText(result.toString());
break;
case R.id.text_minus:
if(resulttemp == -1){
result = new StringBuilder("0-");
break;
}
if(result.length() == 0){
result.append("0-");
}else if(sign.contains(result.subSequence(result.length()-1,result.length())) && result.length()==1)
result.replace(result.length()-1,result.length(),"-");
else if (sign.contains(result.subSequence(result.length() - 1, result.length())))
result.replace(result.length() - 1, result.length(), "-");
else{
result.append("-");
}
result_text.setText(result.toString());
break;
case R.id.text_plus:
if(resulttemp == -1){
result = new StringBuilder("0+");
break;
}
if(result.length() == 0){
result.append("0+");
}else if(sign.contains(result.subSequence(result.length()-1,result.length())) && result.length()==1)
result.replace(result.length()-1,result.length(),"0+");
else if(sign.contains(result.subSequence(result.length()-1,result.length())))
result.replace(result.length()-1,result.length(),"+");
else{
result.append("+");
}
result_text.setText(result.toString());
break;
case R.id.text_equals:
if(resulttemp == -1){ //右括号数目错误,运算式错误,除数不能为0
result = new StringBuilder("0");
break;
}
if(result.length() == 0){
result_text.setText("0");
break;
}
//如果最后一位是符号则忽略
if(!Character.isDigit(result.charAt(result.length()-1))){
result.delete(result.length()-1,result.length());
if(result.toString().equals("")) {
result = new StringBuilder("0");
result_text.setText(result.toString());
break;
}
break;
}
history = new StringBuilder(result.toString());
mlist.add(history.toString());
// if (countofleftbracket < countofrightbracket) {
// result_text.setText("出错!");
// result = new StringBuilder("0");
// result_text.setText(result.toString());
// break;
// }
// //补齐右括号
// while (countofleftbracket > countofrightbracket) {
// result.append(')');
// countofrightbracket++;
// result_text.setText(result.toString());
// }
//ans是List,result是StringBuilder
result = new StringBuilder(result_text.getText());
ans = Core_Algorithm.toInfixExpressionList(result.toString());//中缀List
Log.d("anssssss",ans.toString());
ans = Core_Algorithm.parseSuffixExpressionList(ans);//后缀List
result=new StringBuilder(Core_Algorithm.calculate(ans));//结果String
result_text.setText(result.toString());
if(!isNumber(result.toString())){
resulttemp = -1;
}
break;
case R.id.text_point:
if(resulttemp == -1){
result = new StringBuilder("0.");
result_text.setText(result.toString());
break;
}
if(result.length() == 0){
result.append("0.");
result_text.setText(result.toString());
break;
}
//如果最后一位是数字,就判断前一个符号前有没有小数点,没有才能加。
if(Character.isDigit(result.charAt(result.length()-1))) {
int len = result.length()-1;
while(len>=0 && Character.isDigit(result.charAt(len))){
len--;
}
//len指向不是数字时跳出while
if(len>=0 && result.charAt(len)=='.'){
break;
}else{
result.append(".");
result_text.setText(result.toString());
break;
}
}
//无字符或最后一位是+-*/就直接加0.
//.contains方法不能传入char,只能用indexOf,
else if(result.length()==0 || sign.toString().indexOf(result.charAt(result.length()-1)) != -1){
result.append("0.");
result_text.setText(result.toString());
}
//最后一位是括号
else{
result.append(".");
result_text.setText(result.toString());
}
break;
case R.id.text_leftbracket:
if(resulttemp == -1){
result = new StringBuilder("(");
result_text.setText(result.toString());
break;
}
if(Character.isDigit(result.charAt(result.length()-1))){
result.append("*(");
}
else {
result.append("(");
}
result_text.setText(result.toString());
break;
case R.id.text_rightbracket:
if(resulttemp == -1){
result = new StringBuilder(")");
result_text.setText(result.toString());
break;
}
result.append(")");
result_text.setText(result.toString());
break;
case R.id.text_0:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*0");
else if(resulttemp == -1){
result = new StringBuilder("0");
}
else result.append("0");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_1:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*1");
else if(resulttemp == -1){
result = new StringBuilder("1");
}
else result.append("1");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_2:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*2");
else if(resulttemp == -1){
result = new StringBuilder("2");
}
else result.append("2");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_3:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*3");
else if(resulttemp == -1){
result = new StringBuilder("3");
}
else result.append("3");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_4:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*4");
else if(resulttemp == -1){
result = new StringBuilder("4");
}
else result.append("4");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_5:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*5");
else if(resulttemp == -1){
result = new StringBuilder("5");
}
else result.append("5");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_6:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*6");
else if(resulttemp == -1){
result = new StringBuilder("6");
}
else result.append("6");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_7:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*7");
else if(resulttemp == -1){
result = new StringBuilder("7");
}
else result.append("7");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_8:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*8");
else if(resulttemp == -1){
result = new StringBuilder("8");
}
else result.append("8");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_9:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*9");
else if(resulttemp == -1){
result = new StringBuilder("9");
}
else result.append("9");
result_text.setText(result.toString());
resulttemp = 1;
break;
default:
result_text.setText("运算式错误!");
result = new StringBuilder("0");
result_text.setText(result.toString());
break;
}
}
private void initView(){
//result = new StringBuilder(result_text.getText().toString());
text_clear=(TextView)findViewById(R.id.text_clear);
text_back=(TextView)findViewById(R.id.text_back);
text_divi=(TextView)findViewById(R.id.text_divi);
text_multiply=(TextView)findViewById(R.id.text_multiply);
text_minus=(TextView)findViewById(R.id.text_minus);
text_plus=(TextView)findViewById(R.id.text_plus);
text_equals=(TextView)findViewById(R.id.text_equals);
text_leftbracket=(TextView)findViewById(R.id.text_leftbracket);
text_rightbracket=(TextView)findViewById(R.id.text_rightbracket);
text_point=(TextView)findViewById(R.id.text_point);
text_0=(TextView)findViewById(R.id.text_0);
text_1=(TextView)findViewById(R.id.text_1);
text_2=(TextView)findViewById(R.id.text_2);
text_3=(TextView)findViewById(R.id.text_3);
text_4=(TextView)findViewById(R.id.text_4);
text_5=(TextView)findViewById(R.id.text_5);
text_6=(TextView)findViewById(R.id.text_6);
text_7=(TextView)findViewById(R.id.text_7);
text_8=(TextView)findViewById(R.id.text_8);
text_9=(TextView)findViewById(R.id.text_9);
history_text=(TextView)findViewById(R.id.history_text);
result_text=(TextView)findViewById(R.id.result_text);
text_clear.setOnClickListener(this);
text_back.setOnClickListener(this);
text_divi.setOnClickListener(this);
text_multiply.setOnClickListener(this);
text_minus.setOnClickListener(this);
text_plus.setOnClickListener(this);
text_equals.setOnClickListener(this);
text_point.setOnClickListener(this);
text_leftbracket.setOnClickListener(this);
text_rightbracket.setOnClickListener(this);
text_0.setOnClickListener(this);
text_1.setOnClickListener(this);
text_2.setOnClickListener(this);
text_3.setOnClickListener(this);
text_4.setOnClickListener(this);
text_5.setOnClickListener(this);
text_6.setOnClickListener(this);
text_7.setOnClickListener(this);
text_8.setOnClickListener(this);
text_9.setOnClickListener(this);
}
}
思路:
为每个控件起相对应的名字和监控器,点击不同控件触发不同的事件
package com.example.calculator;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.example.calculator.Core_Algorithm.getLastNumber;
import static com.example.calculator.Core_Algorithm.isNumber;
import static com.example.calculator.Core_Algorithm.isOpeartor;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView text_clear;
TextView text_back;
TextView text_divi;
TextView text_multiply;
TextView text_minus;
TextView text_plus;
TextView text_equals;
TextView text_point;
TextView text_0;
TextView text_1;
TextView text_2;
TextView text_3;
TextView text_4;
TextView text_5;
TextView text_6;
TextView text_7;
TextView text_8;
TextView text_9;
TextView history_text;
TextView result_text;
TextView text_leftbracket;
TextView text_rightbracket;
StringBuilder history=new StringBuilder("0");
StringBuilder result=new StringBuilder();
String sign="+-*/";
List<String> ans= new ArrayList<>(11111);
int resulttemp;
List<String> mlist = new ArrayList<>();
int countofleftbracket;
int countofrightbracket;
char[] ttttttt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
@SuppressLint("NonConstantResourceId")
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.text_clear:
result.delete(0, result.length());
result = new StringBuilder();
result_text.setText("0");
resulttemp = 1;
break;
case R.id.text_back:
//长度不为0就可以删
if (result.length() != 0) {
result.delete(result.length() - 1, result.length());
result_text.setText(result);
//删没了显示0
if (result.length() == 0)
result_text.setText("0");
} else {//没东西删长度为0就直接显示0
result_text.setText("0");
}
resulttemp = 1;
break;
//得出来正确的数字 resulttemp == 2 再按符号添在后面
// 输出中文(报错)resulttemp == -1 ,再按符号键清空text加符号
case R.id.text_divi:
if(resulttemp == -1){
// result = new StringBuilder("0/");
break;
}
if(result.length() == 0){
result.append("0/");
}
// else if(sign.contains(result.subSequence(result.length()-1,result.length())) && result.length()==1)
// result.replace(result.length()-1,result.length(),"0/");
//
else if (sign.contains(result.subSequence(result.length() - 1, result.length())))//前一位是加减乘除就替换,左闭右开
result.replace(result.length() - 1, result.length(), "/");
else {
result.append("/");
}
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_multiply:
if(resulttemp == -1){
// result = new StringBuilder("0*");
break;
}
if(result.length() == 0){
result.append("0*");
}else if(sign.contains(result.subSequence(result.length()-1,result.length())) && result.length()==1)
result.replace(result.length()-1,result.length(),"0*");
else if (sign.contains(result.subSequence(result.length() - 1, result.length())))
result.replace(result.length() - 1, result.length(), "*");
else {
result.append("*");
}
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_minus:
if(resulttemp == -1 ){
// result = new StringBuilder("0-");
// result_text.setText(result.toString());
break;
}
if(result.length() == 0){
result.append("0-");
//最后一位是+-*/ 并且 只有一个字符=-*/
}else if(sign.contains(result.subSequence(result.length()-1,result.length())) && result.length()==1)
result.replace(result.length()-1,result.length(),"0-");
//最后一位是+-*/
else if (sign.contains(result.subSequence(result.length() - 1, result.length())))
result.replace(result.length() - 1, result.length(), "-");
else{
result.append("-");
}
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_plus:
if(resulttemp == -1 ){
// result = new StringBuilder("0+");
break;
}
if(result.length() == 0){
result.append("0+");
}else if(sign.contains(result.subSequence(result.length()-1,result.length())) && result.length()==1)
result.replace(result.length()-1,result.length(),"0+");
else if(sign.contains(result.subSequence(result.length()-1,result.length())))
result.replace(result.length()-1,result.length(),"+");
else{
result.append("+");
}
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_equals:
if(resulttemp == -1){ //右括号数目错误,运算式错误,除数不能为0
result = new StringBuilder("0");
result_text.setText("0");
break;
}
if(result.length() == 0){
result = new StringBuilder("0");
result_text.setText("0");
break;
}
//右括号大于左括号
ttttttt = result_text.getText().toString().toCharArray();
for(char c : ttttttt){
if(c == '(') countofleftbracket++;
else if(c == ')') countofrightbracket++;
}
if (countofleftbracket < countofrightbracket) {
result_text.setText("右括号数目有误!");
// result = new StringBuilder("0");
resulttemp = -1;
countofleftbracket = 0;
countofrightbracket = 0;
break;
}
//补齐右括号在算法类里
//如果最后一位是符号则忽略
//while(!Character.isDigit(result.charAt(result.length()-1)) && result.charAt(result.length()-1)!='('&& result.charAt(result.length()-1)!=')'){
if(isOpeartor(result.toString().charAt(result.toString().length()-1))){
result.delete(result.toString().length()-1,result.toString().length());
//result= new StringBuilder(result.toString());
if(result.toString().equals("")) {//删空了
result = new StringBuilder("0");
result_text.setText(result.toString());
break;
}
}
history = new StringBuilder(result.toString());
//mlist.add(history.toString());
//ans是List,result是StringBuilder
//result = new StringBuilder(result_text.getText());
ans = Core_Algorithm.toInfixExpressionList(result.toString());//中缀List
Log.d("anssssss1",ans.toString());
ans = Core_Algorithm.parseSuffixExpressionList(ans);//后缀List
result=new StringBuilder(Core_Algorithm.calculate(ans));//结果String
result_text.setText(result.toString());
Log.d("anssssss2",result.toString());
Log.d("anssssss3",result_text.getText().toString());
if(isNumber(result.toString())) resulttemp = 2;
else if(!isNumber(result.toString())){
resulttemp = -1;
}
break;
case R.id.text_point:
if( result.toString().equals("") ||result.length() == 0 || result.toString().equals("0")){
//result.append("0.");
result = new StringBuilder("0.");
result_text.setText(result.toString());
break;
}
if(resulttemp == 0 || resulttemp == -1 || resulttemp == 2 || result.charAt(result.length()-1)=='.'){
result= new StringBuilder("0.");
result_text.setText(result.toString());
break;
}
if(isOpeartor(result.toString().charAt(result.toString().length()-1)) || result.toString().charAt(result.toString().length()-1) == '('){
result.append("0.");
result_text.setText(result.toString());
break;
}
//如果最后一位是数字,就判断前一个符号前有没有小数点,没有才能加。
if(Character.isDigit(result.charAt(result.length()-1))) {
int len = result.length()-1;
while(len>=0 && Character.isDigit(result.charAt(len))){
len--;
}
//len指向不是数字时跳出while
if (len < 0 || result.charAt(len) != '.') {
result.append(".");
result_text.setText(result.toString());
}
resulttemp = 1;
break;
}
//无字符或最后一位是+-*/就直接加0.
//.contains方法不能传入char,只能用indexOf,
else if(result.length()==0 || sign.indexOf(result.charAt(result.length()-1)) != -1){
result.append(".");
result_text.setText(result.toString());
}
//最后一位是括号
else{
result.append(".");
result_text.setText(result.toString());
}
resulttemp = 1;
break;
case R.id.text_leftbracket:
if(resulttemp == -1 || result.length() == 0){
result = new StringBuilder("(");
result_text.setText(result.toString());
break;
}
if(Character.isDigit(result.charAt(result.length()-1))){
result.append("*(");
}
else {
result.append("(");
}
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_rightbracket:
if(resulttemp == -1 || result.length() == 0){
result = new StringBuilder(")");
result_text.setText(result.toString());
break;
}
result.append(")");
result_text.setText(result.toString());
resulttemp = 1;
break;
//得出来正确的数字 resulttemp == 2运算结束 再按数字键重置字符串 或 输出中文(报错)resulttemp == -1 ,再按数字键情况text加数字
// resulttemp == 1可以追加输入
case R.id.text_0:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*0");
else if(resulttemp == -1 || resulttemp == 2 ){
result = new StringBuilder("0");
//最后是int0就不能追加0,小数才行
} else if((getLastNumber(result.toString())).equals("0")){
resulttemp = 1;
break;
}
else result.append("0");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_1:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*1");
else if(resulttemp == -1 || resulttemp == 2 ){
result = new StringBuilder("1");
}
else if(result.toString().equals("0")){
result = new StringBuilder("1");
}
else result.append("1");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_2:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*2");
else if(resulttemp == -1 || resulttemp == 2){
result = new StringBuilder("2");
}
else if(result.toString().equals("0")){
result = new StringBuilder("2");
}
else result.append("2");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_3:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*3");
else if(resulttemp == -1 || resulttemp == 2 ){
result = new StringBuilder("3");
}
else if(result.toString().equals("0")){
result = new StringBuilder("3");
}
else result.append("3");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_4:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*4");
else if(resulttemp == -1 || resulttemp == 2){
result = new StringBuilder("4");
}
else if(result.toString().equals("0")){
result = new StringBuilder("4");
}
else result.append("4");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_5:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*5");
else if(resulttemp == -1 || resulttemp == 2){
result = new StringBuilder("5");
}
else if(result.toString().equals("0")){
result = new StringBuilder("5");
}
else result.append("5");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_6:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*6");
else if(resulttemp == -1 || resulttemp == 2){
result = new StringBuilder("6");
}
else if(result.toString().equals("0")){
result = new StringBuilder("6");
}
else result.append("6");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_7:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*7");
else if(resulttemp == -1 || resulttemp == 2){
result = new StringBuilder("7");
}
else if(result.toString().equals("0")){
result = new StringBuilder("7");
}
else result.append("7");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_8:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*8");
else if(resulttemp == -1 || resulttemp == 2){
result = new StringBuilder("8");
}
else if(result.toString().equals("0")){
result = new StringBuilder("8");
}
else result.append("8");
result_text.setText(result.toString());
resulttemp = 1;
break;
case R.id.text_9:
if(result.length()!=0 && result.charAt(result.length()-1) ==')') result.append("*9");
else if(resulttemp == -1 || resulttemp == 2){
result = new StringBuilder("9");
}
else if(result.toString().equals("0")){
result = new StringBuilder("9");
}
else result.append("9");
result_text.setText(result.toString());
resulttemp = 1;
break;
default:
result_text.setText("运算式错误!");
result = new StringBuilder("0");
result_text.setText(result.toString());
break;
}
}
private void initView(){
//result = new StringBuilder(result_text.getText().toString());
text_clear=(TextView)findViewById(R.id.text_clear);
text_back=(TextView)findViewById(R.id.text_back);
text_divi=(TextView)findViewById(R.id.text_divi);
text_multiply=(TextView)findViewById(R.id.text_multiply);
text_minus=(TextView)findViewById(R.id.text_minus);
text_plus=(TextView)findViewById(R.id.text_plus);
text_equals=(TextView)findViewById(R.id.text_equals);
text_leftbracket=(TextView)findViewById(R.id.text_leftbracket);
text_rightbracket=(TextView)findViewById(R.id.text_rightbracket);
text_point=(TextView)findViewById(R.id.text_point);
text_0=(TextView)findViewById(R.id.text_0);
text_1=(TextView)findViewById(R.id.text_1);
text_2=(TextView)findViewById(R.id.text_2);
text_3=(TextView)findViewById(R.id.text_3);
text_4=(TextView)findViewById(R.id.text_4);
text_5=(TextView)findViewById(R.id.text_5);
text_6=(TextView)findViewById(R.id.text_6);
text_7=(TextView)findViewById(R.id.text_7);
text_8=(TextView)findViewById(R.id.text_8);
text_9=(TextView)findViewById(R.id.text_9);
history_text=(TextView)findViewById(R.id.history_text);
result_text=(TextView)findViewById(R.id.result_text);
for (TextView textView : Arrays.asList(
text_clear,
text_back,
text_divi,
text_multiply,
text_minus,
text_plus,
text_equals,
text_point,
text_leftbracket,
text_rightbracket,
text_0,
text_1,
text_2,
text_3,
text_4,
text_5,
text_6,
text_7,
text_8,
text_9)) {
textView.setOnClickListener(this);
}
}
}
package com.example.calculator;
public class Core_Algorithm {
public static boolean isOpeartor(Character c) {
return c.equals('+') || c.equals('-') || c.equals('/') || c.equals('*');
}
//
// //方法:将中缀表达式转成对应的List
public static List<String> toInfixExpressionList(String s) {
//定义一个List,存放中缀表达式对应的内容
List<String> ls = new ArrayList<>();
StringBuilder str;//对多位数的拼接
char c;//每遍历到一个字符,就放入到c
int i = 0;
int countofleftbracket = 0;
int countofrightbracket = 0;
//如果c是一个非数字,就需要加入到ls
//'0'[48] ->'9'[57]
while (i < s.length()) {
if (s.charAt(i) == '(') countofleftbracket++;
else if (s.charAt(i) == ')') countofrightbracket++;
if (s.charAt(i) >= 48 && s.charAt(i) <= 57) {
str = new StringBuilder();
//下一位数字或者小数点就加进一个字符串
while ((i < s.length() && s.charAt(i) >= 48 && (c = s.charAt(i)) <= 57) || (i < s.length() && (c = s.charAt(i)) == '.')) {
str.append(c);
i++;
}
ls.add(str.toString());
} else if ((c = s.charAt(i)) == '-' && i == 0) {
str = new StringBuilder("-");
i++;
while ((i < s.length() && s.charAt(i) >= 48 && (c = s.charAt(i)) <= 57) || (i < s.length() && (c = s.charAt(i)) == '.')) {
str.append(c);
i++;
}
ls.add(str.toString());
}
else {
//(-视为*-1
if(i>=1 && (c = s.charAt(i)) == '-' && (c= s.charAt(i-1))=='('){
ls.add("" + -1);
ls.add("*");
i++;
//有左括号且不在第0位视为*(
}else
if(i>=1 && (c = s.charAt(i)) == '(' && Character.isDigit(s.charAt(i-1))){
ls.add("" + c);
ls.add("*");
i++;
}else {
ls.add("" + c);
i++;
}
}
if (countofleftbracket < countofrightbracket) {
ls.clear();
ls.add("右括号数目错误!");
return ls;
}
}
//补齐右括号
while (countofleftbracket > countofrightbracket) {
ls.add(")");
countofrightbracket++;
}
return ls;
}
//中缀转后缀
//后缀表达式中不能有括号
public static List<String> parseSuffixExpressionList(List<String> ls) {
//定义两个栈
Stack<String> s1 = new Stack<>();//符号栈
//说明:因为s2这个栈,在整个转换过程中没有pop操作,而且后面我们还需要逆序输出
//因此我们不用Stack,直接使用Lists2
//Stack s2 = new Stack();//存储中间结果的栈s2
List<String> s2 = new ArrayList<>();//存储中间结果的List2
//遍历ls
for (String item : ls) {
if (!isNumber(item) && !item.equals("+") && !item.equals("-") && !item.equals("*")
&& !item.equals("/") && !item.equals("(") && !item.equals(")")) {
ls.clear();
ls.add("右括号数目错误!");
return ls;
}
//如果是一个数,加入s2
if (isNumber(item)) {
s2.add(item);
} else if (item.equals("(")) {
s1.push(item);
} else if (item.equals(")")) {
//如果是右括号时)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号 丢弃
while (!s1.peek().equals("(")) {
s2.add(s1.pop());
}
s1.pop();//!!!将(弹出s1栈,消除左括号
} else {
//当item的优先级小于等于栈顶运算符,将s1栈顶的运算符弹出并加入到s2中,再次转到(4.1)与s1中新的栈顶运算符相比较;
//问题:我们缺少一个比较优先级高低的方法
while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)) {
s2.add(s1.pop());
}
//还需要将item压入栈
s1.push(item);
}
}
//将s1中剩余的元素依次弹出并加入s2
while (!s1.isEmpty()) {
if (s1.peek().equals("(")) s1.pop();
else s2.add(s1.pop());
}
//因为存放到List,因此按顺序输出就是对应的后缀表达式对应List
return s2;
}
//计算后缀表达式
public static String calculate(List<String> list) {
if(list.isEmpty()){
return "运算符错误!1";
}
ArrayList<String> arr = new ArrayList<>();
for(int i = 0; i < list.size(); i++){
try {
int j = arr.size();
switch (list.get(i)) {
case "+":
//这里注意:remove后arr.size少一个所以第二个j-2可理解为arr.size()-1-1
BigDecimal a = new BigDecimal(arr.remove(j - 2)).add(new BigDecimal(arr.remove(j - 2)));
arr.add(String.valueOf(a));
break;
case "-":
BigDecimal b = new BigDecimal(arr.remove( j- 2)).subtract(new BigDecimal(arr.remove(j - 2)));
arr.add(String.valueOf(b));
break;
case "*":
BigDecimal c = new BigDecimal(arr.remove(j - 2)).multiply(new BigDecimal(arr.remove(j - 2)));
arr.add(String.valueOf(c));
break;
case "/":
if (Double.parseDouble(arr.get(arr.size() - 1)) == 0) {
return "除数不能为0";
}
BigDecimal d = new BigDecimal(arr.remove(j - 2)).divide
(new BigDecimal(arr.remove(j - 2)), 20, BigDecimal.ROUND_HALF_UP);
arr.add(String.valueOf(d));
break;
default:
if (isNumber(list.get(i))) {
arr.add(list.get(i));
} else {
return "运算符错误!2";
}
break;
}
}catch (Exception e){
return "运算符错误!3";
}
}
String rt = arr.get(0);
if(rt.length()==1)
return rt;
if(rt.matches("^[\\\\+\\\\-]?([0-9]*[.][0-9]*)$")) { //匹配是否是小数
int i = rt.length() - 1; //匹配成功的去除末尾的0
while (rt.charAt(i) == '0')
i--;
if (rt.charAt(i) == '.')
i--;
rt = rt.substring(0, i + 1);
}
if(Double.parseDouble(rt) == 0){
return "0";
}
return rt;
}
/**
* judge the string whether is the number
*
* @param num the source number
* @return true if the string is number, else return false
*/
public static boolean isNumber(String num) {
if (num.length() > 0) {
char ch = num.charAt(0);
if (ch == '-')
return !num.equals("-");
else
return num.charAt(0) >= 48 && num.charAt(0) <= 57;
}
return false;
}
public static void main(String[] args) {
// String expression = "1.6+((2+3)*4)-5";
// List infixExpressionList = toInfixExpressionList(expression);//ArrayList [1,+,(,(,2,+,3,),*,4,),-,5]
// System.out.println("中缀表达式对应的List"+infixExpressionList);
// List suffixExpressionList = parseSuffixExpressionList(infixExpressionList);
// System.out.println("后缀表达式对应的List"+suffixExpressionList);//ArrayList[ 1,2,3,+,4,*,+,5,-]
// System.out.println("后缀表达式结果:"+Core_Algorithm.calculate(suffixExpressionList));
String expression = "-5(5(5(5";
List<String> infixExpressionList = toInfixExpressionList(expression);
System.out.println("中缀表达式对应的List" + infixExpressionList);
List<String> suffixExpressionList = parseSuffixExpressionList(infixExpressionList);
System.out.println("后缀表达式对应的List" + suffixExpressionList);
System.out.println("后缀表达式结果:" + Core_Algorithm.calculate(suffixExpressionList));
}
public static String getLastNumber(String str)
{
Stack<Character> stack = new Stack<>();
int index = str.length() - 1;
for (int i = index; i >= 0; i--)
{
char ch = str.charAt(i);
if(isNumber(String.valueOf(ch)) || ch == '.')
stack.push(ch);
else
break;
}
StringBuilder ret = new StringBuilder();
while(!stack.isEmpty())
{
ret.append(stack.pop());
}
return ret.toString();
}
}
//Operation返回一个运算符对应的优先级
class Operation {
//写一个方法,返回对应的优先级数字
public static int getValue(String opeartion) {
int res = 0;
int ADD = 1;
int MINUS = 1;
int MUL = 2;
int DIV = 2;
switch (opeartion) {
case "+":
res = ADD;
break;
case "-":
res = MINUS;
break;
case "*":
res = MUL;
break;
case "/":
res = DIV;
break;
default:
break;
}
return res;
}
}