Android简单计算器计算机制作

article/2025/9/19 8:39:26

在这里插入图片描述
在这里插入图片描述

工程链接

前端界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_vertical"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"><TextViewandroid:id="@+id/hand"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="科学计算器"android:textSize="30dp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"><TextViewandroid:id="@+id/edit"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/shap_rectangle_et"android:maxLines="4"android:minLines="4"android:text=""android:textSize="30dp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center_horizontal"><Buttonandroid:id="@+id/but_CE"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="CE"android:textColor="@color/color_button"android:textSize="30dp" /><Buttonandroid:id="@+id/but_chu"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="/"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_cheng"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="*"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_C"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="C"android:textColor="@color/color_button"android:textSize="50dp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center_horizontal"><Buttonandroid:id="@+id/but_7"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="7"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_8"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="8"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_9"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="9"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_jia"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="+"android:textColor="@color/color_button"android:textSize="50dp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center_horizontal"><Buttonandroid:id="@+id/but_4"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="4"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_5"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="5"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_6"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="6"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_jian"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="-"android:textColor="@color/color_button"android:textSize="50dp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center_horizontal"><Buttonandroid:id="@+id/but_1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="1"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_2"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="2"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_3"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="3"android:textColor="@color/color_button"android:textSize="50dp" /><ImageButtonandroid:id="@+id/but_eq"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:scaleType="fitCenter"android:src="@drawable/select_button_src_sqrt"></ImageButton></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center_horizontal"><Buttonandroid:id="@+id/but_0"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="2"android:background="@drawable/select_button"android:text="0"android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_dian"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="."android:textColor="@color/color_button"android:textSize="50dp" /><Buttonandroid:id="@+id/but_dengyu"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@drawable/select_button"android:text="="android:textColor="@color/color_button"android:textSize="50dp" /></LinearLayout></LinearLayout>

后端逻辑


import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener {TextView editText;ImageButton but_eq;Button but_cheng;Button but_0;Button but_1;Button but_2;Button but_3;Button but_4;Button but_5;Button but_6;Button but_7;Button but_8;Button but_9;Button but_dian;Button but_dengyu;Button but_chu;Button but_jia;Button but_jian;Button but_C;Button but_CE;String input = "";Boolean clear_flag = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText = findViewById(R.id.edit);but_0 = findViewById(R.id.but_0);but_1 = findViewById(R.id.but_1);but_2 = findViewById(R.id.but_2);but_3 = findViewById(R.id.but_3);but_4 = findViewById(R.id.but_4);but_5 = findViewById(R.id.but_5);but_6 = findViewById(R.id.but_6);but_7 = findViewById(R.id.but_7);but_8 = findViewById(R.id.but_8);but_9 = findViewById(R.id.but_9);but_dian = findViewById(R.id.but_dian);but_dengyu = findViewById(R.id.but_dengyu);but_cheng = findViewById(R.id.but_cheng);but_chu = findViewById(R.id.but_chu);but_jia = findViewById(R.id.but_jia);but_jian = findViewById(R.id.but_jian);but_eq = findViewById(R.id.but_eq);but_C = findViewById(R.id.but_C);but_CE = findViewById(R.id.but_CE);but_0.setOnClickListener(this);but_1.setOnClickListener(this);but_2.setOnClickListener(this);but_3.setOnClickListener(this);but_4.setOnClickListener(this);but_5.setOnClickListener(this);but_6.setOnClickListener(this);but_7.setOnClickListener(this);but_8.setOnClickListener(this);but_9.setOnClickListener(this);but_dian.setOnClickListener(this);but_jia.setOnClickListener(this);but_jian.setOnClickListener(this);but_cheng.setOnClickListener(this);but_chu.setOnClickListener(this);but_eq.setOnClickListener(this);but_C.setOnClickListener(this);but_CE.setOnClickListener(this);but_dengyu.setOnClickListener(this);}@Overridepublic void onClick(View v) {try {//获取文本内容input = editText.getText().toString();switch (v.getId()) {case R.id.but_0:case R.id.but_1:case R.id.but_2:case R.id.but_3:case R.id.but_4:case R.id.but_5:case R.id.but_6:case R.id.but_7:case R.id.but_8:case R.id.but_9:case R.id.but_dian:case R.id.but_jia:case R.id.but_jian:case R.id.but_cheng:case R.id.but_chu:if (clear_flag) {clear_flag = false;input = "";editText.setText("");}editText.setText(input + ((Button) v).getText());break;case R.id.but_eq:if (clear_flag) {clear_flag = false;input = "";editText.setText("");}if ((input.length() != 0) && (input.charAt(input.length() - 1) >= '0' && input.charAt(input.length() - 1) <= '9')) {editText.setText(input + "*√");} else editText.setText(input + "√");break;case R.id.but_dengyu:if (input != null || input.length() != 0) {editText.setText(Calculate.getResult(input));clear_flag = true;}break;case R.id.but_C:case R.id.but_CE:editText.setText("");break;default:input = "";editText.setText(input);}} catch (Exception e) {//异常输出editText.setText("语法错误");clear_flag = true;}}
}

数据处理类


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;public class Calculate {//处理字符得出结果public static String getResult(String cal) {return cal(youhua(cal)).toString();}// 计算处理根号 public static String youhua(String str) {String box = new String();int i;for (i = str.length() - 1; i >= 0; i--) {if ('√' == str.charAt(i)) {int a = i + 1;while (a < str.length()) {if ('+' == str.charAt(a) || '-' == str.charAt(a) || '*' == str.charAt(a) || '/' == str.charAt(a)) {box = str.substring(i + 1, a);str = str.replaceAll(str.substring(i, a),String.valueOf(Math.sqrt(cal(str.substring(i + 1, a)).doubleValue())));a = i;break;}a++;}if (a == str.length()) {str = str.substring(0, i) + String.valueOf(Math.sqrt(cal(str.substring(i + 1, a)).doubleValue()));i = str.length() - 1;a = i;}}}return str;}public static BigDecimal cal(String str) {// 对表达式进行预处理,并简单验证是否是正确的表达式// 存放处理后的表达式List<String> list = new ArrayList<>();char[] arr = str.toCharArray();// 存放数字临时变量StringBuffer tmpStr = new StringBuffer();for (char c : arr) {// 如果是数字或小数点,添加到临时变量中if (c >= '0' && c <= '9') {tmpStr.append(c);} else if (c == '.') {if (tmpStr.indexOf(".") > 0) {throw new RuntimeException("非法字符");}tmpStr.append(c);}// 如果是加减乘除或者括号,将数字临时变量和运算符依次放入list中else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')') {if (tmpStr.length() > 0) {list.add(tmpStr.toString());tmpStr.setLength(0);}list.add(c + "");}// 如果是空格,跳过else if (c == ' '||c=='\n') {continue;} else {throw new RuntimeException("非法字符");}}if (tmpStr.length() > 0) {list.add(tmpStr.toString());}// 初始化后缀表达式List<String> strList = new ArrayList<>();// 运算过程中,使用了两次栈结构,第一次是将中缀表达式转换为后缀表达式,第二次是计算后缀表达式的值Stack<String> stack = new Stack<>();// 声明临时变量,存放出栈元素String tmp;// 1. 将中缀表达式转换为后缀表达式for (String s : list) {// 如果是左括号直接入栈if (s.equals("(")) {stack.push(s);}// 如果是右括号,执行出栈操作,依次添加到后缀表达式中,直到出栈元素为左括号,左括号和右括号都不添加到后缀表达式中else if (s.equals(")")) {while (!(tmp = stack.pop()).equals("(")) {strList.add(tmp);}}// 如果是加减乘除,弹出所有优先级大于或者等于该运算符的栈顶元素(栈中肯定没有右括号,认为左括号的优先级最低),然后将该运算符入栈else if (s.equals("*") || s.equals("/")) {while (!stack.isEmpty()) {// 取出栈顶元素tmp = stack.peek();if (tmp.equals("*") || tmp.equals("/")) {stack.pop();strList.add(tmp);} else {break;}}stack.push(s);} else if (s.equals("+") || s.equals("-")) {while (!stack.isEmpty()) {// 取出栈顶元素tmp = stack.peek();if (!tmp.equals("(")) {stack.pop();strList.add(tmp);} else {break;}}stack.push(s);}// 如果是数字,直接添加到后缀表达式中else {strList.add(s);}}// 最后依次出栈,放入后缀表达式中while (!stack.isEmpty()) {strList.add(stack.pop());}// 2.计算后缀表达式的值Stack<BigDecimal> newStack = new Stack<>();for (String s : strList) {// 若遇运算符,则从栈中退出两个元素,先退出的放到运算符的右边,后退出的放到运算符左边,// 运算后的结果再进栈,直到后缀表达式遍历完毕if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {BigDecimal b1 = newStack.pop();BigDecimal b2 = newStack.pop();switch (s) {case "+":newStack.push(b2.add(b1));break;case "-":newStack.push(b2.subtract(b1));break;case "*":newStack.push(b2.multiply(b1));break;case "/":newStack.push(b2.divide(b1, 9, BigDecimal.ROUND_HALF_UP));break;}}// 如果是数字,入栈else {newStack.push(new BigDecimal(s));}}// 最后,栈中仅有一个元素,就是计算结果return newStack.peek();}}

http://chatgpt.dhexx.cn/article/ivlvAgW5.shtml

相关文章

用Android实现计算器

今天课程&#xff1a; 一、点击事件的添加 ①首先我们应该用写入EditText可编辑文本框&#xff0c;它有一个属性 android:hint"" 可在文本框里写入字符&#xff0c;当你写入其他的字符时会消失&#xff1b;写一个Button按钮标签&#xff0c;属性 android:onClick…

Android studio制作计算器源代码

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。https://mp.csdn.net/postedit/82623704 一、Android studio制作计算器源代码 这是我学Android 以来第一次制作计算器&#xff0c;Android学起来说难也不难&#xff0c;说简单也不简单 制作简易的计…

Android入门之简易计算器(一)

Android Android入门之简易计算器&#xff08;一&#xff09; 文章目录 AndroidAndroid入门之简易计算器&#xff08;一&#xff09;[TOC](文章目录) 前言一、项目结构二、界面显示三、前端界面1.界面分布2.文本定义在了strings.xml里面3、圆形按钮创建 四、前端页面总代码五、…

Android 实战项目:简单计算器

文章目录 实战项目&#xff1a;简易计算器1.需求分析2.界面设计3.关键代码1&#xff0e;输入按键的合法性校验2&#xff0e;执行运算并显示计算结果 简单计算器 - 详细操作步骤总结 实战项目&#xff1a;简易计算器 1.需求分析 虽然只学了一些Android的简单控件&#xff0c;但…

Android Studio制作简单计算器

代码地址 https://github.com/xjhqre/android_calculator 效果演示 1. 连续加法 2. 连续减法 3. 连续乘法 4. 连续除法 5. 优先级运算 6. 退格功能 7. 错误提示 制作步骤 1. 创建按钮图片 1.1. 退格图形 <vector xmlns:android"http://schemas.android.com/apk/…

【Android应用开发之前端——简单计算器效果】

1.完成计算器布局 整个计算器界面主要分为两部分&#xff0c;一部分是上面的文本框&#xff0c;用于显示计算结果&#xff1b;另一部分是下面的几排按钮&#xff0c;用户输入数字与各种运算符。为了减少复杂度&#xff0c;我们可以精简一些功能&#xff0c;只保留数字与加、减…

使用Android 实现计算器功能

使用android实现简易的计算器的功能 1、给计算机布局&#xff1a;activity_main_xml&#xff1a; <?xml version"1.0" encoding"utf-8"?> <GridLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"h…

android studio实现计算器界面

实现计算器界面 新建项目1、打开Android Studio2、创建项目3、设置项目基本信息4、等待项目文件加载 打开界面文件1、设置线性布局2、增加子容器3、添加内容 计算器界面完成效果 新建项目 1、打开Android Studio 2、创建项目 点击右上角【New Project】选择“Empty Activity”…

Android Studio实现计算器功能

实验一&#xff1a;做一个简单的计算器 1.创建布局文件Activity_main.xml 代码如下&#xff1a; <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:orient…

Android Studio入门教程(计算器)

一、建立开发环境 1、AS简介 Android Studio 是Google开发的一款面向Android开发者的IDE&#xff0c;支持Windows、Mac、Linux等操作系统&#xff0c;基于流行的开发语言java集成开发环境IntelliJ搭建而成的&#xff0c;类似Eclipse ADT。该IDE在2003年5月的Google I/O开发者…

Android开发——简单计算器实现

计算器项目&#xff0c;要求实现加、减、乘、除、求倒数、求平方根等简单运算。 真机调试结果如下图&#xff1a; 布局文件&#xff1a;main_activity.xml <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://sc…

Android Studio简易计算器

目录 第一步&#xff0c;创建新项目 第二步&#xff0c;设计UI 第三步&#xff0c;实现计算逻辑 第四步&#xff0c;测试应用程序 随着移动互联网的普及&#xff0c;手机应用程序已经成为人们生活中不可或缺的一部分。计算器是一类被广泛使用的应用程序之一&#xff0c;因此…

十五、吉布斯采样的理解

由于本人喜欢在纸上手推原理&#xff0c;所以附上照片&#xff0c;欢迎提出建议

MCMC、吉布斯采样

学习视频&#xff1a;B站白板推导 学习和代码教程&#xff1a; Li Hang code 知乎&#xff08;和上面内容一模一样&#xff09; &#xff08;权当参考&#xff09; 关于上面代码中&#xff0c;吉布斯采样二维正态分布的理解&#xff1a; Σ的意思是协方差矩阵&#xff0c;在…

LDA----吉布斯采样

w~Mult(w|p) 这里可以引入一个新的概念:概率图模型,来画出这种模型。如图 3-1所示,图中被涂色的 表示可观测变量,方框表示重复抽取的次数, 表示一篇文档中总共 个单词, 表示M篇文档。也就是说,重复抽取 篇文档,每个文档抽取 个单词,这样的生成模型生成了整个语料(corpus)。 总结…

LDA-模型的实现-----吉布斯采样

https://www.cnblogs.com/nlp-yekai/p/3858705.html?utm_sourcetuicool&utm_mediumreferral 算法 LDA Collapsed Gibbs Sampling 输入&#xff1a;文档集(分词后)&#xff0c;K(主题数)&#xff0c;α&#xff0c;β&#xff0c;iter_number(迭代次数) 输出&#xff1a;…

R语言实现MCMC中的Metropolis–Hastings算法与吉布斯采样

创建测试数据 第一步&#xff0c;我们创建一些测试数据&#xff0c;用来拟合我们的模型。我们假设预测变量和因变量之间存在线性关系&#xff0c;所以我们用线性模型并添加一些噪音。 trueA <- 5trueB <- 0trueSd <- 10sampleSize <- 31# 创建独立的x值x <- (…

马尔科夫过程与吉布斯采样

随机模拟(或者统计模拟)方法有一个很酷的别名是蒙特卡罗方法(Monte Carlo Simulation)。这个方法的发展始于20世纪40年代&#xff0c;和原子弹制造的曼哈顿计划密切相关&#xff0c;当时的几个大牛&#xff0c;包括乌拉姆、冯.诺依曼、费米、费曼、Nicholas Metropolis&#xf…

吉布斯采样的简单描述

几个可以学习gibbs sampling的方法1&#xff0c;读Bishop的Pattern Recognition and Machine Learning&#xff0c;讲的很清楚&#xff0c;但是我记得好像没有例子。2&#xff0c;读artificial Intelligence&#xff0c;2、3版&#xff0c;都有。但是我没读过。3&#xff0c;最…

【ML】线性回归的吉布斯采样(Gibbs Sampling)实现(python)

导航 Bayesian Linear RegressionGibbs SamplingDerving a Gibbs samplerUpdate for β 0 \beta_0 β0​Update for β 1 \beta_1 β1​Update for τ \tau τSynthetic dataGibbs sampler code downlaodReferences Bayesian Linear Regression 考虑只有一个自变量(indepen…