Android计算器简单逻辑实现
Android计算器简单逻辑实现
引言:
我的android计算器的实现方式是:按钮输入一次,就处理一次。
但是如果你学过数据结构(栈),就可以使用表达式解析(前缀,后缀)处理。
而这个方式已经很成熟了,但是时间有限,只完成了这个简单的计算器。
至于,这个Android的布局已经在我博客中发布了,不再讲述。
package com.example.androidlessontwo; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity { private Button[] buttonNum=new Button[11];
private Button[] buttonComand=new Button[5];
private TextView input=null;
private TextView rl=null;
private Button buttonClear=null;
private boolean firstFlag=true;
private double result=0.0;
private String lastCommand; public void MyCalculator()
{
result = 0.0;
firstFlag=true;
lastCommand="=";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonNum[0]=(Button) findViewById(R.id.num0);
buttonNum[1]=(Button) findViewById(R.id.num1);
buttonNum[2]=(Button) findViewById(R.id.num2);
buttonNum[3]=(Button) findViewById(R.id.num3);
buttonNum[4]=(Button) findViewById(R.id.num4);
buttonNum[5]=(Button) findViewById(R.id.num5);
buttonNum[6]=(Button) findViewById(R.id.num6);
buttonNum[7]=(Button) findViewById(R.id.num7);
buttonNum[8]=(Button) findViewById(R.id.num8);
buttonNum[9]=(Button) findViewById(R.id.num9);
buttonNum[10]=(Button) findViewById(R.id.point); buttonComand[0]=(Button) findViewById(R.id.add);
buttonComand[1]=(Button) findViewById(R.id.sub);
buttonComand[2]=(Button) findViewById(R.id.ride);
buttonComand[3]=(Button) findViewById(R.id.divide);
buttonComand[4]=(Button) findViewById(R.id.equal); input=(TextView) findViewById(R.id.input);
rl =(TextView) findViewById(R.id.rl);
buttonClear=(Button) findViewById(R.id.clean); NumberAction na= new NumberAction();
CommandAction ca=new CommandAction();
for(Button bc:buttonComand)
{
bc.setOnClickListener(ca);
}
for(Button bc:buttonNum)
{
bc.setOnClickListener(na);
}
buttonClear.setOnClickListener(new Button.OnClickListener()
{ @Override
public void onClick(View v) {
MyCalculator();
rl.setText("0.0");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class NumberAction implements Button.OnClickListener
{ @Override
public void onClick(View view)
{
Button btn = (Button)view;
String inputTemp =btn.getText().toString();//
input.setText(input.getText().toString()+inputTemp);
double numtemp = 0;
switch(btn.getId())
{
case R.id.num0:
{
if(firstFlag)
{
result=result*10+0;
firstFlag=false;
}
else
numtemp=numtemp*10+0;
break;
}
case R.id.num1:
{
if(firstFlag)
{
result=result*10+1;
firstFlag=false;
}
else
numtemp=numtemp*10+1;
break;
}
case R.id.num2:
{
if(firstFlag)
{
result=result*10+2;
firstFlag=false;
}
else
numtemp=numtemp*10+2;
break;
}
case R.id.num3:
{
if(firstFlag)
{
result=result*10+3;
firstFlag=false;
}
else
numtemp=numtemp*10+3;
break;
}
case R.id.num4:
{
if(firstFlag)
{
result=result*10+4;
firstFlag=false;
}
else
numtemp=numtemp*10+4;
break;
}
case R.id.num5:
{
if(firstFlag)
{
result=result*10+5;
firstFlag=false;
}
else
numtemp=numtemp*10+5;
break;
}
case R.id.num6:
{
if(firstFlag)
{
result=result*10+6;
firstFlag=false;
}
else
{
numtemp=numtemp*10+6;
calculate(numtemp);
}
break;
}
case R.id.num7:
{
if(firstFlag)
{
result=result*10+7;
firstFlag=false;
}
else
{
numtemp=numtemp*10+7;
calculate(numtemp);
}
break;
}
case R.id.num8:
{
if(firstFlag)
{
result=result*10+8;
{
result=result*10+8;
firstFlag=false;
}
}
else
{
numtemp=numtemp*10+8;
calculate(numtemp);
}
break;
}
case R.id.num9:
{
if(firstFlag)
{
result=result*10+9;
firstFlag=false;
}
else
{
numtemp=numtemp*10+9;
calculate(numtemp);
}
break;
}
} } } private class CommandAction implements Button.OnClickListener
{
@Override
public void onClick(View v)
{
Button btn=(Button)v;
String inputCommand=(String)btn.getText();
switch(btn.getId())
{
case R.id.add:
{
lastCommand="+";
break;
}
case R.id.sub:
{
lastCommand="-";
break;
}
case R.id.ride:
{
lastCommand="*";
break;
}
case R.id.divide:
{
lastCommand="/";
break;
}
case R.id.equal:
{
lastCommand="=";
input.setText("");
rl.setText(String.valueOf(result));
return ;
} }
input.setText(input.getText()+inputCommand);
} }
private void calculate(double x)
{ if(lastCommand.equals("+"))
{
result += x;
} if(lastCommand.equals("-"))
{
result -= x;
} if(lastCommand.equals("*"))
{
result *= x;
} if(lastCommand.equals("/"))
{
result /= x;
}
} }
Android计算器简单逻辑实现的更多相关文章
- C#实现任意大数的计算和简单逻辑命题的证明——前言
介绍 这是本人毕业设计的项目,一直想将其整理成文,可一不小心4年就过去了(这个时间又可以读个大学了).现在给自己定一个目标,一个月时间里将项目的所有关键点都整理出来.不然真怕一眨眼又一个4年过去了,而 ...
- Android实现简单音乐播放器(MediaPlayer)
Android实现简单音乐播放器(MediaPlayer) 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个简单的音乐播放器,要求功能 ...
- Android发展简单介绍
Android一词的本义指“机器人”,同一时候也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,号称是首个为移动 ...
- Android实现简单音乐播放器(startService和bindService后台运行程序)
Android实现简单音乐播放器(MediaPlayer) 开发工具:Andorid Studio 1.3运行环境:Android 4.4 KitKat 工程内容 实现一个简单的音乐播放器,要求功能有 ...
- Android 实现简单音乐播放器(二)
在Android 实现简单音乐播放器(一)中,我介绍了MusicPlayer的页面设计. 现在,我简单总结一些功能实现过程中的要点和有趣的细节,结合MainActivity.java代码进行说明(写出 ...
- Android 实现简单音乐播放器(一)
今天掐指一算,学习Android长达近两个月了,今天开始,对过去一段时间的学习收获以及遇到的疑难杂症做一些总结. 简单音乐播放器是我自己完成的第一个功能较为完整的APP,可以说是我的Android学习 ...
- Android课程---Android Studio简单设置
Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings-->Appearance-->Theme, ...
- Android实现简单拨号器
Android实现简单拨号器 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 界面布局只有GridLayout和EditText两个控件,全部 ...
- 【转】Android Studio简单设置
原文网址:http://ask.android-studio.org/?/article/14 Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以 ...
随机推荐
- 【Linux学习】Linux文件系统2—linux常用目录结构、绝对路径、相对路径
Linux文件系统2-linux常用目录结构.绝对路径.相对路径 一. 常见目录结构总结 Linux目录结构就是"树形结构",常见的目录结构: /bin 系统需要的命令位于此目录 ...
- css 3d旋转
- 进击python第4篇:初探模块
模块,用一砣代码实现了某个功能的代码集合,任何python程序都可以作为模块导入,n个 .py 文件组成的代码集合就称为模块. but 为什么要引入模块概念?主要原因是代码重用(code reuse) ...
- poj3276 Face The Right Way
Face The Right Way POJ - 3276 题目大意: n头牛排成一列,每头牛向前或向后,为了让所有牛都面向前方,设定一个k值,每操作一次恰好使k头连续的牛转向,求最少的操作次数m和对 ...
- [Xcode 实际操作]八、网络与多线程-(20)时间控件Timer定时功能
目录:[Swift]Xcode实际操作 本文将演示时间控件Timer定时功能的使用. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit cl ...
- [Xcode 实际操作]九、实用进阶-(26)对Storyboard(故事版)中的文字标签(Label)进行本地化处理
目录:[Swift]Xcode实际操作 对Storyboard(故事版)中的文字标签(Label)进行本地化处理. 点击项目名称[DemoApp]进入项目信息面板. [Build Setting]-& ...
- PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)
嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...
- 数据库 | 远程连接centos7上数据库
用root身份进入远程服务器控制台: 进入Mysql命令: # mysql -uroot -p 或者在本地上连接到远程主机上的MySQL: 假设远程主机的IP为:10.0.0.1,用户名为root,密 ...
- library not found for -lXXXXX 编译问题的解决方法
喜欢交朋友的加:微信号 dwjluck2013 编译和真机调试的时候都正常 在打包的时候遇到这个问题 解决方案:pod install 重新下载就解决了 分享给遇到同类问题的小伙伴
- Codeforces 1114E(简单交互)
这里有一道老实题,大家快来踩爆它! 交互题:根据你的输出决定下一次的输入. 请听题: 管理员有个乱序数列(举例:{14, 24, 9, 19}),排序以后是个等差数列({9, 14, 19, 24}) ...