android-计算器实现两位数的加减乘除运算

注:以下计算器只注重实现功能,不考虑其他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"
                />
            <
package ymq.demo03; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.widget.*; public class demo03 extends Activity { /** Called when the activity is first created. */ String str=""; EditText et; int c=0,flag=0; double b=0.0,g=0.0,f=0.0; View vi; public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, 1, 1, "退出"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==1){finish();} return super.onOptionsItemSelected(item); } //计算方法 public double calculater(){ switch(c){ case 0:f=g;break; case 1:f=b+g;break; case 2:f=b-g;break; case 3:f=b*g;break; case 4:f=b/g;break; } b=f; c=0; return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得按键 final Button number[]=new Button[10]; final Button fuhao[]=new Button[11]; fuhao[0]=(Button)findViewById(R.id.button01); fuhao[1]=(Button)findViewById(R.id.button02); fuhao[2]=(Button)findViewById(R.id.button03); fuhao[3]=(Button)findViewById(R.id.button04); fuhao[4]=(Button)findViewById(R.id.button05); fuhao[5]=(Button)findViewById(R.id.button06); fuhao[6]=(Button)findViewById(R.id.buttonce); fuhao[7]=(Button)findViewById(R.id.buttonc); fuhao[8]=(Button)findViewById(R.id.zheng); fuhao[9]=(Button)findViewById(R.id.kaifang); fuhao[10]=(Button)findViewById(R.id.pingfang); number[0]=(Button)findViewById(R.id.button0); number[1]=(Button)findViewById(R.id.button1); number[2]=(Button)findViewById(R.id.button2); number[3]=(Button)findViewById(R.id.button3); number[4]=(Button)findViewById(R.id.button4); number[5]=(Button)findViewById(R.id.button5); number[6]=(Button)findViewById(R.id.button6); number[7]=(Butto
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值