当前位置: 移动技术网 > IT编程>移动开发>Android > Java实现Andriod带看括弧的计算器代码

Java实现Andriod带看括弧的计算器代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

平度孤寡老人去世,台湾代理ip,bt狗狗

废话不多说了,一切尽在代码中,具体代码如下所示:

界面

<?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"
android:background="#ff808080"
>
<!-- 结果显示框 -->
<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="53sp"
android:layout_height="wrap_content" />
<!-- 正弦计算 -->
<button android:id="@+id/sin"
android:text="sin"
android:layout_width="53sp"
android:layout_height="wrap_content" />
<!-- 余弦计算 -->
<button android:id="@+id/cos"
android:text="cos"
android:layout_width="53sp"
android:layout_height="wrap_content" />
<!-- 正切计算 -->
<button android:id="@+id/tan"
android:text="tan"
android:layout_width="53sp"
android:layout_height="wrap_content" />
<!-- 阶乘计算 -->
<button android:id="@+id/factorial"
android:text="n!"
android:layout_width="53sp"
android:layout_height="wrap_content" />
<!-- 退格键 -->
<button android:id="@+id/bksp"
android:text="bksp"
android:layout_width="53sp"
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="60sp"
android:layout_height="wrap_content"
android:text="reminder:" />
<textview android:id="@+id/tip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="欢迎使用!-www.javaapk.com 提供源码" />
</tablerow>
</linearlayout> 

代码

package com.example.yanlei.mytk;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.widget.adapterview;
import android.widget.arrayadapter;
import android.widget.button;
import android.widget.spinner;
import java.util.arraylist;
import java.util.list;
import android.app.activity;
import android.content.context;
import android.os.bundle;
import android.view.view;
import android.view.viewgroup;
import android.view.animation.animationutils;
import android.widget.adapterview;
import android.widget.baseadapter;
import android.widget.gallery;
import android.widget.gallery.layoutparams;
import android.widget.imageswitcher;
import android.widget.imageview;
import android.widget.viewswitcher;
import java.text.decimalformat;
import java.util.stringtokenizer;
import android.os.bundle;
import android.r.integer;
import android.app.activity;
import android.util.log;
import android.view.menu;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.textview;

public class mainactivity extends appcompatactivity {

// 定义变量
private button[] btn = new button[10];// 0~9十个数字
private edittext input;// 用于显示输出结果
private textview mem, _drg, tip;
private button div, mul, sub, add, equal, sin, cos, tan, log, ln, sqrt,
square, factorial, bksp, left, right, dot, exit, drg, mc, c;
public string str_old;
public string str_new;
public boolean vbegin = true;// 控制输入,true为重新输入,false为接着输入
public boolean drg_flag = true;// true为角度,false为弧度
public double pi = 4 * math.atan(1);// π值
public boolean tip_lock = true;// true为正确,可以继续输入,false错误,输入锁定
public boolean equals_flag = true;// 是否在按下=之后输入,true为之前,false为之后
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
initwigdet();
allwigdetlistener();
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.menu_main, menu);
return true;
}
/**
* 初始化所有的组件
*/
private void initwigdet() {
// 获取界面元素
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);
}
/**
* 为所有按键绑定监听器
*/
private void allwigdetlistener() {
// 数字键
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);
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];
int tip_i = 0;// tipcommand的指针
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.()sincostanlnlogn!+-×÷√^".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("welcome use the app!");
}
equals_flag = true;
}
if (tip_i > 0)
tipchecker(tipcommand[tip_i - 1], command);
else if (tip_i == 0) {
tipchecker("#", command);
}
if ("0123456789.()sincostanlnlogn!+-×÷√^".indexof(command) != -1
&& tip_lock) {
tipcommand[tip_i] = command;
tip_i++;
}
// 若输入正确,则将输入信息显示到显示器上
if ("0123456789.()sincostanlnlogn!+-×÷√^".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) {
// 一次删除3个字符
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("welcome use the app!");
}
// 依次删除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("welcome use the app!");
}
// 依次删除一个字符
} 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("welcome use the app!");
}
// 若之前输入的字符串不合法则删除全部字符
} else {
input.settext("0");
vbegin = true;
tip_i = 0;
tip.settext("welcome use the app!");
}
}
if (input.gettext().tostring().compareto("-") == 0
|| equals_flag == false) {
input.settext("0");
vbegin = true;
tip_i = 0;
tip.settext("welcome use the app!");
}
tip_lock = true;
if (tip_i > 0)
tip_i--;
// 如果是在按=之后输入退格键
} else if (command.compareto("bksp") == 0 && equals_flag == false) {
// 将显示器内容设置为0
input.settext("0");
vbegin = true;
tip_i = 0;
tip_lock = true;
tip.settext("welcome use the app!");
// 如果输入的是清除键
} 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("welcome use the app!");
// 如果输入的是”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;
// 替换算式中的运算符,便于计算
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;
}
};
/*
* 检测函数,对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".indexof(tipcommand1) != -1) {
tiptype1 = 4;
} else if ("+-×÷".indexof(tipcommand1) != -1) {
tiptype1 = 5;
} else if ("√^".indexof(tipcommand1) != -1) {
tiptype1 = 6;
} else if ("sincostanloglnn!".indexof(tipcommand1) != -1) {
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".indexof(tipcommand2) != -1) {
tiptype2 = 4;
} else if ("+-×÷".indexof(tipcommand2) != -1) {
tiptype2 = 5;
} else if ("√^".indexof(tipcommand2) != -1) {
tiptype2 = 6;
} else if ("sincostanloglnn!".indexof(tipcommand2) != -1) {
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("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 ("√^sincostanloglnn!".indexof(tipcommand1) != -1) {
tipcode1 = 6;
}
// 若前一个字符是以下之一,表明=号不合法
if ("+-×÷".indexof(tipcommand1) != -1) {
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);
}
/**
* 将信息显示在显示屏上
*/
private void print(string str) {
// 清屏后输出
if (vbegin) {
input.settext(str);
} else {
input.append(str);
}
vbegin = false;
}
/*
* 检测函数,返回值为3、2、1 表示应当一次删除几个? three+two+one = tto 为bksp按钮的删除方式提供依据
* 返回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) == 'n'
&& str.charat(str.length() - 2) == 'i' && str.charat(str
.length() - 3) == 's')
|| (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是否是合法的,返回值为true、false
* 只包含0123456789.()sincostanlnlogn!+-×÷√^的是合法的str,返回true
* 包含了除0123456789.()sincostanlnlogn!+-×÷√^以外的字符的str为非法的,返回false
*/
private boolean right(string str) {
int i = 0;
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) != '!')
break;
}
if (i == str.length()) {
return true;
} else {
return false;
}
}
/*
* 整个计算核心,
* 只要将表达式的整个字符串传入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以+-×÷()sctgl!√^分段,+-×÷()sctgl!√^字符之间的字符串即为数字
weight = new int[maxlen];
number = new double[maxlen];
operator = new char[maxlen];
string expression = str;
stringtokenizer exptoken = new stringtokenizer(expression,
"+-×÷()sctgl!√^");
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 == '^') {
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':
case '!':
weighttemp = 3 + weightplus;
break;
// 其余优先级为4
// case '^':
// case '√':
default:
weighttemp = 4 + weightplus;
break;
}
// 如果当前优先级大于堆栈顶部元素,则直接入栈
if (topop == 0 || weight[topop - 1] < weighttemp) {
weight[topop] = weighttemp;
operator[topop] = ch;
topop++;
// 否则将堆栈中运算符逐个取出,直到当前堆栈顶部运算符的优先级小于当前运算符
} 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':
if (drg_flag == true) {
number[topnum - 1] = math
.sin((number[topnum - 1] / 180)
* pi);
} else {
number[topnum - 1] = math
.sin(number[topnum - 1]);
}
topnum++;
break;
// cos
case 'c':
if (drg_flag == true) {
number[topnum - 1] = math
.cos((number[topnum - 1] / 180)
* pi);
} else {
number[topnum - 1] = math
.cos(number[topnum - 1]);
}
topnum++;
break;
// tan
case 't':
if (drg_flag == true) {
if ((math.abs(number[topnum - 1]) / 90) % 2 == 1) {
showerror(2, str_old);
return;
}
number[topnum - 1] = math
.tan((number[topnum - 1] / 180)
* pi);
} else {
if ((math.abs(number[topnum - 1]) / (pi / 2)) % 2 == 1) {
showerror(2, str_old);
return;
}
number[topnum - 1] = math
.tan(number[topnum - 1]);
}
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':
if (drg_flag == true) {
number[topnum - 1] = math
.sin((number[topnum - 1] / 180) * pi);
} else {
number[topnum - 1] = math.sin(number[topnum - 1]);
}
topnum++;
break;
// cos
case 'c':
if (drg_flag == true) {
number[topnum - 1] = math
.cos((number[topnum - 1] / 180) * pi);
} else {
number[topnum - 1] = math.cos(number[topnum - 1]);
}
topnum++;
break;
// tan
case 't':
if (drg_flag == true) {
if ((math.abs(number[topnum - 1]) / 90) % 2 == 1) {
showerror(2, str_old);
return;
}
number[topnum - 1] = math
.tan((number[topnum - 1] / 180) * pi);
} else {
if ((math.abs(number[topnum - 1]) / (pi / 2)) % 2 == 1) {
showerror(2, str_old);
return;
}
number[topnum - 1] = math.tan(number[topnum - 1]);
}
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");
}
}
}

菜单menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderincategory="100"
android:showasaction="never"
android:title="@string/action_settings"/>
</menu>

以上代码是小编给大家介绍的java实现andriod带看括弧的计算器代码,代码有哪里写的不好,还请各位大侠多多提出宝贵意见,小编会及时和大家取得联系的。同时也非常感谢大家一直以来对移动技术网网站的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网