说明

代码逻辑部分是copy的,为了应付课设,就自己弄个布局,本人小白啥也不懂,参考
链接链接地址

运行结果



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:layout_editor_absoluteX="0dp"tools:layout_editor_absoluteY="0dp"><EditTextandroid:id="@+id/editView"android:layout_width="360dp"android:layout_height="80dp"android:layout_centerHorizontal="true"android:layout_marginTop="10dp"android:background="@drawable/border"android:editable="false"android:gravity="bottom|right"android:hint="please enter..."android:textSize="24sp" /><GridLayoutandroid:id="@+id/GridLayout1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/editView"android:layout_centerHorizontal="true"android:layout_marginTop="20dp"android:columnCount="4"android:orientation="horizontal"android:rowCount="9"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="clickButton"android:text="(" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="clickButton"android:text=")" /><Buttonandroid:layout_width="174dp"android:layout_columnSpan="2"android:onClick="delete"android:text="删除" /><Buttonandroid:onClick="square"android:text="x^2" /><Buttonandroid:onClick="cube"android:text="x^3" /><Buttonandroid:id="@+id/button18"android:layout_width="175dp"android:layout_height="wrap_content"android:layout_columnSpan="2"android:onClick="empty"android:text="清空" /><Buttonandroid:onClick="power"android:text="x^y" /><Buttonandroid:onClick="factorial"android:text="x!" /><Buttonandroid:onClick="squareRoot"android:text="√" /><Buttonandroid:onClick="ln"android:text="ln" /><Buttonandroid:onClick="log"android:text="log" /><Buttonandroid:onClick="eulerNumber"android:text="e" /><Buttonandroid:onClick="sin"android:text="sin" /><Buttonandroid:onClick="cos"android:text="cos" /><Buttonandroid:onClick="tan"android:text="tan" /><Buttonandroid:onClick="pi"android:text="π" /><Buttonandroid:onClick="reciprocal"android:text="1/x" /><Buttonandroid:onClick="percentage"android:text="%" /><Buttonandroid:onClick="clickButton"android:text="+" /><Buttonandroid:onClick="clickButton"android:text="1" /><Buttonandroid:onClick="clickButton"android:text="2" /><Buttonandroid:onClick="clickButton"android:text="3" /><Buttonandroid:onClick="clickButton"android:text="-" /><Buttonandroid:onClick="clickButton"android:text="4" /><Buttonandroid:onClick="clickButton"android:text="5" /><Buttonandroid:onClick="clickButton"android:text="6" /><Buttonandroid:onClick="clickButton"android:text="*" /><Buttonandroid:onClick="clickButton"android:text="7" /><Buttonandroid:onClick="clickButton"android:text="8" /><Buttonandroid:onClick="clickButton"android:text="9" /><Buttonandroid:onClick="clickButton"android:text="/" /><Buttonandroid:onClick="clickButton"android:text="." /><Buttonandroid:onClick="clickButton"android:text="0" /><Buttonandroid:onClick="equal"android:text="=" /></GridLayout><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="10dp"android:textColor="#FF0000"android:textSize="20sp" /></RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.calculator;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;import android.widget.TextView;import java.util.ArrayList;
import java.util.List;
import java.util.Stack;public class MainActivity extends AppCompatActivity {private EditText editText;private TextView text;  //输入格式错误提示private Button button;private StringBuilder str = new StringBuilder();  //参与运算的式子private int indexYN = 0;    //是否出错的标志@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText = (EditText) findViewById(R.id.editView);text = (TextView) findViewById(R.id.textView);}//数字及符号按钮public void clickButton(View view){button = (Button) view;editText.append(button.getText());str.append(button.getText());}//清空public void empty(View view){editText.setText(null);str.delete(0,str.length());}//删除public void delete(View view){String nowText = editText.getText().toString();if(nowText.length()!=0&&str.length()!=0){editText.setText(nowText.substring(0,nowText.length()-1));str.deleteCharAt(str.length()-1);}}//等于public void equal(View view){indexYN = 0;System.out.println("str:\t"+str);System.out.println("lenth:\t"+str.length());estimate();if(indexYN==0){List<String> zhongZhui = zhuanZhongZhui(str.toString());System.out.println(zhongZhui);List<String> houZhui = zhuanHouZhui(zhongZhui);System.out.println(houZhui);editText.append("\n"+math(houZhui));str.delete(0,str.length());str.append(math(houZhui));}}//倒数public void reciprocal(View view){editText.append("1/");str.append("1/");}//阶乘public void factorial(View view){editText.append("!");str.append("!");}//平方public void square(View view){editText.append("^2");str.append("^2");}//立方public void cube(View view){editText.append("^3");str.append("^3");}//幂运算public void power(View view){editText.append("^");str.append("^");}//开方public void squareRoot(View view){editText.append("√");str.append("g");}//欧拉数epublic void eulerNumber(View view){editText.append("e");str.append("e");}//百分数public void percentage(View view){editText.append("%");str.append("*0.01");}//圆周率public void pi(View view){editText.append("π");str.append("p");}//sinpublic void sin(View view){editText.append("sin");str.append("s");}//cospublic void cos(View view){editText.append("cos");str.append("c");}//tanpublic void tan(View view){editText.append("tan");str.append("t");}//lnpublic void ln(View view){editText.append("ln");str.append("l");}//logpublic void log(View view){editText.append("log");str.append("o");}private List<String> zhuanZhongZhui(String str) {//把输入的字符串转换成中缀表达式。存入list中int index = 0;List<String> list = new ArrayList<>();do{char ch = str.charAt(index);if("+-*/^!logsct()".indexOf(str.charAt(index)) >= 0){//是操作符,直接添加至list中index ++;list.add(ch+"");}else if (str.charAt(index) == 'e' || str.charAt(index) == 'p'){index ++;list.add(ch+"");} else if("0123456789".indexOf(str.charAt(index)) >= 0){//是数字,判断多位数的情况String str1 = "";while (index < str.length() && "0123456789.".indexOf(str.charAt(index)) >= 0){str1 += str.charAt(index);index ++;}list.add(str1);}}while (index < str.length());return list;}public List<String> zhuanHouZhui(List<String> list){//中缀表达式转换称后缀表达式Stack<String> fuZhan = new Stack<>();List<String> list2 = new ArrayList<>();if (!list.isEmpty()) {for (int i = 0; i < list.size(); i++) {if (isNumber(list.get(i))){list2.add(list.get(i));} else if (list.get(i).charAt(0) == '('){fuZhan.push(list.get(i));} else if (isOperator(list.get(i)) && list.get(i).charAt(0) != '('){if (fuZhan.isEmpty()){fuZhan.push(list.get(i));} else {//符栈不为空if (list.get(i).charAt(0) != ')'){if (adv(fuZhan.peek()) <= adv(list.get(i))){//入栈fuZhan.push(list.get(i));} else {//出栈while (!fuZhan.isEmpty() && !"(".equals(fuZhan.peek())){if(adv(list.get(i)) <= adv(fuZhan.peek())){list2.add(fuZhan.pop());}}if (fuZhan.isEmpty() || fuZhan.peek().charAt(0) == '('){fuZhan.push(list.get(i));}}} else if (list.get(i).charAt(0) == ')'){while (fuZhan.peek().charAt(0) != '('){list2.add(fuZhan.pop());}fuZhan.pop();}}}}while (!fuZhan.isEmpty()){list2.add(fuZhan.pop());}} else {editText.setText("");}return list2;}public static boolean isOperator(String op){//判断是否为操作符if ("0123456789.ep".indexOf(op.charAt(0)) == -1) {return true;} else {return false;}}public static boolean isNumber(String num){//判断是否为操作数if ("0123456789ep".indexOf(num.charAt(0)) >= 0) {return true;} else {return false;}}public static int adv(String f){//判断操作符的优先级int result = 0;switch(f) {case "+":result = 1;break;case "-":result = 1;break;case "*":result = 2;break;case "/":result = 2;break;case "^":result = 3;break;case "!":result = 4;break;case "g":result = 4;break;case "l":result = 4;break;case "o":result = 4;break;case "s":result = 4;break;case "c":result = 4;break;case "t":result = 4;break;}return result;}public double math(List<String> list2) {//通过后缀表达式进行计算Stack<String> stack = new Stack<String>();for (int i = 0; i < list2.size(); i++) {if (isNumber(list2.get(i))) {if (list2.get(i).charAt(0) == 'e'){stack.push(String.valueOf(Math.E));} else if (list2.get(i).charAt(0) == 'p'){stack.push(String.valueOf(Math.PI));} else {stack.push(list2.get(i));}} else if (isOperator(list2.get(i))){double res = 0;if (list2.get(i).equals("+")) {double num2 = Double.parseDouble(stack.pop());double num1 = Double.parseDouble(stack.pop());res = num1 + num2;} else if (list2.get(i).equals("-")) {double num2 = Double.parseDouble(stack.pop());double num1 = Double.parseDouble(stack.pop());res = num1 - num2;} else if (list2.get(i).equals("*")) {double num2 = Double.parseDouble(stack.pop());double num1 = Double.parseDouble(stack.pop());res = num1 * num2;} else if (list2.get(i).equals("/")) {double num2 = Double.parseDouble(stack.pop());double num1 = Double.parseDouble(stack.pop());if (num2 != 0){res = num1 / num2;} else {editText.setText("除数不能为0");indexYN = 1;}} else if (list2.get(i).equals("^")) {double num2 = Double.parseDouble(stack.pop());double num1 = Double.parseDouble(stack.pop());res = Math.pow(num1, num2);} else if (list2.get(i).equals("!")) {double num1 = Double.parseDouble(stack.pop());if (num1 == 0 || num1 == 1){res = 1;} else if (num1 == (int)num1 && num1 > 1){int d = 1;for (int j = (int)num1; j >0; j--) {d *= j;}res = d;} else {editText.setText("阶乘必须为自然数");indexYN = 1;}} else if (list2.get(i).equals("g")) {double num1 = Double.parseDouble(stack.pop());res = Math.sqrt(num1);} else if (list2.get(i).equals("l")) {double num1 = Double.parseDouble(stack.pop());if (num1 > 0){res = Math.log(num1);} else {editText.setText("ln的x必须大于0");indexYN = 1;}} else if (list2.get(i).equals("o")) {double num1 = Double.parseDouble(stack.pop());if (num1 > 0){res = Math.log(num1) / Math.log(2);} else {editText.setText("log的x必须大于0");indexYN = 1;}} else if (list2.get(i).equals("s")) {double num1 = Double.parseDouble(stack.pop());res = Math.sin(num1);} else if (list2.get(i).equals("c")) {double num1 = Double.parseDouble(stack.pop());res = Math.cos(num1);} else if (list2.get(i).equals("t")) {double num1 = Double.parseDouble(stack.pop());if (Math.cos(num1) != 0){res = Math.tan(num1);} else {editText.setText("tan的x不能为+-(π/2 + kπ)");indexYN = 1;}}stack.push("" + res);}}if (indexYN == 0){if (!stack.isEmpty()){return Double.parseDouble(stack.pop());} else {return 0;}} else {return -999999;}}public void estimate(){//判断输入是否错误int i = 0;if (str.length() == 0){text.setText("输入为空!");indexYN = 1;}if (str.length() == 1){//当只有一位字符时,只能是“0123456789ep”中的一个if ("0123456789ep".indexOf(str.charAt(0)) == -1){text.setText("输入错误!");indexYN = 1;}}if (str.length() > 1){for (i = 0; i < str.length() - 1; i++) {//1.第一个字符只能为"losctg(0123456789ep"中的一个if ("losctg(0123456789ep".indexOf(str.charAt(0)) == -1){text.setText("输入错误!");indexYN = 1;}//2.“+-*/”后面只能是"0123456789losctg(ep"中的一个if ("+-*/".indexOf(str.charAt(i)) >= 0 && "0123456789losctg(ep".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}//3."."后面只能是“0123456789”中的一个if (str.charAt(i) == '.' && "0123456789".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}//4."!"后面只能是“+-*/^)”中的一个if (str.charAt(i) == '!' && "+-*/^)".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}//5."losctg"后面只能是“0123456789(ep”中的一个if ("losctg".indexOf(str.charAt(i)) >= 0 && "0123456789(ep".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}//6."0"的判断操作if (str.charAt(0) == '0' && str.charAt(1) == '0'){text.setText("输入错误!");indexYN = 1;}if (i >= 1 && str.charAt(i) == '0'){//&& str.charAt(0) == '0' && str.charAt(1) == '0'int m = i;int n = i;int is = 0;//1.当0的上一个字符不为"0123456789."时,后一位只能是“+-*/.!^)”中的一个if ("0123456789.".indexOf(str.charAt(m - 1)) == -1 && "+-*/.!^)".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}//2.如果0的上一位为“.”,则后一位只能是“0123456789+-*/.^)”中的一个if (str.charAt(m - 1) == '.' && "0123456789+-*/.^)".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}n -= 1;while (n > 0){if ("(+-*/^glosct".indexOf(str.charAt(n)) >= 0){break;}if (str.charAt(n) == '.'){is++;}n--;}//3.如果0到上一个运算符之间没有“.”的话,整数位第一个只能是“123456789”,//  且后一位只能是“0123456789+-*/.!^)”中的一个。if ((is == 0 && str.charAt(n) == '0') || "0123456789+-*/.!^)".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}//4.如果0到上一个运算符之间有一个“.”的话,则后一位只能是“0123456789+-*/.^)”中的一个if (is == 1 && "0123456789+-*/.^)".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}if (is > 1){text.setText("输入错误!");indexYN = 1;}}//7."123456789"后面只能是“0123456789+-*/.!^)”中的一个if ("123456789".indexOf(str.charAt(i)) >= 0 && "0123456789+-*/.!^)".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}//8."("后面只能是“0123456789locstg()ep”中的一个if (str.charAt(i) == '(' && "0123456789locstg()ep".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}//9.")"后面只能是“+-*/!^)”中的一个if (str.charAt(i) == ')' && "+-*/!^)".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}//10.最后一位字符只能是“0123456789!)ep”中的一个if ("0123456789!)ep".indexOf(str.charAt(str.length() - 1)) == -1){text.setText("输入错误!");indexYN = 1;}//12.不能有多个“.”if (i > 2 && str.charAt(i) == '.'){int n = i - 1;int is = 0;while (n > 0){if ("(+-*/^glosct".indexOf(str.charAt(n)) >= 0){break;}if (str.charAt(n) == '.'){is++;}n--;}if (is > 0){text.setText("输入错误!");indexYN = 1;}}//13."ep"后面只能是“+-*/^)”中的一个if ("ep".indexOf(str.charAt(i)) >= 0 && "+-*/^)".indexOf(str.charAt(i + 1)) == -1){text.setText("输入错误!");indexYN = 1;}}}}
}

border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><solid android:color="#fff" />
<!--    //设置该控件的背景颜色-->
<!--    //设置边距--><paddingandroid:bottom="10dp"android:left="10dp"android:right="10dp"android:top="10dp" /><!--    //控制边界线颜色和笔触大小--><strokeandroid:width="1dp"android:color="#CdCdCd" />
<!--    //控制界面颜色渐变(你这个用不到)--><gradientandroid:startColor="#E9E9E9"android:endColor="#FFFFFF"android:type="linear"android:angle="90"/>
<!--    //控制圆角大小--><corners android:radius="2dp" />
<!--    //如果需要制定每个角度大小--><cornersandroid:topRightRadius="2dp"android:bottomRightRadius="2dp"android:topLeftRadius="2dp"android:bottomLeftRadius="2dp"/>
</shape>

安卓开发 科学计算器相关推荐

  1. 手写计算器java_可编程科学计算器app

    可编程科学计算器app是一款功能超级强大的科学计算器,它通过自建程序解决很多工程测算的问题!欢迎有需要的朋友在芒果下载站下载体验! 可编程科学计算器app官方介绍 程序开发科学计算器是一个和Matla ...

  2. 可编程计算机边长计算器,可编程科学计算器(Scientific Calculator Plus)

    Scientific Calculator Plus这款手机可编程计算器ui做的不怎么好看,不过胜在功能强大,它是一个和Matlab相似的强大的数学分析工具.除了其他大部分计算器所拥有的基本功能. 功 ...

  3. android商务办公型计算器开发与设计,可编程科学计算器v1.6.7.56 安卓版_Scientific Calculator Plus-腾牛安卓网...

    可编程科学计算器 Scientific Calculator Plus 是一款台湾同胞开发的高级计算器软件,原名程式开发科学计算器.普通的函数计算什么的自然不再话下,他还支持复数计算.阵列.不定积分三 ...

  4. android角度计算器,Calckit高级版一款安卓多功能计算器,内置高度定制的科学计算器...

    前言 手机中的计算器功能你多久用一次呢,我相信对于多数人来说计算器是一个低频使用软件. 其原因可能是用不上计算功能也很可能是单一的计算功能并不能满足你对于计算的需求. 那么如果现在有一款可以完成中学到 ...

  5. 安卓matlab+calculator,Calculator科学计算器

    Calculator科学计算器是一款非常优秀的计算器软件,软件计算功能是十分丰富的,适合科学家.工程师.学生等用户群体.Calculator科学计算器是值得您下载的科学计算工具. 相关软件软件大小版本 ...

  6. 科学计算机安卓图像,Plotter图形科学计算器

    Plotter图形科学计算器(Plotter)是一款手机图形计算器软件.Plotter图形科学计算器app支持多种函数以及常数用户可以使用此软件进行复杂数学公式的计算,还可以进行复杂的函数计算,操作简 ...

  7. 中专计算机学数学,科学计算器在中职数学教学中的意义

    科学计算器在中职数学教学中的意义 (7页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 9.90 积分 科学计算器在中职数学教学中的意义 摘 要:科学计算器 ...

  8. Android Studio小作业:科学计算器

    背景 上一章,我们完成基于Xamarin.Android的简单计算器 今天突发奇想,做一个科学计算器 支持标准四则运算.括号.小数点.三角函数.log.ln.倒数.阶乘.幂.算数平方根.百分数 开发环 ...

  9. 从0开始安卓开发之路_Android Studio安装包

    Android Studio原本是基于JAVA IDEA下的一个安卓开发插件,后被谷歌从插件中移了出来,成为独立安卓开发IDE,但语言是基于JAVA! 但是如果想要下载此IDE就要到谷歌官方的应用商店 ...

最新文章

  1. WEB+windows集群
  2. [总结篇4] l2-agent的细节
  3. toolkit,phonetextbox中实现用户按回车键会换行
  4. Nginx教程系列五:Nginx+Keepalived搭建高可用主从架构
  5. 为不同目录设置Forms身份验证
  6. 重装Nodejs后,webstorm代码报错问题
  7. mysql 5.6 command line client闪退_MySQL 5.6 Command Line Client 点开闪退解决方法
  8. Makefile .PHONY用法
  9. RuntimeError: failed to execute [‘dot‘, ‘-Tpdf‘, ‘-O‘, ‘test‘], make sure the Graphviz executables
  10. Harris角点检测原理分析
  11. 易邮服务器com组件注册失败,com组件注册失败有什么办法可以解决
  12. vim下替换字符串命令
  13. 华为手机如何给应用加锁_如何设置华为手机应用程序锁?
  14. grpc报错rpc error:code=DeadlineExceeded desc = context deadline exceeded
  15. 小瘦牛虚拟无线路由器官方版
  16. Android解析SRT字幕文件
  17. 小程序loding动画组件封装及源码
  18. 关于ttyS与ttySAC
  19. python实现base64解码_Python实现base64编码解码
  20. 常对象只能调用常函数成员 c++

热门文章

  1. 健身俱乐部综合管理系统/健身房管理系统
  2. 数字信号处理(8)- 频域采样定理
  3. UPS市场排名“第一”泛滥 三点绝招巧识猫腻
  4. Android开发应该了解的Binder原理,分享PDF高清版
  5. JAVA基于的智慧小区计算机毕业设计Mybatis+系统+数据库+调试部署
  6. 网络3共享网络2计算机打印机,打印机如何共享到另一台电脑
  7. MyBatis快速上手与知识总结
  8. python是不是现在主流的人工智能编程语言_2020年人工智能的5种最佳编程语言
  9. python 投票软件——含UI界面
  10. java程序设计作业_Java程序设计作业