-
2021-06-05 13:16:54
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">
android:id="@+id/myTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/_0"
android:textSize="55sp"
android:textStyle="italic"
app:layout_constraintBottom_toTopOf="@+id/guideline6"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline5"
app:layout_constraintVertical_bias="1.0" />
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="38dp" />
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="372dp" />
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.07" />
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2" />
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="39dp"
android:text="@string/_0"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/myTextView"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline5"
app:layout_constraintVertical_bias="0.57" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toBottomOf="@+id/myTextView">
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_1"
android:textSize="30sp" />
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_2"
android:textSize="30sp" />
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_3"
android:textSize="30sp" />
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_4"
android:textSize="30sp"
app:layout_column="0"
app:layout_row="1" />
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_5"
android:textSize="30sp" />
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_6"
android:textSize="30sp" />
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_7"
android:textSize="30sp"
app:layout_column="0"
app:layout_row="2" />
android:id="@+id/button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_10"
android:textSize="30sp" />
android:id="@+id/button15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_9"
android:textSize="30sp" />
android:id="@+id/button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_0"
android:textSize="30sp"
app:layout_column="1"
app:layout_row="3" />
android:id="@+id/button17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jia"
android:textSize="30sp"
app:layout_column="3"
app:layout_row="0" />
android:id="@+id/button18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jian"
android:textSize="30sp"
app:layout_column="3"
app:layout_row="1" />
android:id="@+id/button19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheng"
android:textSize="30sp"
app:layout_column="3"
app:layout_row="2" />
android:id="@+id/button20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chu"
android:textSize="30sp"
app:layout_column="3"
app:layout_row="3" />
android:id="@+id/button21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/point"
android:textSize="30sp"
app:layout_column="2"
app:layout_row="3" />
android:id="@+id/button22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/计算"
android:textSize="30sp"
app:layout_column="0"
app:layout_row="4" />
android:id="@+id/button23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/清空"
android:textSize="30sp"
app:layout_column="0"
app:layout_row="3" />
android:id="@+id/imageButton"
android:layout_width="82dp"
android:layout_height="85dp"
app:layout_column="3"
app:layout_row="4"
app:srcCompat="@drawable/ic_baseline_backspace_24" />
更多相关内容 -
Android Studio计算器
2019-02-10 04:52:40Android Studio计算器 -
Android studio计算器.docx
2020-05-08 15:57:25运用Android studio编辑的计算器小程序源代码,public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); Uri uri=Uri.parse(... -
Android Studio实现简单计算器功能
2020-08-19 10:02:34主要为大家详细介绍了Android Studio实现简单计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
Android studio计算器
2022-04-08 18:29:00该计算器可以实现加减乘除混合运算 eg: 1+2*3=7 软件链接:https://pan.baidu.com/s/1ETzvmHE7AL1mQuzJg9vYEA 提取码:fhzn -
Android studio设计简易计算器
2020-08-26 02:15:53主要为大家详细介绍了Android studio设计简易计算器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
Android Studio实现计算器
2017-11-09 13:19:38本课题要求学生完成一款计算器,从而了解基于android平台如何开发app应用,了解常见UI控件如何使用。 具体实现要求: Android计算器实现,支持加、减、乘、除、回退、清空等。 要求如下: 1、 支持加、减、乘、除、... -
android studio完成计算器的布局并实现其简单功能
2021-07-15 16:07:07android studio完成计算器的布局并实现其简单功能,详情看我发布的文章 -
android-studio-calculator-app-code:android studio计算器应用程序源代码-Calculator source code
2021-03-24 19:55:35android-studio-calculator-app-code android studio计算器应用程序源代码 -
Android studio实现简单的计算器
2020-08-19 09:46:41主要为大家详细介绍了Android studio实现简单的计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
android studio计算器实例【实测成功】
2022-01-15 12:13:16建立android项目,可参考博主文章至第2步: 拖拉控件 1个button、2个Plain Text(两个数字输入)、3个TextView(+, =, 结果) button: (其中的onClick中的函数calculate,是后面在MainActivity.java里写的...仅作为记录,大佬请跳过。
新建项目
建立android项目,可参考博主文章至第2步:
拖拉控件
1个
button
、2个Plain Text
(两个数字输入)、3个TextView
(+, =, 结果)
button:
(其中的onClick中的函数calculate,是后面在MainActivity.java
里写的button点击函数)
Plain Text:(在程序中是Editbox:
)
TextView:
魔法棒控件
MainActivity.java写程序
所有程序
package com.example.a3calculator; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void calculate(View v){ EditText number1=(EditText) findViewById(R.id.num1); EditText number2=(EditText) findViewById(R.id.num2); TextView result_total=(TextView) findViewById(R.id.result); // result_total.setText(number1.getText().toString()); // double a=Double.valueOf(number1.getText().toString()); // double b=Double.valueOf(number2.getText().toString()); // double c=a+b; int a=Integer.valueOf(number1.getText().toString()); int b=Integer.valueOf(number2.getText().toString()); int c=a+b; result_total.setText(String.valueOf(c)); } }
运行
展示
*******************************
关于android.getText()的数据类型转化
参考:传送门
-
Android Studio简易计算器.rar
2019-11-20 16:37:28使用java语言,在Android studio平台上进行开发,实现加减乘除的运算功能和清屏的功能,首先设计一个可视化的界面,供用户输入数据并查看结果。用户通过点击相应按钮输入正确的表达式,计算器进行相应的加减乘除运算... -
android studio实现简单的计算器功能
2021-01-03 11:44:29本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下 先来个效果图: 功能: 满足加减乘除四则运算规则,有回退、清除功能。 下面的代码只是完成基本功能,若添加背景图先看看下面... -
Android studio实现简单计算器
2021-01-03 12:41:06本文实例为大家分享了Android studio实现简单计算器的具体代码,供大家参考,具体内容如下 需求分析 在Android studio中设计并实现一个简单的计算器,实现连续的加减乘除运算。 界面设计 采用网格GridLayout布局,... -
Android studio 简易计算器实现
2020-04-11 20:23:251.允许用户输入两个数字,进行计算2.用户可以选择计算类型3.输入并确定计算类型,点击计算按钮进行计算4.将结果展示给用户 5.支持的计算类型:加、减、乘、除 -
利用Android Studio实现的简单房贷计算器
2021-07-15 12:02:05利用Android Studio 4.2.1实现的简单房贷计算器。 -
Android studio 做的计算器
2017-10-28 17:35:45简易计算器,大小固定,但是你自己可以改,界面少女心 -
Android studio计算器.zip
2021-12-14 20:30:43Android studio计算器.zip -
Android Studio计算器解决三角函数,反三角函数等
2021-02-01 20:16:46需要设计一款通用计算器,包含基本的四则运算,还引入了括号,还要解决一些特殊的三角函数,反三角函数等,浏览了CSDN发现这方面的博客比较少,多为一些只能解决四则运算的简易计算器,功能比较单一。最后在一个引入...
前言
需要设计一款通用计算器,包含基本的四则运算,还引入了括号,还要解决一些特殊的三角函数,反三角函数等,浏览了CSDN发现这方面的博客比较少,多为一些只能解决四则运算的简易计算器,功能比较单一。最后在一个引入了括号,只能解决四则运算的计算器的基础上,进行功能的扩充,经过四五天时间,最终写出。即下图这些功能。
一、XML布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 结果显示框 --> <EditText android:id="@+id/input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cursorVisible="false" android:gravity="right" android:editable = "false" android:text="0" /> <!-- 接下去采TableRow的格式进行布局设计 --> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 用于显示存储结果 --> <TextView android:id="@+id/M" android:layout_width="53sp" android:layout_height="wrap_content" android:text=" MEM :" /> <!-- 默认显示字符串0 --> <TextView android:id="@+id/mem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="0" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示当前是角度还是弧度,默认是角度 --> <TextView android:id="@+id/_drg" android:layout_width="53sp" android:layout_height="wrap_content" android:text=" DEG" /> <!-- 清除存储结果 --> <Button android:id="@+id/mc" android:text="MC" android:layout_width="106sp" android:layout_height="wrap_content" /> <!-- 清除输出窗口的所有内容 --> <Button android:id="@+id/c" android:text="C" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 在角度和弧度之间切换 --> <Button android:id="@+id/drg" android:text="DRG" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- 正弦计算 --> <Button android:id="@+id/sin" android:text="sin" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- 余弦计算 --> <Button android:id="@+id/cos" android:text="cos" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- 正切计算 --> <Button android:id="@+id/tan" android:text="tan" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- 阶乘计算 --> <Button android:id="@+id/factorial" android:text="n!" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- 退格键 --> <Button android:id="@+id/bksp" android:text="Bksp" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 数字7 --> <Button android:id="@+id/seven" android:text="7" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 数字8 --> <Button android:id="@+id/eight" android:text="8" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 数字9 --> <Button android:id="@+id/nine" android:text="9" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 除号 --> <Button android:id="@+id/divide" android:text="÷" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 左括号 --> <Button android:id="@+id/left" android:text="(" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 右括号 --> <Button android:id="@+id/right" android:text=")" android:layout_width="53sp" android:layout_height="wrap_content" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 数字4 --> <Button android:id="@+id/four" android:text="4" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 数字5 --> <Button android:id="@+id/five" android:text="5" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 数字6 --> <Button android:id="@+id/six" android:text="6" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 乘号 --> <Button android:id="@+id/mul" android:text="×" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 开方 --> <Button android:id="@+id/sqrt" android:text="√" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 乘方 --> <Button android:id="@+id/square" android:text="^" android:layout_width="53sp" android:layout_height="wrap_content" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 数字1 --> <Button android:id="@+id/one" android:text="1" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 数字2 --> <Button android:id="@+id/two" android:text="2" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 数字3 --> <Button android:id="@+id/three" android:text="3" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 减号 --> <Button android:id="@+id/sub" android:text="-" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 对数 --> <Button android:id="@+id/log" android:text="log" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 自然对数 --> <Button android:id="@+id/ln" android:text="ln" android:layout_width="53sp" android:layout_height="wrap_content" /> </TableRow> <TableLayout android:layout_width="fill_parent" android:layout_height="57sp" > <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 数字0 --> <Button android:id="@+id/zero" android:text="0" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 小数点 --> <Button android:id="@+id/dot" android:text="." android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 等号 --> <Button android:id="@+id/equal" android:text="=" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 加号 --> <Button android:id="@+id/add" android:text="+" android:layout_width="53sp" android:layout_height="wrap_content" /> <!-- 退出计算器 --> <Button android:id="@+id/exit" android:text="exit" android:layout_width="106sp" android:layout_height="wrap_content" /> </TableRow> </TableLayout> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 用于提示,告诉用户如何使用计算器的一些功能等 --> <TextView android:id="@+id/T" android:layout_width="45sp" android:layout_height="wrap_content" android:text="提示:" /> <TextView android:id="@+id/tip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="欢迎使用!" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- asin --> <Button android:id="@+id/asin" android:text="asin" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- acos --> <Button android:id="@+id/acos" android:text="acos" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- atan --> <Button android:id="@+id/atan" android:text="atan" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- sinh --> <Button android:id="@+id/sinh" android:text="sinh" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- cosh --> <Button android:id="@+id/cosh" android:text="cosh" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- tanh --> <Button android:id="@+id/tanh" android:text="tanh" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- asinh --> <Button android:id="@+id/asinh" android:text="asinh" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- acosh --> <Button android:id="@+id/acosh" android:text="acosh" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> <!-- atanh --> <Button android:id="@+id/atanh" android:text="atanh" android:layout_width="0sp" android:layout_weight="1" android:layout_height="wrap_content" /> </TableRow> </LinearLayout>
1.
两个TextView一个属于计算器的输入框,显示用户通过按钮输入的表达式。另一个用来提醒用户采用正确的输入。
2.有"0123456789()sincostanasinacosatansinhcoshtanhasinhacoshatanhlnlogn!±×÷√^"这些按钮
3.后台利用Buttoon 的android:text这个属性获取运算符或者数字,然后进行运算。二、MainActivity
package com.example.calculator; import java.text.NumberFormat; import java.text.DecimalFormat; import android.app.Activity; import android.os.Bundle; import android.widget.*; import android.util.Log; import android.view.View.*; import android.view.*; import java.util.*; public class MainActivity extends Activity { //0~9十个按键 private Button[] btn = new Button[10]; //显示器,用于显示输出结果 private EditText input; //显示器下方的记忆器,用于记录上一次计算结果 private TextView mem; //三角计算时标志显示:角度还是弧度 private TextView _drg; //小提示,用于加强人机交互的弱检测、提示 private TextView tip; private Button div, mul, sub, add, equal, // ÷ × - + = sin, cos, tan, log, ln, //函数 sqrt, square, factorial, bksp, //根号 平方 阶乘 退格 left, right, dot, exit, drg, //( ) . 退出 角度弧度控制键 mc, c, // mem清屏键 input清屏键 asin, acos, atan, // arcsin,arccos,arctan sinh, cosh, tanh, //sinh, cosh, tanh asinh, acosh, atanh; //asinh,acosh,atanh //保存原来的算式样子,为了输出时好看,因计算时,算式样子被改变 public String str_old; //变换样子后的式子 public String str_new; //输入控制,true为重新输入,false为接着输入 public boolean vbegin = true; //控制DRG按键,true为角度,false为弧度 public boolean drg_flag = true; //π值:3.14 public double pi=4*Math.atan(1); //true表示正确,可以继续输入;false表示有误,输入被锁定 public boolean tip_lock = true; //判断是否是按=之后的输入,true表示输入在=之前,false反之 public boolean equals_flag = true; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.activity_main); //获取界面元素 input = (EditText)findViewById(R.id.input); mem = (TextView)findViewById(R.id.mem); tip = (TextView)findViewById(R.id.tip); _drg = (TextView)findViewById(R.id._drg); btn[0] = (Button)findViewById(R.id.zero); btn[1] = (Button)findViewById(R.id.one); btn[2] = (Button)findViewById(R.id.two); btn[3] = (Button)findViewById(R.id.three); btn[4] = (Button)findViewById(R.id.four); btn[5] = (Button)findViewById(R.id.five); btn[6] = (Button)findViewById(R.id.six); btn[7] = (Button)findViewById(R.id.seven); btn[8] = (Button)findViewById(R.id.eight); btn[9] = (Button)findViewById(R.id.nine); div = (Button)findViewById(R.id.divide); mul = (Button)findViewById(R.id.mul); sub = (Button)findViewById(R.id.sub); add = (Button)findViewById(R.id.add); equal = (Button)findViewById(R.id.equal); sin = (Button)findViewById(R.id.sin); cos = (Button)findViewById(R.id.cos); tan = (Button)findViewById(R.id.tan); log = (Button)findViewById(R.id.log); ln = (Button)findViewById(R.id.ln); sqrt = (Button)findViewById(R.id.sqrt); square = (Button)findViewById(R.id.square); factorial = (Button)findViewById(R.id.factorial); bksp = (Button)findViewById(R.id.bksp); left = (Button)findViewById(R.id.left); right = (Button)findViewById(R.id.right); dot = (Button)findViewById(R.id.dot); exit = (Button)findViewById(R.id.exit); drg = (Button)findViewById(R.id.drg); mc = (Button)findViewById(R.id.mc); c = (Button)findViewById(R.id.c); asin = (Button)findViewById(R.id.asin); acos = (Button)findViewById(R.id.acos); atan = (Button)findViewById(R.id.atan); sinh = (Button)findViewById(R.id.sinh); cosh = (Button)findViewById(R.id.cosh); tanh = (Button)findViewById(R.id.tanh); asinh = (Button)findViewById(R.id.asinh); acosh = (Button)findViewById(R.id.acosh); atanh = (Button)findViewById(R.id.atanh); //为数字按键绑定监听器 for(int i = 0; i < 10; ++i) { btn[i].setOnClickListener(actionPerformed); } //为+-x÷等按键绑定监听器 div.setOnClickListener(actionPerformed); mul.setOnClickListener(actionPerformed); sub.setOnClickListener(actionPerformed); add.setOnClickListener(actionPerformed); equal.setOnClickListener(actionPerformed); sin.setOnClickListener(actionPerformed); cos.setOnClickListener(actionPerformed); tan.setOnClickListener(actionPerformed); //新增的 asin,acos,atan,sinh,cosh,tanh,asinh,acosh,atanh asin.setOnClickListener(actionPerformed); acos.setOnClickListener(actionPerformed); atan.setOnClickListener(actionPerformed); sinh.setOnClickListener(actionPerformed); cosh.setOnClickListener(actionPerformed); tanh.setOnClickListener(actionPerformed); asinh.setOnClickListener(actionPerformed); acosh.setOnClickListener(actionPerformed); atanh.setOnClickListener(actionPerformed); log.setOnClickListener(actionPerformed); ln.setOnClickListener(actionPerformed); sqrt.setOnClickListener(actionPerformed); square.setOnClickListener(actionPerformed); factorial.setOnClickListener(actionPerformed); bksp.setOnClickListener(actionPerformed); left.setOnClickListener(actionPerformed); right.setOnClickListener(actionPerformed); dot.setOnClickListener(actionPerformed); exit.setOnClickListener(actionPerformed); drg.setOnClickListener(actionPerformed); mc.setOnClickListener(actionPerformed); c.setOnClickListener(actionPerformed); } /* * 键盘命令捕捉 */ //命令缓存,用于检测输入合法性 String[] Tipcommand = new String[500]; //Tipcommand的指针 int tip_i = 0; private OnClickListener actionPerformed = new OnClickListener() { public void onClick(View v) { //按键上的命令获取 String command = ((Button)v).getText().toString(); //显示器上的字符串 String str = input.getText().toString(); //检测输入是否合法 if(equals_flag == false && "0123456789.()sincostanasinacosatansinhcoshtanhasinhacoshatanhlnlogn!+-×÷√^".indexOf(command) != -1) { //检测显示器上的字符串是否合法 if(right(str)) { if("+-×÷√^)".indexOf(command) != -1) { for(int i =0 ; i < str.length(); i++) { Tipcommand[tip_i] = String.valueOf(str.charAt(i)); tip_i++; } vbegin = false; } } else { input.setText("0"); vbegin = true; tip_i = 0; tip_lock = true; tip.setText("欢迎使用!"); } equals_flag = true; } if(tip_i > 0) TipChecker(Tipcommand[tip_i-1] , command); else if(tip_i == 0) { TipChecker("#" , command); } if("0123456789.()sincostanasinacosatansinhcoshtanhasinhacoshatanhlnlogn!+-×÷√^".indexOf(command) != -1 && tip_lock) { Tipcommand[tip_i] = command; tip_i++; } //若输入正确,则将输入信息显示到显示器上 if("0123456789.()sincostanasinacosatansinhcoshtanhasinhacoshatanhlnlogn!+-×÷√^".indexOf(command) != -1 && tip_lock) { //共25个按键 print(command); //若果点击了“DRG”,则切换当前弧度角度制,并将切换后的结果显示到按键上方。 } else if(command.compareTo("DRG") == 0 && tip_lock) { if(drg_flag == true) { drg_flag = false; _drg.setText(" RAD"); } else { drg_flag = true; _drg.setText(" DEG"); } //如果输入时退格键,并且是在按=之前 } else if(command.compareTo("Bksp") == 0 && equals_flag) { //一次删除5个字符 if(TTO(str) == 5){ if(str.length()>5) input.setText(str.substring(0,str.length() - 5)); else if(str.length() == 5){ input.setText("0"); vbegin = true; tip_i = 0; tip.setText("欢迎使用!"); } } //一次删除4个字符 else if(TTO(str) == 4){ if(str.length()>4) input.setText(str.substring(0,str.length() - 4)); else if(str.length() == 4){ input.setText("0"); vbegin = true; tip_i = 0; tip.setText("欢迎使用!"); } } //一次删除3个字符 else if(TTO(str) == 3) { if(str.length() > 3) input.setText(str.substring(0, str.length() - 3)); else if(str.length() == 3) { input.setText("0"); vbegin = true; tip_i = 0; tip.setText("欢迎使用!"); } //依次删除2个字符 } else if(TTO(str) == 2) { if(str.length() > 2) input.setText(str.substring(0, str.length() - 2)); else if(str.length() == 2) { input.setText("0"); vbegin = true; tip_i = 0; tip.setText("欢迎使用!"); } //依次删除一个字符 } else if(TTO(str) == 1) { //若之前输入的字符串合法则删除一个字符 if(right(str)) { if(str.length() > 1) input.setText(str.substring(0, str.length() - 1)); else if(str.length() == 1) { input.setText("0"); vbegin = true; tip_i = 0; tip.setText("欢迎使用!"); } //若之前输入的字符串不合法则删除全部字符 } else { input.setText("0"); vbegin = true; tip_i = 0; tip.setText("欢迎使用!"); } } if(input.getText().toString().compareTo("-") == 0 || !equals_flag) { input.setText("0"); vbegin = true; tip_i = 0; tip.setText("欢迎使用!"); } tip_lock = true; if(tip_i > 0) tip_i--; //如果是在按=之后输入退格键 } else if(command.compareTo("Bksp") == 0 && !equals_flag) { //将显示器内容设置为0 input.setText("0"); vbegin = true; tip_i = 0; tip_lock = true; tip.setText("欢迎使用!"); //如果输入的是清除键 } else if(command.compareTo("C") == 0) { //将显示器内容设置为0 input.setText("0"); //重新输入标志置为true vbegin = true; //缓存命令位数清0 tip_i = 0; //表明可以继续输入 tip_lock = true; //表明输入=之前 equals_flag = true; tip.setText("欢迎使用!"); //如果输入的是”MC“,则将存储器内容清0 } else if(command.compareTo("MC") == 0) { mem.setText("0"); //如果按”exit“则退出程序 } else if(command.compareTo("exit") == 0) { System.exit(0); //如果输入的是=号,并且输入合法 } else if(command.compareTo("=") == 0 && tip_lock && right(str) && equals_flag) { tip_i = 0; //表明不可以继续输入 tip_lock = false; //表明输入=之后 equals_flag = false; //保存原来算式样子 str_old = str; /* 新增asinh,acosh,atanh 使用 o,p,q */ str = str.replaceAll("asinh", "o"); str = str.replaceAll("acosh", "p"); str = str.replaceAll("atanh", "q"); /* 新增 asin,acos,atan,sinh,cosh,tanh,"sctgl已用" */ str = str.replaceAll("asin", "h"); str = str.replaceAll("acos", "i"); str = str.replaceAll("atan" , "j"); str = str.replaceAll("sinh", "k"); str = str.replaceAll("cosh", "m"); str = str.replaceAll("tanh", "n"); //替换算式中的运算符,便于计算 str = str.replaceAll("sin", "s"); str = str.replaceAll("cos", "c"); str = str.replaceAll("tan", "t"); str = str.replaceAll("log", "g"); str = str.replaceAll("ln", "l"); str = str.replaceAll("n!", "!"); //重新输入标志设置true vbegin = true; //将-1x转换成- str_new = str.replaceAll("-", "-1×"); //计算算式结果 new calc().process(str_new); } //表明可以继续输入 tip_lock = true; } }; //向input输出字符 private void print(String str) { //清屏后输出 if(vbegin) input.setText(str); //在屏幕原str后增添字符 else input.append(str); vbegin = false; } /* * 判断一个str是否是合法的,返回值为true、false * 只包含"0123456789.()sincostanasinacosatansinhcoshtanhasinhacoshatanhlnlogn!+-×÷√^"的是合法的str,返回true * 包含了除"0123456789.()sincostanasinacosatansinhcoshtanhasinhacoshatanhlnlogn!+-×÷√^"以外的字符的str为非法的,返回false */ private boolean right(String str) { int i; for(i = 0;i < str.length();i++) { if(str.charAt(i)!='0' && str.charAt(i)!='1' && str.charAt(i)!='2' && str.charAt(i)!='3' && str.charAt(i)!='4' && str.charAt(i)!='5' && str.charAt(i)!='6' && str.charAt(i)!='7' && str.charAt(i)!='8' && str.charAt(i)!='9' && str.charAt(i)!='.' && str.charAt(i)!='-' && str.charAt(i)!='+' && str.charAt(i)!='×' && str.charAt(i)!='÷' && str.charAt(i)!='√' && str.charAt(i)!='^' && str.charAt(i)!='s' && str.charAt(i)!='i' && str.charAt(i)!='n' && str.charAt(i)!='c' && str.charAt(i)!='o' && str.charAt(i)!='t' && str.charAt(i)!='a' && str.charAt(i)!='l' && str.charAt(i)!='g' && str.charAt(i)!='(' && str.charAt(i)!=')' && str.charAt(i)!='!' && str.charAt(i)!='h') // 后面加的h break; } return i == str.length(); } /* * 检测函数,返回值为5,4,3、2、1 表示应当一次删除几个? * 为Bksp按钮的删除方式提供依据 * 返回5,表示str尾部为asinh,acosh,atanh中的一个,应当一次删除5个 * 返回4,表示str尾部为asin,acos,atan,sinh,cosh,tanh中的一个,应当一次删除4个 * 返回3,表示str尾部为sin、cos、tan、log,中的一个,应当一次删除3个 * 返回2,表示str尾部为ln、n!中的一个,应当一次删除2个 * 返回1,表示为除返回3、2外的所有情况,只需删除一个(包含非法字符时要另外考虑:应清屏) */ private int TTO(String str) { if((str.charAt(str.length() - 1) == 'h' && str.charAt(str.length() - 2) == 'n' && str.charAt(str.length() - 3) == 'i' && str.charAt(str.length() - 4) == 's' && str.charAt(str.length() - 5) == 'a')//asinh || (str.charAt(str.length() - 1) == 'h' && str.charAt(str.length() - 2) == 's' && str.charAt(str.length() - 3) == 'o' && str.charAt(str.length() - 4) == 'c' && str.charAt(str.length() - 5) == 'a')//acosh || (str.charAt(str.length() - 1) == 'h' && str.charAt(str.length() - 2) == 'n' && str.charAt(str.length() - 3) == 'a' && str.charAt(str.length() - 4) == 't' && str.charAt(str.length() - 5) == 'a')) //atanh {return 5;} else if((str.charAt(str.length() - 1) == 'n' && str.charAt(str.length() - 2) == 'i' && str.charAt(str.length() - 3) == 's' && str.charAt(str.length() - 4) == 'a')//asin || (str.charAt(str.length() - 1) == 's' && str.charAt(str.length() - 2) == 'o' && str.charAt(str.length() - 3) == 'c' && str.charAt(str.length() - 4) == 'a')//acos || (str.charAt(str.length() - 1) == 'n' && str.charAt(str.length() - 2) == 'a' && str.charAt(str.length() - 3) == 't' && str.charAt(str.length() - 4) == 'a')//atan || (str.charAt(str.length() - 1) == 'h' && str.charAt(str.length() - 2) == 'n' && str.charAt(str.length() - 3) == 'i' && str.charAt(str.length() - 4) == 's')//sinh || (str.charAt(str.length() - 1) == 'h' && str.charAt(str.length() - 2) == 's' && str.charAt(str.length() - 3) == 'o' && str.charAt(str.length() - 4) == 'c')//cosh || (str.charAt(str.length() - 1) == 'h' && str.charAt(str.length() - 2) == 'n' && str.charAt(str.length() - 3) == 'a' && str.charAt(str.length() - 4) == 't')) //tanh {return 4;} else if((str.charAt(str.length() - 1) == 'n' && str.charAt(str.length() - 2) == 'i' && str.charAt(str.length() - 3) == 's') //sin || (str.charAt(str.length() - 1) == 's' && str.charAt(str.length() - 2) == 'o' && str.charAt(str.length() - 3) == 'c') || (str.charAt(str.length() - 1) == 'n' && str.charAt(str.length() - 2) == 'a' && str.charAt(str.length() - 3) == 't') || (str.charAt(str.length() - 1) == 'g' && str.charAt(str.length() - 2) == 'o' && str.charAt(str.length() - 3) == 'l')) { return 3;} else if((str.charAt(str.length() - 1) == 'n' && str.charAt(str.length() - 2) == 'l') || (str.charAt(str.length() - 1) == '!' && str.charAt(str.length() - 2) == 'n')) {return 2; } else { return 1; } } /* * 检测函数,对str进行前后语法检测 * 为Tip的提示方式提供依据,与TipShow()配合使用 * 编号 字符 其后可以跟随的合法字符 * 1 ( 数字|(|-|.|函数 * 2 ) 算符|)|√ ^ * 3 . 数字|算符|)|√ ^ * 4 数字 .|数字|算符|)|√ ^ * 5 算符 数字|(|.|函数 * 6 √ ^ ( |. | 数字 * 7 函数 数字|(|. * * 小数点前后均可省略,表示0 * 数字第一位可以为0 */ private void TipChecker(String tipcommand1,String tipcommand2) { //Tipcode1表示错误类型,Tipcode2表示名词解释类型 int Tipcode1 = 0 , Tipcode2 = 0; //表示命令类型 int tiptype1 = 0 , tiptype2 = 0; //括号数 int bracket = 0; //“+-x÷√^”不能作为第一位 if(tipcommand1.compareTo("#") == 0 && (tipcommand2.compareTo("÷") == 0 || tipcommand2.compareTo("×") == 0 || tipcommand2.compareTo("+") == 0 || tipcommand2.compareTo(")") == 0 || tipcommand2.compareTo("√") == 0 || tipcommand2.compareTo("^") == 0)) { Tipcode1 = -1; } //定义存储字符串中最后一位的类型 else if(tipcommand1.compareTo("#") != 0) { if(tipcommand1.compareTo("(") == 0) { tiptype1 = 1; } else if(tipcommand1.compareTo(")") == 0) { tiptype1 = 2; } else if(tipcommand1.compareTo(".") == 0) { tiptype1 = 3; } else if("0123456789".contains(tipcommand1)) { tiptype1 = 4; } else if("+-×÷".contains(tipcommand1)) { tiptype1 = 5; } else if("√^".contains(tipcommand1)) { tiptype1 = 6; } else if("sincostanasinacosatansinhcoshtanhasinhacoshatanhlnlogn!".contains(tipcommand1)) { tiptype1 = 7; } //定义欲输入的按键类型 if(tipcommand2.compareTo("(") == 0) { tiptype2 = 1; } else if(tipcommand2.compareTo(")") == 0) { tiptype2 = 2; } else if(tipcommand2.compareTo(".") == 0) { tiptype2 = 3; } else if("0123456789".contains(tipcommand2)) { tiptype2 = 4; } else if("+-×÷".contains(tipcommand2)) { tiptype2 = 5; } else if("√^".contains(tipcommand2)) { tiptype2 = 6; } else if("sincostanasinacosatansinhcoshtanhasinhacoshatanhlnlogn!".contains(tipcommand2)) { tiptype2 = 7; } switch(tiptype1) { case 1: //左括号后面直接接右括号,“+x÷”(负号“-”不算),或者"√^" if(tiptype2 == 2 || (tiptype2 == 5 && tipcommand2.compareTo("-") != 0) || tiptype2 == 6) Tipcode1 = 1; break; case 2: //右括号后面接左括号,数字,“+-x÷sin^...” if(tiptype2 == 1 || tiptype2 == 3 || tiptype2 == 4 || tiptype2 == 7) Tipcode1 = 2; break; case 3: //“.”后面接左括号或者“sincos...” if(tiptype2 == 1 || tiptype2 == 7) Tipcode1 = 3; //连续输入两个“.” if(tiptype2 == 3) Tipcode1 = 8; break; case 4: //数字后面直接接左括号或者“sincos...” if(tiptype2 == 1 || tiptype2 == 7) Tipcode1 = 4; break; case 5: //“+-x÷”后面直接接右括号,“+-x÷√^” if(tiptype2 == 2 || tiptype2 == 5 || tiptype2 == 6) Tipcode1 = 5; break; case 6: //“√^”后面直接接右括号,“+-x÷√^”以及“sincos...” if(tiptype2 == 2 || tiptype2 == 5 || tiptype2 == 6 || tiptype2 == 7) Tipcode1 = 6; break; case 7: //“sincos...”后面直接接右括号“+-x÷√^”以及“sincos...” if(tiptype2 == 2 || tiptype2 == 5 || tiptype2 == 6 || tiptype2 == 7) Tipcode1 = 7; break; } } //检测小数点的重复性,Tipconde1=0,表明满足前面的规则 if(Tipcode1 == 0 && tipcommand2.compareTo(".") == 0) { int tip_point = 0; for(int i = 0;i < tip_i;i++) { //若之前出现一个小数点点,则小数点计数加1 if(Tipcommand[i].compareTo(".") == 0) { tip_point++; } //若出现以下几个运算符之一,小数点计数清零 if(Tipcommand[i].compareTo("sin") == 0 || Tipcommand[i].compareTo("cos") == 0 || Tipcommand[i].compareTo("asin") == 0 || Tipcommand[i].compareTo("acos") == 0 || Tipcommand[i].compareTo("atan") == 0 || Tipcommand[i].compareTo("sinh") == 0 || Tipcommand[i].compareTo("cosh") == 0 || Tipcommand[i].compareTo("tanh") == 0 || Tipcommand[i].compareTo("asinh") == 0 || Tipcommand[i].compareTo("acosh") == 0 || Tipcommand[i].compareTo("atanh") == 0 || Tipcommand[i].compareTo("tan") == 0 || Tipcommand[i].compareTo("log") == 0 || Tipcommand[i].compareTo("ln") == 0 || Tipcommand[i].compareTo("n!") == 0 || Tipcommand[i].compareTo("√") == 0 || Tipcommand[i].compareTo("^") == 0 || Tipcommand[i].compareTo("÷") == 0 || Tipcommand[i].compareTo("×") == 0 || Tipcommand[i].compareTo("-") == 0 || Tipcommand[i].compareTo("+") == 0 || Tipcommand[i].compareTo("(") == 0 || Tipcommand[i].compareTo(")") == 0 ) { tip_point = 0; } } tip_point++; //若小数点计数大于1,表明小数点重复了 if(tip_point > 1) { Tipcode1 = 8; } } //检测右括号是否匹配 if(Tipcode1 == 0 && tipcommand2.compareTo(")") == 0) { int tip_right_bracket = 0; for(int i = 0;i < tip_i;i++) { //如果出现一个左括号,则计数加1 if(Tipcommand[i].compareTo("(") == 0) { tip_right_bracket++; } //如果出现一个右括号,则计数减1 if(Tipcommand[i].compareTo(")") == 0) { tip_right_bracket--; } } //如果右括号计数=0,表明没有响应的左括号与当前右括号匹配 if(tip_right_bracket == 0) { Tipcode1 = 10; } } //检查输入=的合法性 if(Tipcode1 == 0 && tipcommand2.compareTo("=") == 0) { //括号匹配数 int tip_bracket = 0; for(int i = 0;i < tip_i;i++) { if(Tipcommand[i].compareTo("(") == 0) { tip_bracket++; } if(Tipcommand[i].compareTo(")") == 0) { tip_bracket--; } } //若大于0,表明左括号还有未匹配的 if(tip_bracket > 0) { Tipcode1 = 9; bracket = tip_bracket; } else if(tip_bracket == 0) { //若前一个字符是以下之一,表明=号不合法 if("sincostanasinacosatansinhcoshtanhasinhacoshatanhlnlogn!".contains(tipcommand1)) { Tipcode1 = 6; } //若前一个字符是以下之一,表明=号不合法 if("+-×÷".contains(tipcommand1)) { Tipcode1 = 5; } } } //若命令式以下之一,则显示相应的帮助信息 if(tipcommand2.compareTo("MC") == 0) Tipcode2 = 1; if(tipcommand2.compareTo("C") == 0) Tipcode2 = 2; if(tipcommand2.compareTo("DRG") == 0) Tipcode2 = 3; if(tipcommand2.compareTo("Bksp") == 0) Tipcode2 = 4; if(tipcommand2.compareTo("sin") == 0) Tipcode2 = 5; if(tipcommand2.compareTo("cos") == 0) Tipcode2 = 6; if(tipcommand2.compareTo("tan") == 0) Tipcode2 = 7; if(tipcommand2.compareTo("log") ==0) Tipcode2 = 8; if(tipcommand2.compareTo("ln") == 0) Tipcode2 = 9; if(tipcommand2.compareTo("n!") == 0) Tipcode2 = 10; if(tipcommand2.compareTo("√") == 0) Tipcode2 = 11; if(tipcommand2.compareTo("^") == 0) Tipcode2 = 12; //显示帮助和错误信息 TipShow(bracket , Tipcode1 , Tipcode2 , tipcommand1 , tipcommand2); } /* * 反馈Tip信息,加强人机交互,与TipChecker()配合使用 */ private void TipShow(int bracket , int tipcode1 , int tipcode2 , String tipcommand1 , String tipcommand2) { String tipmessage = ""; if(tipcode1 != 0) tip_lock = false;//表明输入有误 switch(tipcode1) { case -1: tipmessage = tipcommand2 + " 不能作为第一个算符\n"; break; case 1: tipmessage = tipcommand1 + " 后应输入:数字/(/./-/函数 \n"; break; case 2: tipmessage = tipcommand1 + " 后应输入:)/算符 \n"; break; case 3: tipmessage = tipcommand1 + " 后应输入:)/数字/算符 \n"; break; case 4: tipmessage = tipcommand1 + " 后应输入:)/./数字 /算符 \n"; break; case 5: tipmessage = tipcommand1 + " 后应输入:(/./数字/函数 \n"; break; case 6: tipmessage = tipcommand1 + " 后应输入:(/./数字 \n"; break; case 7: tipmessage = tipcommand1 + " 后应输入:(/./数字 \n"; break; case 8: tipmessage = "小数点重复\n"; break; case 9: tipmessage = "不能计算,缺少 "+ bracket +" 个 )"; break; case 10: tipmessage = "不需要 )"; break; } switch(tipcode2) { case 1: tipmessage = tipmessage + "[MC 用法: 清除记忆 MEM]"; break; case 2: tipmessage = tipmessage + "[C 用法: 归零]"; break; case 3: tipmessage = tipmessage + "[DRG 用法: 选择 DEG 或 RAD]"; break; case 4: tipmessage = tipmessage + "[Bksp 用法: 退格]"; break; case 5: tipmessage = tipmessage + "sin 函数用法示例:\n" + "DEG:sin30 = 0.5 RAD:sin1 = 0.84\n" + "注:与其他函数一起使用时要加括号,如:\n" + "sin(cos45),而不是sincos45" ; break; case 6: tipmessage = tipmessage + "cos 函数用法示例:\n" + "DEG:cos60 = 0.5 RAD:cos1 = 0.54\n" + "注:与其他函数一起使用时要加括号,如:\n" + "cos(sin45),而不是cossin45" ; break; case 7: tipmessage = tipmessage + "tan 函数用法示例:\n" + "DEG:tan45 = 1 RAD:tan1 = 1.55\n" + "注:与其他函数一起使用时要加括号,如:\n" + "tan(cos45),而不是tancos45" ; break; case 8: tipmessage = tipmessage + "log 函数用法示例:\n" + "log10 = log(5+5) = 1\n" + "注:与其他函数一起使用时要加括号,如:\n" + "log(tan45),而不是logtan45" ; break; case 9: tipmessage = tipmessage + "ln 函数用法示例:\n" + "ln10 = le(5+5) = 2.3 lne = 1\n" + "注:与其他函数一起使用时要加括号,如:\n" + "ln(tan45),而不是lntan45" ; break; case 10: tipmessage = tipmessage + "n! 函数用法示例:\n" + "n!3 = n!(1+2) = 3×2×1 = 6\n" + "注:与其他函数一起使用时要加括号,如:\n" + "n!(log1000),而不是n!log1000" ; break; case 11: tipmessage = tipmessage + "√ 用法示例:开任意次根号\n" + "如:27开3次根为 27√3 = 3\n" + "注:与其他函数一起使用时要加括号,如:\n" + "(函数)√(函数) , (n!3)√(log100) = 2.45"; break; case 12: tipmessage = tipmessage + "^ 用法示例:开任意次平方\n" + "如:2的3次方为 2^3 = 8\n" + "注:与其他函数一起使用时要加括号,如:\n" + "(函数)√(函数) , (n!3)^(log100) = 36"; break; } //将提示信息显示到tip tip.setText(tipmessage); } /* * 整个计算核心,只要将表达式的整个字符串传入calc().process()就可以实行计算了 * 算法包括以下几部分: * 1、计算部分 process(String str) 当然,这是建立在查错无错误的情况下 * 2、数据格式化 FP(double n) 使数据有相当的精确度 * 3、阶乘算法 N(double n) 计算n!,将结果返回 * 4、错误提示 showError(int code ,String str) 将错误返回 */ public class calc { public calc(){ } final int MAXLEN = 500; /* * 计算表达式 * 从左向右扫描,数字入number栈,运算符入operator栈 * +-基本优先级为1,×÷基本优先级为2,log ln sin cos tan n!基本优先级为3,√^基本优先级为4 * 括号内层运算符比外层同级运算符优先级高4 * 当前运算符优先级高于栈顶压栈,低于栈顶弹出一个运算符与两个数进行运算 * 重复直到当前运算符大于栈顶 * 扫描完后对剩下的运算符与数字依次计算 */ public void process(String str) { int weightPlus = 0, topOp = 0, topNum = 0, flag = 1, weightTemp = 0; //weightPlus为同一()下的基本优先级,weightTemp临时记录优先级的变化 //topOp为weight[],operator[]的计数器;topNum为number[]的计数器 //flag为正负数的计数器,1为正数,-1为负数 int weight[]; //保存operator栈中运算符的优先级,以topOp计数 double number[]; //保存数字,以topNum计数 char ch, ch_gai, operator[];//operator[]保存运算符,以topOp计数 String num;//记录数字,str以+-×÷()sctglhijkmn!√^分段,+-×÷()sctglhijkmn!√^字符之间的字符串即为数字 weight = new int[MAXLEN]; number = new double[MAXLEN]; operator = new char[MAXLEN]; String expression = str; StringTokenizer expToken = new StringTokenizer(expression,"+-×÷()sctghijkmnlopq!√^"); int i = 0; while (i < expression.length()) { ch = expression.charAt(i); //判断正负数 if (i == 0) { if (ch == '-') flag = -1; } else if(expression.charAt(i-1) == '(' && ch == '-') flag = -1; //取得数字,并将正负符号转移给数字 if (ch <= '9' && ch >= '0'|| ch == '.' || ch == 'E') { num = expToken.nextToken(); ch_gai = ch; Log.e("guojs",ch+"--->"+i); //取得整个数字 while (i < expression.length() && (ch_gai <= '9' && ch_gai >= '0'|| ch_gai == '.' || ch_gai == 'E')) { ch_gai = expression.charAt(i++); Log.e("guojs","i的值为:"+i); } //将指针退回之前的位置 if (i >= expression.length()) i-=1; else {i-=2;} if (num.compareTo(".") == 0) number[topNum++] = 0; //将正负符号转移给数字 else { number[topNum++] = Double.parseDouble(num)*flag; flag = 1; } } //计算运算符的优先级 if (ch == '(') weightPlus+=4; if (ch == ')') weightPlus-=4; if (ch == '-' && flag == 1 || ch == '+' || ch == '×'|| ch == '÷' || ch == 's' ||ch == 'c' || ch == 't' || ch == 'g' || ch == 'l' || ch == '!' || ch == '√' || ch == '^' || ch == 'h' || ch == 'i' || ch == 'j' || ch == 'k' || ch == 'm' || ch == 'n' || ch == 'o' || ch == 'p' || ch == 'q') { //hijkmn 为新增 //opq 二次新增 switch (ch) { //+-的优先级最低,为1 case '+': case '-': weightTemp = 1 + weightPlus; break; //x÷的优先级稍高,为2 case '×': case '÷': weightTemp = 2 + weightPlus; break; //sincos之类优先级为3 case 's': case 'c': case 't': case 'g': case 'l': //新增hijkmn case 'h': case 'i': case 'j': case 'k': case 'm': case 'n': case 'o': case 'p': case 'q': case '!': weightTemp = 3 + weightPlus; break; //其余优先级为4 //case '^': //case '√': default: weightTemp = 4 + weightPlus; break; } //如果当前优先级大于堆栈顶部元素,则直接入栈 if (topOp == 0 || weight[topOp-1] < weightTemp) { //否则将堆栈中运算符逐个取出,直到当前堆栈顶部运算符的优先级小于当前运算符 }else { while (topOp > 0 && weight[topOp-1] >= weightTemp) { switch (operator[topOp-1]) { //取出数字数组的相应元素进行运算 case '+': number[topNum-2]+=number[topNum-1]; break; case '-': number[topNum-2]-=number[topNum-1]; break; case '×': number[topNum-2]*=number[topNum-1]; break; //判断除数为0的情况 case '÷': if (number[topNum-1] == 0) { showError(1,str_old); return; } number[topNum-2]/=number[topNum-1]; break; case '√': if(number[topNum-1] == 0 || (number[topNum-2] < 0 && number[topNum-1] % 2 == 0)) { showError(2,str_old); return; } number[topNum-2] = Math.pow(number[topNum-2], 1/number[topNum-1]); break; case '^': number[topNum-2] = Math.pow(number[topNum-2], number[topNum-1]); break; //计算时进行角度弧度的转换 //sin case 's': number[topNum-1] = Math.sin((number[topNum-1]/180)*pi); topNum++; break; //cos case 'c': number[topNum-1] = Math.cos((number[topNum-1]/180)*pi); topNum++; break; //tan case 't': if((Math.abs(number[topNum-1])/90)%2 == 1) { showError(2, str_old); return; } number[topNum-1] = Math.tan((number[topNum-1]/180)*pi); topNum++; break; //arcsin case 'h': number[topNum-1] = Math.toDegrees(Math.asin(number[topNum-1])); topNum++; break; //arccos case 'i': number[topNum-1] = Math.toDegrees(Math.acos(number[topNum-1])); topNum++; break; //arctan case 'j': number[topNum-1] = Math.toDegrees(Math.atan(number[topNum-1])); topNum++; break; //sinh case 'k': number[topNum-1] = Math.sinh(number[topNum-1]); topNum++; break; //cosh case 'm': number[topNum-1] = Math.cosh(number[topNum-1]); topNum++; break; //tanh case 'n': number[topNum-1] = Math.tanh(number[topNum-1]); topNum++; break; //asinh case 'o': number[topNum-1] = Math.log(number[topNum-1] + Math.pow(Math.pow(number[topNum-1],2) + 1,0.5d)); topNum++; break; //acosh case 'p': // number[topNum-1] = 1; number[topNum-1] = Math.log(number[topNum-1] + Math.pow(number[topNum-1] + 1,0.5d)*Math.pow(number[topNum-1] - 1,0.5d)); topNum++; break; //atanh case 'q': double operationNumber = number[topNum-1]; number[topNum-1] = Math.log((1 + operationNumber)/(1 - operationNumber))/2; topNum++; break; //log case 'g': if(number[topNum-1] <= 0) { showError(2,str_old); return; } number[topNum-1] = Math.log10(number[topNum-1]); topNum++; break; //ln case 'l': if(number[topNum-1] <= 0) { showError(2,str_old); return; } number[topNum-1] = Math.log(number[topNum-1]); topNum++; break; //阶 case '!': if(number[topNum-1] > 170) { showError(3,str_old); return; } else if(number[topNum-1] < 0) { showError(2,str_old); return; } number[topNum-1] = N(number[topNum-1]); topNum++; break; } //继续取堆栈的下一个元素进行判断 topNum--; topOp--; } //将运算符如堆栈 } weight[topOp] = weightTemp; operator[topOp] = ch; topOp++; } i++; } //依次取出堆栈的运算符进行运算 while (topOp>0) { //+-x直接将数组的后两位数取出运算 switch (operator[topOp-1]) { case '+': number[topNum-2]+=number[topNum-1]; break; case '-': number[topNum-2]-=number[topNum-1]; break; case '×': number[topNum-2]*=number[topNum-1]; break; //涉及到除法时要考虑除数不能为零的情况 case '÷': if (number[topNum-1] == 0) { showError(1,str_old); return; } number[topNum-2]/=number[topNum-1]; break; case '√': if(number[topNum-1] == 0 || (number[topNum-2] < 0 && number[topNum-1] % 2 == 0)) { showError(2,str_old); return; } number[topNum-2] = Math.pow(number[topNum-2], 1/number[topNum-1]); break; case '^': number[topNum-2] = Math.pow(number[topNum-2], number[topNum-1]); break; //sin case 's': number[topNum-1] = Math.sin((number[topNum-1]/180)*pi); topNum++; break; //cos case 'c': number[topNum-1] = Math.cos((number[topNum-1]/180)*pi); topNum++; break; //tan case 't': if((Math.abs(number[topNum-1])/90)%2 == 1) { showError(2,str_old); return; } number[topNum-1] = Math.tan((number[topNum-1]/180)*pi); topNum++; break; //arcsin case 'h': number[topNum-1] = Math.toDegrees(Math.asin(number[topNum-1])); topNum++; break; //arccos case 'i': number[topNum-1] = Math.toDegrees(Math.acos(number[topNum-1])); topNum++; break; //arctan case 'j': number[topNum-1] = Math.toDegrees(Math.atan(number[topNum-1])); topNum++; break; //sinh case 'k': number[topNum-1] = Math.sinh(number[topNum-1]); topNum++; break; //cosh case 'm': number[topNum-1] = Math.cosh(number[topNum-1]); topNum++; break; //tanh case 'n': number[topNum-1] = Math.tanh(number[topNum-1]); topNum++; break; //asinh case 'o': // number[topNum-1] = 1; number[topNum-1] = Math.log(number[topNum-1] + Math.pow(Math.pow(number[topNum-1],2) + 1,0.5d)); topNum++; break; //acosh case 'p': // number[topNum-1] = 1; number[topNum-1] = Math.log(number[topNum-1] + Math.pow(number[topNum-1] + 1,0.5d)*Math.pow(number[topNum-1] - 1,0.5d)); topNum++; break; //atanh case 'q': double operationNumber = number[topNum-1]; number[topNum-1] = Math.log((1 + operationNumber)/(1 - operationNumber))*0.5; topNum++; break; //对数log case 'g': if(number[topNum-1] <= 0) { showError(2,str_old); return; } number[topNum-1] = Math.log10(number[topNum-1]); topNum++; break; //自然对数ln case 'l': if(number[topNum-1] <= 0) { showError(2,str_old); return; } number[topNum-1] = Math.log(number[topNum-1]); topNum++; break; //阶乘 case '!': if(number[topNum-1] > 170) { showError(3,str_old); return; } else if(number[topNum-1] < 0) { showError(2,str_old); return; } number[topNum-1] = N(number[topNum-1]); topNum++; break; } //取堆栈下一个元素计算 topNum--; topOp--; } //如果是数字太大,提示错误信息 if(number[0] > 7.3E306) { showError(3,str_old); return; } //输出最终结果 input.setText(String.valueOf(FP(number[0]))); tip.setText("计算完毕,要继续请按归零键 C"); mem.setText(str_old+"="+String.valueOf(FP(number[0]))); } /* * FP = floating point 控制小数位数,达到精度 * 否则会出现 0.6-0.2=0.39999999999999997的情况,用FP即可解决,使得数为0.4 * 本格式精度为15位 */ public double FP(double n) { //NumberFormat format=NumberFormat.getInstance(); //创建一个格式化类f //format.setMaximumFractionDigits(18); //设置小数位的格式 DecimalFormat format = new DecimalFormat("0.#############"); return Double.parseDouble(format.format(n)); } /* * 阶乘算法 */ public double N(double n) { int i = 0; double sum = 1; //依次将小于等于n的值相乘 for(i = 1;i <= n;i++) { sum = sum*i; } return sum; } /* * 错误提示,按了"="之后,若计算式在process()过程中,出现错误,则进行提示 */ public void showError(int code ,String str) { String message=""; switch (code) { case 1: message = "零不能作除数"; break; case 2: message = "函数格式错误"; break; case 3: message = "值太大了,超出范围"; } input.setText("\""+str+"\""+": "+message); tip.setText(message+"\n"+"计算完毕,要继续请按归零键 C"); } } }
思路
1.通过设置触发器,获取用户通过Button输入的计算表达式
2.对表达式进行检错,看是否有非法输入
3.进行替换,把超过一个字符的运算符或者函数,替换为一个字母代替,如sin->s
4.把这个仅含数字,’-’ ‘+’ ‘×’ ‘÷’ ‘s’ ‘c’ ‘t’ ‘g’ ‘l’’!’ ‘√’ ‘^’ ‘h’ ‘i’ | ‘j’ ‘k’ ‘m’ ‘n’ ‘o’ ‘p’'q’字符的进行从头至尾进行扫描,数字如数字栈,操作符如操作符栈
5.通过逆波兰式算法计算出表达式的结果。总结
1.
在一个已有四则运算功能的计算器框架下进行新增功能其实并没有开始想的那么复杂,想的那么难以实现,实现起来还是比较容易。
2.
多看博客,但也不要看太多,之前去GitHub下载了一些功能非常大,非常商业化的项目,最后都没有用上,耗费了许多时间,还不如静下来好好完善代码。
3.
修改代码一定不能操之过急,不要一次改很多,而是如烹小鲜,修改一点,运行一下,不如很容易心态炸裂。
-
AndroidStudio源码,计算器安卓版源码
2018-12-21 12:23:44就是一个安卓手机上的计算器的源代码,是用Android studio做的Android的原生开发。。。 -
Android studio写的计算器小程序
2017-11-25 10:21:18用Android studio写的计算器小程序,可以实现简单的加减乘除。 -
Android Studio实现简易计算器(表格布局TableLayout)
2020-08-19 10:02:14主要为大家详细介绍了Android Studio实现简易计算器,表格布局TableLayout,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
android studio实现计算器
2021-01-20 09:39:02本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下 效果图: 资源文件: color.xml <?xml version=1.0 encoding=utf-8?> <color name=colorPrimary>#3F51B5 ... -
android 计算器
2017-11-02 15:05:53自己写的一个简易的计算器,功能测试通过,基于android studio开发的,新手练习项目。
-
<em>Android</em> <em>Studio计算器</em><em>Android</em> <em>Studio计算器</em>
-
<em>Android</em> <em>Studio</em>实现<em>计算器</em>本课题要求学生完成一款<em>计算器</em>,从而了解基于<em>android</em>平台如何开发app应用,了解常见UI控件如何使用。 具体实现要求: <em>Android计算器</em>实
-
<em>Android</em> <em>studio</em> 做的<em>计算器</em>简易<em>计算器</em>,大小固定,但是你自己可以改,界面少女心
-
基于<em>Android</em> <em>Studio</em> 开发<em>计算器</em>代码用<em>Android</em> <em>Studio</em> 开发,进行简单的连加、连减、连乘、连除功能
-
<em>Android</em> <em>Studio</em>简易<em>计算器</em>.rar使用java语言,在<em>Android</em> <em>studio</em>平台上进行开发,实现加减乘除的运算功能和清屏的功能,首先设计一个可视化的界面,供用户输入数据并查看结果。用户通过点击