在好例子网,分享、交流、成长!
您当前所在位置:首页Flash 开发实例ActionScript语言基础 → AndroidStudio之计算器(入门级)

AndroidStudio之计算器(入门级)

ActionScript语言基础

下载此实例
  • 开发语言:Flash
  • 实例大小:12.98M
  • 下载次数:35
  • 浏览次数:3227
  • 发布时间:2019-06-25
  • 实例类别:ActionScript语言基础
  • 发 布 人:吾爱咯
  • 文件格式:.zip
  • 所需积分:1
 相关标签: 计算器

同类人气实例

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

package com.example.dell.calculator;


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;
    boolean clear_flag ;//清空标识
    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:
                if (clear_flag) {
                    clear_flag =false ;
                    str ="" ;
                    output.setText("");
                }else if (str!=null&&!str.equals("")){
                    output.setText(str.substring(0,str.length()-1));
                }
                break;
            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];
    }
};

标签: 计算器

实例下载地址

AndroidStudio之计算器(入门级)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警