【Android】AndroidStudio的简单计算器(带优先级)开发小程序~

写了一个简单的AndroidStudio的计算器程序。(带优先级的)

把代码贴一下,相互学习。

 

成果展示图片:

 

开机时:

 

 

一级运算:

 

 

 二级运算:

 

 

含优先级运算:

 

 

自动更新末尾运算符:

 

 

输入错误(自动纠错):

贴代码处~:(懒得写注释了,如果有问题可以留言问一下~)

    大致项目思路,就是布局-》更改ID-》创建变量-》添加事件-》填写事件!

    计算器的算法思路是:自己手写一个字符栈以及数值栈,设置好优先级,在运算符的字符栈底装在最低优先级的‘#’字符,然后再待计算的算式末尾添加‘#’字符,分别用于运算符的比较以及运算的结束。

以下是代码~~

activity_XML代码,即布局代码:

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.administrator.calculatororororooror.MainActivity">

    <EditText
        android:id="@+id/output"
        android:text="What the hell???!!"
        android:layout_width="300dp"
        android:layout_height="60dp"
        android:layout_marginTop="40dp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:editable="false"
        android:background="#fffbaf"
        />

    <Button
        android:id="@+id/jia"
        android:text="+"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_marginTop="50dp"
        android:layout_marginRight="110dp"
        android:layout_alignRight="@+id/jian"
        android:layout_below="@id/output"
        android:background="#ff0900"/>

    <Button
        android:id="@+id/jian"
        android:text="-"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/jia"
        android:layout_centerHorizontal="true"
        android:background="#ff0900"/>

    <Button
        android:id="@+id/chen"
        android:text="*"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@+id/jia"
        android:layout_marginLeft="40dp"
        android:layout_toRightOf="@id/jian"
        android:background="#ff0900"/>

    <Button
        android:id="@+id/id7"
        android:text="7"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_below="@id/jia"
        android:layout_marginRight="110dp"
        android:layout_alignRight="@id/jian"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/id8"
        android:text="8"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/id7"
        android:layout_centerHorizontal="true"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/id9"
        android:text="9"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/id7"
        android:layout_marginLeft="40dp"
        android:layout_toRightOf="@id/jian"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/id4"
        android:text="4"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_below="@id/id7"
        android:layout_marginRight="110dp"
        android:layout_alignRight="@id/jian"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/id5"
        android:text="5"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/id4"
        android:layout_centerHorizontal="true"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/id6"
        android:text="6"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/id4"
        android:layout_marginLeft="40dp"
        android:layout_toRightOf="@id/jian"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/id1"
        android:text="1"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_below="@id/id4"
        android:layout_marginRight="110dp"
        android:layout_alignRight="@id/jian"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/id2"
        android:text="2"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/id1"
        android:layout_centerHorizontal="true"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/id3"
        android:text="3"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/id1"
        android:layout_marginLeft="40dp"
        android:layout_toRightOf="@id/jian"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/clear"
        android:text="AC"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_below="@id/id1"
        android:layout_marginRight="110dp"
        android:layout_alignRight="@id/jian"
        android:background="#ff0900"/>

    <Button
        android:id="@+id/id0"
        android:text="0"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/clear"
        android:layout_centerHorizontal="true"
        android:background="#01aaff"/>

    <Button
        android:id="@+id/cal"
        android:text="="
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/clear"
        android:layout_marginLeft="40dp"
        android:layout_toRightOf="@id/jian"
        android:background="#ff8001"/>

</RelativeLayout>

 

 

 

MainActivity.java代码(即计算器代码):

 

package com.example.administrator.calculatororororooror;

import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //这里只能加声明,并不能使用函数
    EditText output;
    Button id0;
    Button id1;
    Button id2;
    Button id3;
    Button id4;
    Button id5;
    Button id6;
    Button id7;
    Button id8;
    Button id9;
    Button jia;
    Button jian;
    Button chen;
    //Button chu;
    //Button del;
    Button clear;
    Button cal;
    iStack s1=new iStack(); //s1是值栈,s2是符号栈
    cStack s2=new cStack();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        output=(EditText)findViewById(R.id.output);
        id0=(Button)findViewById(R.id.id0);
        id1=(Button)findViewById(R.id.id1);
        id2=(Button)findViewById(R.id.id2);
        id3=(Button)findViewById(R.id.id3);
        id4=(Button)findViewById(R.id.id4);
        id5=(Button)findViewById(R.id.id5);
        id6=(Button)findViewById(R.id.id6);
        id7=(Button)findViewById(R.id.id7);
        id8=(Button)findViewById(R.id.id8);
        id9=(Button)findViewById(R.id.id9);
        jia=(Button)findViewById(R.id.jia);
        jian=(Button)findViewById(R.id.jian);
        chen=(Button)findViewById(R.id.chen);
        //chu=(Button)findViewById(R.id.chu);
        //del=(Button)findViewById(R.id.del);
        clear=(Button)findViewById(R.id.clear);
        cal=(Button)findViewById(R.id.cal);

        id0.setOnClickListener(this);   //为此,我的类加了个接口
        id1.setOnClickListener(this);
        id2.setOnClickListener(this);
        id3.setOnClickListener(this);
        id4.setOnClickListener(this);
        id5.setOnClickListener(this);
        id6.setOnClickListener(this);
        id7.setOnClickListener(this);
        id8.setOnClickListener(this);
        id9.setOnClickListener(this);
        jia.setOnClickListener(this);
        jian.setOnClickListener(this);
        chen.setOnClickListener(this);
        //chu.setOnClickListener(this);
        clear.setOnClickListener(this);
        //del.setOnClickListener(this);
        cal.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String str=output.getText().toString();
       switch(v.getId()) {
           case R.id.jia:
           case R.id.jian:
           case R.id.chen:
           //case R.id.chu:
               int len=str.length();
               if(len!=0)
               {
                   char lastcode=str.charAt(len-1);
                   if(lastcode<48||lastcode>57)
                       str=str.substring(0,len-1);
               }
           case R.id.id0:
           case R.id.id1:
           case R.id.id2:
           case R.id.id3:
           case R.id.id4:
           case R.id.id5:
           case R.id.id6:
           case R.id.id7:
           case R.id.id8:
           case R.id.id9:
               str=str+((Button)v).getText();
               output.setText(str);
               break;
           case R.id.clear:
               str="";
               output.setText(str);
               break;
           //case R.id.del:
           case R.id.cal:
               getAns();
               break;
       }
    }
    public void getAns()
    {
        String str=output.getText().toString()+'#';
        int len=str.length();
        if(len==1)return;
        if(str.charAt(0) < 48 || str.charAt(0) > 57)
        {
            str=str.substring(1,len-1);
            output.setText(str);
            return;
        }
        if(str.charAt(len-2) < 48 || str.charAt(len-2) > 57)
        {
            str=str.substring(0,len-2);
            output.setText(str);
            return;
        }
        s2.push('#');
        int n=0;    //n是表示扫描到的数字值为多少,初始化为0
        boolean book=false; //没办法,用来当做n取值的有效值,true有效
        for(int i=0;i<len;i++) {
            char c = str.charAt(i);
            if (c >= 48 && c <= 57) {
                n = n * 10 + c - 48;
                book = true;
            } else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '#' ) {
                if (book == true) {
                    s1.push(n); //遇到符号就入栈,并清零
                    n = 0;
                    book = false;
                }
                if (compare(c)) {    //如果当前优先级要低就开始计算
                    int n2 = s1.top();    //3+5,则n1为3,n2为5
                    s1.pop();
                    ;
                    int n1 = s1.top();
                    s1.pop();
                    ;

                    char op = s2.top();
                    s2.pop();
                    int ans = 0;
                    switch (op) {
                        case '+':
                            ans = n1 + n2;
                            break;
                        case '-':
                            ans = n1 - n2;
                            break;
                        case '*':
                            ans = n1 * n2;
                            break;
                        case '/':
                            ans = n1 / n2;
                            break;
                    }
                    s1.push(ans);
                    i--;    //下一轮继续比
                } else {
                    s2.push(c);
                }
            }
            else {
                output.setText("");
                return;
            }
        }
        str=s1.top()+"";
        output.setText(str);
        s1.clear();
        s2.clear();
    }
    boolean compare(char t)
    {
        int n1,n2;      //n1是栈顶的优先级,n2是当前扫到符号的优先级,#为0,+、-为1,*、/为2
        if(t=='*'||t=='/')n2=2;
        else if(t=='+'||t=='-') n2=1;
        else n2=0;
        t=s2.top();
        if(t=='*'||t=='/')n1=2;
        else if(t=='+'||t=='-') n1=1;
        else n1=0;
        return n2<n1;   //当前符号优先级小于栈顶元素的时候开始计算
    }
}
class iStack{
    private int value[]=new int[100];
    private int top;
    iStack()
    {
        top=-1;
    }
    public void push(int c)
    {
        value[++top]=c;
    }
    public void pop()
    {
        top--;
    }
    public boolean empty()
    {
        return top==-1?true:false;
    }
    public void clear()
    {
        top=-1;
    }
    public int top()
    {
        return value[top];
    }
};
class cStack{
    private char value[]=new char[100];
    private int top;
    cStack()
    {
        top=-1;
    }
    public void push(char c)
    {
        value[++top]=c;
    }
    public void pop()
    {
        top--;
    }
    public boolean empty()
    {
        return top==-1?true:false;
    }
    public void clear()
    {
        top=-1;
    }
    public char top()
    {
        return value[top];
    }
};

 

 

 

       

 

### 回答1: 要使用Android Studio制作一个计算器,需要遵循以下步骤: 1. 创建一个新的Android项目,并选择空白活动模板。 2. 在布局文件中添加一个TextView和多个Button,用于显示计算器的输入和输出。 3. 在MainActivity.java文件中,定义按钮的点击事件,并编写计算器的逻辑代码。 4. 在适当的位置添加异常处理代码,以确保计算器在输入错误时不会崩溃。 5. 运行应用程序,并测试计算器的功能。 总之,制作一个计算器需要一定的编程知识和技能,但是使用Android Studio可以使这个过程更加简单和高效。 ### 回答2: Android Studio 是一个非常流行的 Android 开发工具,它可以帮助开发者快速地创建 Android 应用程序。在这篇文章中,我将向您介绍如何使用 Android Studio 创建一个简单计算器应用程序,并探讨一些您需要了解的基础知识。 1. 创建新项目 首先,您需要创建一个新项目。在 Android Studio 中,点击 “File” 菜单,然后选择 “New Project”。在创建项目过程中,您需要为您的项目指定一个应用程序名称,选择一个适当的包名和提交一个空白的 Activity。 2. 设计界面 在项目创建后,您需要开始设计应用程序的用户界面。使用 Android Studio 的布局编辑器可以轻松快速地设计您的界面。在布局编辑器中,可以将各种元素拖动到您的布局中,例如 EditText、TextView 和 Button 等。 3. 计算器逻辑 在设计好界面后,您需要开始编写计算器的逻辑。在 Android 应用程序中,可以使用 Java 或 Kotlin 编写代码。在计算器应用程序中,您需要为各个按钮添加单击事件,并编写计算器逻辑以实现各种计算操作。 例如,在点击加号按钮时,您需要将用户输入的第一个数字添加到第二个数字,并将结果显示在界面上。在完成所有必要的操作后,您需要测试您的应用程序以确保它能够正常工作。 4. 测试应用程序 在开发应用程序时,测试是非常重要的。您可以使用模拟器来测试您的应用程序,也可以将应用程序安装在 Android 设备上进行测试。在测试过程中,您需要确保应用程序的各项功能都能够正常工作,并且没有任何错误或问题。 总之,使用 Android Studio 创建计算器应用程序非常简单。您只需要创建一个新项目、设计界面、实现计算器逻辑并进行测试即可。通过了解这些基础知识,您可以开始了解 Android 开发,并创建您自己的应用程序。 ### 回答3: Android Studio是当前最流行的Android开发工具,其强大的功能和易用的界面受到了众多开发者的青睐。在Android Studio上实现一个简单计算器是一个非常好的学习Android编程的方法,可以让我们更好地了解如何构建一个Android应用程序。 以下是如何使用Android Studio制作一个计算器的步骤: 1. 创建新项目 我们首先需要在Android Studio里创建一个新项目。打开Android Studio,点击“Start a new Android Studio project”,然后根据提示填写应用程序的各项信息。在“Add an activity to Mobile”界面上选择“Empty Activity”,然后单击“Finish”按钮以完成创建新项目的步骤。 2. 设计用户界面 接下来,我们需要设计计算器的用户界面。在Android Studio中,可以使用XML文件来布局UI界面。找到“activity_main.xml”文件并打开它,然后使用Android Studio提供的视图控件(如TextView、EditText、Button等)设计计算器的用户界面,设置UI布局和颜色等。 3. 实现计算器逻辑 在我们完成设计好界面后,需要编写Java代码来实现计算器逻辑。在Android Studio中,所有的Java代码都在“src”目录下的“main/java”文件夹中。创建一个名为“MainActivity.java”的类,并引入所需的包,开发“MainActivity.java”类中的计算逻辑。 4. 运行应用程序 在以上步骤完成后,可以点击Android Studio的“Run”按钮来启动应用程序并测试计算器功能。如果一切正常,计算器将显示在模拟器或真实设备的屏幕上,用户可以通过UI界面进行数字和计算操作。 总的来说,在Android Studio中制作一个简单计算器需要遵循以上四个基本步骤。实际上,Android Studio还提供了许多其他功能和工具,例如使用Gradle自动构建、使用调试器等,可以帮助开发者更方便地进行应用程序开发。无论您是初学者还是有经验的开发人员,建议您在从事Android开发之前掌握Android Studio的使用方法。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值