所使用的算法:表达式求值(中缀表达式转后缀表达式,后缀表达式求值值)

不如何设计接口,有时间来美化!

MainActivity.java

package com.example.calculator;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
String mid=null;
TextView textView=null;
//操作符栈
static LinkedList<String> opStack=new LinkedList<String>();
//优先级映射
static Map<String, Integer> priority=new HashMap<String, Integer>(){
{
put("(", 0);
put(")", 0);
put("+", 1);
put("-", 1);
put("×", 2);
put("÷",2);
}
};
public void init(){
textView=(TextView) findViewById(R.id.textView);
textView.setTextSize(30); }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init(); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void delete(View view){
String text = (String) textView.getText();
if(text!=null&&text.length()!=0){
text=text.substring(0, text.length()-1);
textView.setText(text);
}
}
public void clear(View view){
textView.setText("");
}
public void showText(View view){
Button bt=(Button)view;
String s = (String) bt.getText();
//Toast.makeText(this, s, Toast.LENGTH_LONG).show();
String text = (String) textView.getText();
textView.setText(text+s);
} public void calc(View view){ mid = (String) textView.getText();
String[] midSplit=goToSplit(mid);
Double ans=0.;
try {
List<String> after = midToAfter(midSplit);
ans = afterValue(after);
textView.setText(ans.toString());
} catch (Exception e) {
Toast.makeText(this, "输入不合法,请检查", Toast.LENGTH_LONG).show();
} }
public String[] goToSplit(String s){
int pre=-1;//上一个符号的位置,当两个符号一起时:)* 应分成:*# 否则分成:#*#
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++){
if(s.charAt(i)!='.'&&(s.charAt(i)<'0'||s.charAt(i)>'9')){
if(i-1==pre){ //上一个也是操作符号
sb.append(s.charAt(i)+"#");
}
else sb.append("#"+s.charAt(i)+"#");
pre=i;//更新pre
}else{
sb.append(s.charAt(i));
}
}
String[] split = sb.toString().split("#");
return split;
} /**
* 中缀转后缀:
* 从左到右扫描表达式
* a:若是数字直接输出
* b:若是(直接入栈
* c:若是)将栈中操作符依次退栈输出,直到遇到(为止,将(出栈丢弃
* d其它:将当前操作符的优先级小于等于栈顶操作符优先级,则将栈顶操作出栈输出。直到不小于或栈空为止;将当前操作符入栈
*/
public static List<String> midToAfter(String [] mid) throws Exception{
LinkedList<String> after=new LinkedList<String>();
int index=0;
for(String ss:mid){
if(ss.equals("=")) continue;
if(priority.get(ss)==null){//说明是操作数
after.add(ss);
}else if(ss.equals("(")){
opStack.push(ss);
}else if(ss.equals(")")){
while(!opStack.peek().equals("(")){//不是“(”,则输出,
after.add(opStack.pop());
}
opStack.pop();//去除(
}else {
while(!opStack.isEmpty()&&priority.get(ss)<=priority.get(opStack.peek())){
after.add(opStack.pop());
}
opStack.push(ss);
}
}
while(!opStack.isEmpty()) after.add(opStack.pop());
return after;
}
/**
* 后缀求值:从左到右扫描后缀表达式
* a:若为数字,直接入栈
* b:若为操作符,从栈中出栈两个数字,按操作符计算,再把结果入栈,注意两个操作数运算顺序
* 结果:最后栈中仅仅有一个数字,出栈即为答案
* @param after
* @return
*/
public static double afterValue(List<String> after) throws Exception{
LinkedList<Double> number=new LinkedList<Double>();
for(String ss:after){
if(priority.get(ss)!=null){//是操作符,取出两个数。按操作符计算后入数字栈
Double y=number.pop();
Double x=number.pop();
if(ss.equals("+")) number.push(x+y);
else if(ss.equals("-")) number.push(x-y);
else if(ss.equals("×")) number.push(x*y);
else if(ss.equals("÷")) number.push(x/y);
}else{
number.push(Double.valueOf(ss));
}
}
return number.pop();
} }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
> <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="0" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="("
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text=")"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="C"
android:onClick="clear" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="DEL"
android:onClick="delete" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="7"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="8"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="9"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="÷"
android:onClick="showText" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="4"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="5"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="6"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="×"
android:onClick="showText" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="1"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="2"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="3"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="-"
android:onClick="showText" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="."
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="0"
android:onClick="showText" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="="
android:onClick="calc" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="+"
android:onClick="showText" />
</TableRow>
</TableLayout> </LinearLayout>

效果图:

版权声明:本文博客原创文章,博客,未经同意,不得转载。

android简单的计算器的更多相关文章

  1. Android下实现一个简单的计算器源码

    下面的内容是关于Android下实现一个简单的计算器的内容. import android.app.Activity; import android.os.Bundle;import android. ...

  2. Android 简单计算器实现源码

    1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...

  3. js制作简单的计算器

    学着做了一个简单的计算器!记录记录!哈哈 <!DOCTYPE html> <html> <head> <title>简单的计算器</title&g ...

  4. 留念 C语言第一课简单的计算器制作

    留念 C语言第一课简单的计算器制作 学C语言这么久了.  /* 留念 C语言第一课简单的计算器制作 */   #include<stdio.h>  #include<stdlib.h ...

  5. jsp学习---使用jsp和JavaBean实现超简单网页计算器

    一.需求 如题,用jsp实现一个超简单的网页计算器. 二.实现 1.效果图 1)初始界面: 2)随便输入两个数进行相乘: 3)当除数为零时提示报错: 2.代码 Calculator.java pack ...

  6. JS实现一个简单的计算器

    使用JS完成一个简单的计算器功能.实现2个输入框中输入整数后,点击第三个输入框能给出2个整数的加减乘除.效果如上: 第一步: 创建构建运算函数count(). 第二步: 获取两个输入框中的值和获取选择 ...

  7. javascript 简单的计算器

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  8. 教学项目之-通过Python实现简单的计算器

    教学项目之-通过Python实现简单的计算器   计算器开发需求 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/ ...

  9. HDU1237 简单的计算器 【堆】+【逆波兰式】

    简单的计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. Ehcache详细解读(转)

    Ehcache 是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料 以简单介绍和配置方法居多,如果你有这方 ...

  2. 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)

    主题链接:http://pat.zju.edu.cn/contests/ds/3-08 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操 ...

  3. C++ Primer笔记4_静态成员类_IO库

    1.静态成员类 static成员变量与函数 static成员变量:必须在类外初始化.(const或引用类型变量必须在构造函数初始化列表里初始化) static成员函数: 不依赖于类.相当于类里的全局函 ...

  4. SQL Server 2008 R2 性能计数器详细列表(四)

    原文:SQL Server 2008 R2 性能计数器详细列表(四) SQL Server Latches 对象: 监视称为闩锁的内部 SQL Server 资源锁.通过监视闩锁来确定用户活动和资源使 ...

  5. Bye,IE!服务互联网20年IE终于要退役了

    美国当地时间16日中午,Microsoft Edge官微发表了祝词:Internet Explorer 20岁生日快乐!你在过去做出了巨大贡献,今后由我继续发扬光大.服务互联网20年之后,IE终于要退 ...

  6. Meteor全栈开发平台

    Meteor全栈开发平台 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonno ...

  7. 创意HTML5文字特效 类似翻页的效果

    原文:创意HTML5文字特效 类似翻页的效果 之前在网上看到一款比较有新意的HTML5文字特效,文字效果是当鼠标滑过是出现翻开折叠的效果,类似书本翻页.于是我兴致勃勃的点开源码看了一下,发现其实实现也 ...

  8. JS匿名函数&闭包

    <html> <head> <title> test </title> </head> <body> <script ty ...

  9. 自己动手写CPU 笔记

    自己动手写CPU 跳转至: 导航. 搜索 文件夹 1 处理器与MIPS 2 可编程逻辑器件与Verilog HDL 3 教学版OpenMIPS处理器蓝图 4 第一条指令ori 5 逻辑.移位与nop ...

  10. 使用iframe从网页调起移动端应用

    比如想在网页中调起支付宝,我们可以创建一个iframe,src为: alipayqr://platformapi/startapp?saId=10000007&clientVersion=3. ...