2101B大数据
UI:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="end"
android:padding="8dp"
android:text=""
android:textSize="24sp" />
<!--网格布局-->
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/display"
android:columnCount="4"
android:orientation="horizontal">
<!--Grid 属性-->
<Button
android:id="@+id/btnClear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="C" />
<Button
android:id="@+id/btnLeftBracket"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="(" />
<Button
android:id="@+id/btnRightBracket"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text=")" />
<Button
android:id="@+id/btnDivide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="/" />
<Button
android:id="@+id/btn7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="7" />
<Button
android:id="@+id/btn8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="8" />
<Button
android:id="@+id/btn9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="9" />
<Button
android:id="@+id/btnMultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="*" />
<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="4" />
<Button
android:id="@+id/btn5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="5" />
<Button
android:id="@+id/btn6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="6" />
<Button
android:id="@+id/btnSubtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="-" />
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="3" />
<Button
android:id="@+id/btnAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="+" />
<Button
android:id="@+id/btn0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnSpan="2"
android:layout_columnWeight="2"
android:text="0" />
<Button
android:id="@+id/btnDecimal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:text="." />
<Button
android:id="@+id/btnEquals"
android:layout_width="0dp"
android:layout_height="wrap_contenAt"
android:layout_columnWeight="1"
android:text="=" />
</GridLayout>
</RelativeLayout>
MainActivity:
package com.example.myapplication
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class MainActivity : AppCompatActivity() {
private lateinit var display: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
display = findViewById(R.id.display)
val buttons = listOf<Button>(
findViewById(R.id.btn0),
findViewById(R.id.btn1),
findViewById(R.id.btn2),
findViewById(R.id.btn3),
findViewById(R.id.btn4),
findViewById(R.id.btn5),
findViewById(R.id.btn6),
findViewById(R.id.btn7),
findViewById(R.id.btn8),
findViewById(R.id.btn9),
findViewById(R.id.btnAdd),
findViewById(R.id.btnSubtract),
findViewById(R.id.btnMultiply),
findViewById(R.id.btnDivide),
findViewById(R.id.btnClear),
findViewById(R.id.btnEquals),
findViewById(R.id.btnLeftBracket),
findViewById(R.id.btnRightBracket)
)
for (button in buttons) {
button.setOnClickListener {
val buttonText = button.text.toString()
val currentDisplay = display.text.toString()
if (buttonText == "C") {
display.text = ""
} else if (buttonText == "=") {
try {
val result = main(currentDisplay)
display.text = result.toString()
} catch (e: Exception) {
display.text = "Error"
}
} else {
display.text = currentDisplay + buttonText
}
}
}
}
private fun main(expression: String):Int{
// val expression = "3 + (4 * 2)"
val result = evaluateExpression(expression)
// println("Result: $result")
return result
}
fun evaluateExpression(expression: String): Int {
val postfixExpression = convertToPostfix(expression)
return evaluatePostfix(postfixExpression)
}
fun convertToPostfix(expression: String): String {
// 存放操作符
val operatorStack = Stack<Char>()
val postfix = StringBuilder()
// 存放数字
var numberBuffer = StringBuilder()
for (token in expression) {
if (token.isDigit()) {
numberBuffer.append(token)
} else {
if (numberBuffer.isNotEmpty()) {
postfix.append(numberBuffer).append(" ")
numberBuffer = StringBuilder()
}
if (token == '(') {
operatorStack.push(token)
} else if (token == ')') {
while (operatorStack.peek() != '(') {
postfix.append(operatorStack.pop()).append(" ")
}
operatorStack.pop() // Discard the '('
} else {
while (operatorStack.isNotEmpty() && precedence(operatorStack.peek()) >= precedence(token)) {
postfix.append(operatorStack.pop()).append(" ")
}
operatorStack.push(token)
}
}
}
// 处理最后一个数字 因为它后边可能没有括号,所有导致它没有进入 postfix
if (numberBuffer.isNotEmpty()) {
postfix.append(numberBuffer).append(" ")
}
// 处理最后几个(》=1个)字符 因为它后边可能没有括号,所有导致它没有进入 postfix
while (operatorStack.isNotEmpty()) {
postfix.append(operatorStack.pop()).append(" ")
}
//TRIM函数主要用于把单元格内容前后的空格去掉,但并不去除字符之间的空格
return postfix.toString().trim()
}
//计算
fun evaluatePostfix(postfix: String): Int {
val operandStack = Stack<Int>()
for (token in postfix.split(" ")) {
if (token.isNumeric()) {
operandStack.push(token.toInt())
} else {
val operand2 = operandStack.pop()
val operand1 = operandStack.pop()
val result = when (token) {
"+" -> operand1 + operand2
"-" -> operand1 - operand2
"*" -> operand1 * operand2
"/" -> operand1 / operand2
else -> throw IllegalArgumentException("Unknown operator: $token")
}
operandStack.push(result)
}
}
return operandStack.pop()
}
//判断优先级
fun precedence(operator: Char): Int {
return when (operator) {
'+', '-' -> 1
'*', '/' -> 2
else -> 0
}
}
//将字符类型数字转化成数字类型 方便计算
fun String.isNumeric(): Boolean {
return try {
this.toInt()
true
} catch (e: NumberFormatException) {
false
}
}
}