package com.example.calculatorddaa;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Stack;
public class MainActivity extends AppCompatActivity {
//变量定义
EditText editText; //输入框:用于输入数字
TextView textView, textView_Now, textView_M; //文本框:显示计算过程,第二个显示当前结果,第三个个显示是否存在记忆。
HorizontalScrollView horizonLayout_Last, horizonLayout_Now;
Button button_rotate; // 按钮,旋转
Button[] number_buttons = new Button[10]; // 按钮:十个数字
Button button_add, button_sub, button_mul, button_div; // 按钮:加减乘除
Button button_point, button_equal_To, button_C, button_CE; // 按钮:小数点,等号,清空,退格
Button button_MR, button_Mup, button_Mdown, button_MC; // 按钮:读取记忆,记忆加,记忆减,记忆删除
//下面是横屏按钮
Button button_left_parenthesis, button_right_parenthesis;// 两个括号
Button button_e, button_pi;//两个特殊变量
Button button_reciprocal, button_square, button_cubic_power, button_n_power;// 倒数,二次方,三次方,n次方,
Button button_square_root, button_cube_root, button_n_root;// 平方根,立方根,n次根
Button button_inv;// 切换一元运算符的按钮(反函数)
Button button_ln, button_log;//ln,log
Button button_sin, button_cos, button_tan;// 三角函数们
String str_t_digit = "", str_t_equation = "", str_t_Now = ""; // 临时字符串,临时保存输入的数字和表达式和上次结果信息
Double digit_t = 0.0; // 临时变量
Integer parenthesis_n = 0; //未匹配的左括号数目,包含(,^(,三角函数(,ln(,log(
Boolean mark_inv = true;// 标记变量,标记inv的状态 true正,false反
ArrayList<Double> input_Double = new ArrayList<>(); // 储存用户输入的数字,用可变数组储存
ArrayList<String> input_String = new ArrayList<>(); // 储存用户输入的运算符,用可变数组储存
ArrayList<Boolean> input_Boolean = new ArrayList<>(); // 储存用户输入是否是数字,用可变数组储存,
//直接入栈的字符串
ArrayList<String> special_symbols = new ArrayList<String>();
Double memory = 0.0; //记忆
NumberFormat nf = NumberFormat.getInstance();//Double转String
private final View.OnClickListener land_switch_onClickListener = new View.OnClickListener() {//横竖屏切换侦听器
@Override
public void onClick(View view) {//点击事件
try {
// 获取当前屏幕的方向
int currentOrientation = getResources().getConfiguration().orientation;
// 根据当前屏幕方向切换屏幕方向
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} catch (Exception e) {
Log.e("land_switch", "error");
}
}
};
private final View.OnClickListener digit_onClickListener = new View.OnClickListener() {//数字侦听器,数字+小数点
@Override
public void onClick(View view) {//点击事件
Button button = (Button) view; //把点击获得的id信息传递给button
try {
if (!str_t_equation.equals("") && str_t_equation.charAt(str_t_equation.length() - 1) == ')') {//前面是)不让输入数字
return;
} else if (button.getId() == R.id.button_0) {//如果点击了按钮:“0”
if (!(str_t_digit.equals("0"))) { //之前不是只有一个0,才添0,防止000这样情况出现
str_t_digit = str_t_digit + 0;
}
} else if (button.getId() == R.id.button_point) {
if (str_t_digit.contains(".")) { //判断字符串中是否已包含小数点,如果有,就什么也不做
return;
} else { //如果没有小数点
if (str_t_digit.equals("")) {//如果初时显示为空,就替换为"0."
str_t_digit = "0.";
} else {
str_t_digit = str_t_digit + ".";
}
}
} else { // 现在如果只有一个0,就删除0,
if (str_t_digit.equals("0")) {
str_t_digit = "";
}//接下来是1~9的按钮
if (button.getId() == R.id.button_1) {//如果点击了按钮:“1”
str_t_digit = str_t_digit + 1;//变量末尾,添加一个“1”
} else if (button.getId() == R.id.button_2) {//以此类推到9
str_t_digit = str_t_digit + 2;
} else if (button.getId() == R.id.button_3) {
str_t_digit = str_t_digit + 3;
} else if (button.getId() == R.id.button_4) {
str_t_digit = str_t_digit + 4;
} else if (button.getId() == R.id.button_5) {
str_t_digit = str_t_digit + 5;
} else if (button.getId() == R.id.button_6) {
str_t_digit = str_t_digit + 6;
} else if (button.getId() == R.id.button_7) {
str_t_digit = str_t_digit + 7;
} else if (button.getId() == R.id.button_8) {
str_t_digit = str_t_digit + 8;
} else if (button.getId() == R.id.button_9) {
str_t_digit = str_t_digit + 9;
} else {
Log.e("digit", "error");
return;
}
}
editText.setText(str_t_digit);
editText.setSelection(editText.getText().length());
} catch (Exception e) {
Log.e("digit", "error");
}
}
};
private final View.OnClickListener operator_onClickListener = new View.OnClickListener() {//常用运算符侦听器,
@Override
public void onClick(View view) {//点击事件
Button button = (Button) view; //把点击获得的id信息传递给button
try {
//+,-,*,÷逻辑一样,除零之类的运算时再判断
if (!str_t_digit.equals("")) {//有数字
digit_t = Double.parseDouble(str_t_digit);// 字符串转数字
input_Double.add(digit_t);// 入列
//数字添加进表达式字符串
str_t_equation += str_t_digit;
str_t_digit = ""; //删除之前的数字字符串
input_Boolean.add(true);// 入列了一个数字
// 根据按钮入列运算符------------------------------
} else {// 如果输入数字为空,多种情况,
// 前面什么都没有,但是textView_Now不空
if (str_t_equation.equals("") && !str_t_Now.equals("") && !str_t_Now.equals("错误")) {
//将textView_Now内容当数字进来
digit_t = Double.parseDouble(s
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
calculatorddaa.zip (57个子文件)
calculatorddaa
gradle.properties 1KB
gradle
wrapper
gradle-wrapper.jar 58KB
gradle-wrapper.properties 236B
app
src
androidTest
java
com
example
calculatorddaa
ExampleInstrumentedTest.java 766B
test
java
com
example
calculatorddaa
ExampleUnitTest.java 387B
main
java
com
example
calculatorddaa
MainActivity.java 57KB
res
mipmap-xxhdpi
ic_launcher_foreground.webp 3KB
ic_launcher_background.webp 2KB
ic_launcher_round.webp 6KB
ic_launcher.webp 2KB
mipmap-hdpi
ic_launcher_foreground.webp 1KB
ic_launcher_background.webp 1000B
ic_launcher_round.webp 2KB
ic_launcher.webp 1KB
layout-land
activity_main.xml 26KB
mipmap-anydpi-v26
ic_launcher.xml 269B
ic_launcher_round.xml 269B
values-night
styles.xml 3KB
themes.xml 434B
mipmap-mdpi
ic_launcher_foreground.webp 824B
ic_launcher_background.webp 634B
ic_launcher_round.webp 1KB
ic_launcher.webp 868B
mipmap-xxxhdpi
ic_launcher_foreground.webp 4KB
ic_launcher_background.webp 3KB
ic_launcher_round.webp 8KB
ic_launcher.webp 3KB
mipmap-anydpi
layout-sw600dp
mipmap-xhdpi
ic_launcher_foreground.webp 2KB
ic_launcher_background.webp 1KB
ic_launcher_round.webp 4KB
ic_launcher.webp 1KB
xml
data_extraction_rules.xml 551B
backup_rules.xml 478B
values
ic_launcher_background.xml 123B
colors.xml 686B
strings.xml 2KB
styles.xml 3KB
themes.xml 435B
layout
activity_main.xml 20KB
drawable
ic_launcher_background.xml 5KB
blackbutton.xml 1013B
orangebuttons.xml 992B
ic_launcher_foreground.xml 440B
calculator.png 11KB
greybutton.xml 992B
whitebutton.xml 952B
bluebuttons.xml 988B
ic_launcher-playstore.png 21KB
AndroidManifest.xml 965B
proguard-rules.pro 750B
libs
.gitignore 6B
build.gradle.kts 1KB
gradlew.bat 3KB
gradlew 6KB
.gitignore 225B
settings.gradle.kts 334B
build.gradle.kts 173B
共 57 条
- 1
资源评论
i道i
- 粉丝: 696
- 资源: 39
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_2024-05-21-17-06-42-64_2332cb9b27b851b548ba47a91682926c.jpg
- 毕业设计参考 - 基于树莓派、OpenCV及Python的人脸识别
- node-v18.20.2-linux-arm64
- 222222222222
- 16张相机标定图片,可复现本文畸变矫正
- dbeaver-ce-23.3.1-x86_64-setup.zip
- 基于X86 AVX2指令的快速卷积实现
- VMware-ESXi-7.0U3n-21930508-depot.zip文件
- MySQL 在 Windows 系统下的安装教程.zip
- Access文件数据库访问的客户端
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功