这个的完整代码也请给出,在activity_main.xml文件中添加一个TextView用于显示计算结果和一些Button用于输入数字和运算符。

时间: 2024-05-04 07:21:15 浏览: 12
好的,以下是一个简单的计算器的完整代码: MainActivity.java ```java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView resultTextView; private Button clearButton, divideButton, multiplyButton, backButton, sevenButton, eightButton, nineButton, minusButton, fourButton, fiveButton, sixButton, plusButton, oneButton, twoButton, threeButton, equalsButton, zeroButton, dotButton; private StringBuilder calculationString = new StringBuilder(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find views by ID resultTextView = findViewById(R.id.result_text_view); clearButton = findViewById(R.id.clear_button); divideButton = findViewById(R.id.divide_button); multiplyButton = findViewById(R.id.multiply_button); backButton = findViewById(R.id.back_button); sevenButton = findViewById(R.id.seven_button); eightButton = findViewById(R.id.eight_button); nineButton = findViewById(R.id.nine_button); minusButton = findViewById(R.id.minus_button); fourButton = findViewById(R.id.four_button); fiveButton = findViewById(R.id.five_button); sixButton = findViewById(R.id.six_button); plusButton = findViewById(R.id.plus_button); oneButton = findViewById(R.id.one_button); twoButton = findViewById(R.id.two_button); threeButton = findViewById(R.id.three_button); equalsButton = findViewById(R.id.equals_button); zeroButton = findViewById(R.id.zero_button); dotButton = findViewById(R.id.dot_button); // Set OnClickListeners for buttons clearButton.setOnClickListener(this); divideButton.setOnClickListener(this); multiplyButton.setOnClickListener(this); backButton.setOnClickListener(this); sevenButton.setOnClickListener(this); eightButton.setOnClickListener(this); nineButton.setOnClickListener(this); minusButton.setOnClickListener(this); fourButton.setOnClickListener(this); fiveButton.setOnClickListener(this); sixButton.setOnClickListener(this); plusButton.setOnClickListener(this); oneButton.setOnClickListener(this); twoButton.setOnClickListener(this); threeButton.setOnClickListener(this); equalsButton.setOnClickListener(this); zeroButton.setOnClickListener(this); dotButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.clear_button: calculationString.setLength(0); resultTextView.setText(""); break; case R.id.divide_button: appendToCalculationString("/"); break; case R.id.multiply_button: appendToCalculationString("*"); break; case R.id.back_button: if (calculationString.length() > 0) { calculationString.deleteCharAt(calculationString.length() - 1); resultTextView.setText(calculationString.toString()); } break; case R.id.seven_button: appendToCalculationString("7"); break; case R.id.eight_button: appendToCalculationString("8"); break; case R.id.nine_button: appendToCalculationString("9"); break; case R.id.minus_button: appendToCalculationString("-"); break; case R.id.four_button: appendToCalculationString("4"); break; case R.id.five_button: appendToCalculationString("5"); break; case R.id.six_button: appendToCalculationString("6"); break; case R.id.plus_button: appendToCalculationString("+"); break; case R.id.one_button: appendToCalculationString("1"); break; case R.id.two_button: appendToCalculationString("2"); break; case R.id.three_button: appendToCalculationString("3"); break; case R.id.equals_button: evaluateCalculationString(); break; case R.id.zero_button: appendToCalculationString("0"); break; case R.id.dot_button: appendToCalculationString("."); break; } } private void appendToCalculationString(String str) { calculationString.append(str); resultTextView.setText(calculationString.toString()); } private void evaluateCalculationString() { try { double result = eval(calculationString.toString()); resultTextView.setText(Double.toString(result)); calculationString.setLength(0); calculationString.append(Double.toString(result)); } catch (Exception e) { resultTextView.setText("Error"); calculationString.setLength(0); } } public static double eval(final String str) { return new Object() { int pos = -1, ch; void nextChar() { ch = (++pos < str.length()) ? str.charAt(pos) : -1; } boolean skip(char charToSkip) { while (ch == ' ') nextChar(); if (ch == charToSkip) { nextChar(); return true; } return false; } double parse() { nextChar(); double x = parseExpression(); if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch); return x; } double parseExpression() { double x = parseTerm(); for (;;) { if (skip('+')) x += parseTerm(); // addition else if (skip('-')) x -= parseTerm(); // subtraction else return x; } } double parseTerm() { double x = parseFactor(); for (;;) { if (skip('*')) x *= parseFactor(); // multiplication else if (skip('/')) x /= parseFactor(); // division else return x; } } double parseFactor() { if (skip('+')) return parseFactor(); // unary plus if (skip('-')) return -parseFactor(); // unary minus double x; int startPos = this.pos; if (skip('(')) { // parentheses x = parseExpression(); skip(')'); } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers while ((ch >= '0' && ch <= '9') || ch == '.') nextChar(); x = Double.parseDouble(str.substring(startPos, this.pos)); } else { throw new RuntimeException("Unexpected: " + (char)ch); } if (skip('^')) x = Math.pow(x, parseFactor()); // exponentiation return x; } }.parse(); } } ``` activity_main.xml ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/result_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginBottom="16dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:gravity="end" android:textSize="32sp" android:textStyle="bold" android:textColor="@android:color/black" /> <Button android:id="@+id/clear_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="C" android:textSize="24sp" /> <Button android:id="@+id/divide_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="/" android:textSize="24sp" /> <Button android:id="@+id/multiply_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="*" android:textSize="24sp" /> <Button android:id="@+id/back_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="←" android:textSize="24sp" /> <Button android:id="@+id/seven_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="7" android:textSize="24sp" /> <Button android:id="@+id/eight_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="8" android:textSize="24sp" /> <Button android:id="@+id/nine_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="9" android:textSize="24sp" /> <Button android:id="@+id/minus_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="-" android:textSize="24sp" /> <Button android:id="@+id/four_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="4" android:textSize="24sp" /> <Button android:id="@+id/five_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="5" android:textSize="24sp" /> <Button android:id="@+id/six_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="6" android:textSize="24sp" /> <Button android:id="@+id/plus_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="+" android:textSize="24sp" /> <Button android:id="@+id/one_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="1" android:textSize="24sp" /> <Button android:id="@+id/two_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="2" android:textSize="24sp" /> <Button android:id="@+id/three_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="3" android:textSize="24sp" /> <Button android:id="@+id/equals_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="=" android:textSize="24sp" /> <Button android:id="@+id/zero_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="2" android:text="0" android:textSize="24sp" /> <Button android:id="@+id/dot_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="." android:textSize="24sp" /> <View android:id="@+id/view1" android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/colorPrimaryDark" android:layout_below="@+id/result_text_view" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="16dp" android:layout_marginBottom="16dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_below="@+id/view1" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="16dp" android:layout_marginBottom="16dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/sin_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="sin" android:textSize="24sp" /> <Button android:id="@+id/cos_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="cos" android:textSize="24sp" /> <Button android:id="@+id/tan_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="tan" android:textSize="24sp" /> <Button android:id="@+id/ln_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="ln" android:textSize="24sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/sqrt_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="√" android:textSize="24sp" /> <Button android:id="@+id/cbrt_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="3√" android:textSize="24sp" /> <Button android:id="@+id/x2_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="x²" android:textSize="24sp" /> <Button android:id="@+id/x3_button" android:layout_width="0dp" android:layout_height="64dp" android:layout_margin="8dp" android:layout_weight="1" android:text="x³" android:textSize="24sp" /> </LinearLayout> </LinearLayout> </RelativeLayout> ``` 这个计算器还支持一些科学计算功能,例如 sin、cos、tan、ln、平方根、立方根等。

相关推荐

最新推荐

recommend-type

基于go开发的waf,包括网关和WAF两部分;.zip

Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
recommend-type

基于mgo风格封装mongodb-go官方驱动.zip

Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB取整函数与数据分析的应用:round、fix、floor、ceil在数据分析中的应用

![MATLAB取整函数与数据分析的应用:round、fix、floor、ceil在数据分析中的应用](https://img-blog.csdnimg.cn/img_convert/a12c695f8b68033fc45008ede036b653.png) # 1. MATLAB取整函数概述 MATLAB提供了一系列取整函数,用于对数值进行四舍五入或截断操作。这些函数包括: - `round`:四舍五入到最接近的整数 - `floor`:向下取整到最小的整数 - `ceil`:向上取整到最大的整数 - `fix`:截断小数部分,返回整数部分 这些函数在数据分析、可视化和特定领域(如金融
recommend-type

r语言如何调用split函数按照factor分开后得到的dataframe

可以使用如下语句: split_dataframe <- split(original_dataframe, original_dataframe$factor_column) 其中,original_dataframe为待拆分的原始数据框,$factor_column为按照哪一列分组(即因子列),split_dataframe为拆分后得到的数据框列表。