注:以下计算器只注重实现功能,不考虑其他BUG,只有两位整数的算法运算,适合新手
1、实现思想
将从键盘得到的数值放在一个字符数组中,以运算符号(+-/)为分割点,将两个数值分割开,进行算法运算。*
2、难点
如何判断是否为符号?+ - ×/
记录符号的位置?
3、步骤:
1、得到键盘输入的值
2、将值存放在一个字符数组中
3、遍历数组中的每个数,如果找到算法符号,记录下算法符号的位置。(要点,从0开始)
4、将算法符号前面的数放在一个定义的int型数中
5、同理
6、判断是加减乘除的哪一个方法,然后进行简单的运算。
4、代码
i:布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etResult"
android:layout_weight="0.05"
android:textSize="25dp"
android:paddingTop="10dp"
android:gravity="bottom"
android:hint="0.0"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:textSize="25dp"
android:background="@color/colorWhite"
android:id="@+id/btnQingchu"
android:layout_weight="0.5" />
<Button
android:layout_width="235dp"
android:layout_height="wrap_content"
android:text="←"
android:textSize="25dp"
android:background="@color/colorBlue"
android:id="@+id/btnHuishan"
android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn7"
android:text="7"
android:textSize="25dp"
android:layout_weight="0.25"
/>
<