文章

计算字符串运算!

心血来潮想重构JsonLang处理器,看到蹭写的答辩计算器,决定重构。 使用方法迭代来处理优先运算,使用统一加法运算来处理每个运算节点。

废话不多,上码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package club.someoneice.jsonprocessor.multiple

object CalculatorV2 {
    fun calculator(str: String): Double {
        val newStr = str.replace(" ", "")

        val steps: ArrayList<Double> = ArrayList()
        val numberBuffer = StringBuilder()
        val advancedBuffer = StringBuilder()

        var isArray = false

        fun checkCalculator(c: Char) {
            if (advancedBuffer.isEmpty()) {
                if (numberBuffer.isEmpty()) throw CalculatorException("Unexpected symbols: $c in formula $str")
                steps.add(numberBuffer.toString().toDouble())
                numberBuffer.clear()
                return
            }

            val symbolSuf = advancedBuffer[advancedBuffer.length - 1]
            val symbolPre = advancedBuffer[0]
            var strReplace = advancedBuffer.removeSuffix(symbolSuf.toString()).toString()
            if (symbolPre == '-') strReplace = strReplace.removePrefix(symbolPre.toString())
            val numberA = strReplace.toDouble()
            val numberB = numberBuffer.toString().toDouble()
            var callback: Double = when (symbolSuf) {
                '*' -> numberA * numberB
                '/' -> numberA / numberB
                else -> throw CalculatorException("Unexpected symbols: $c in formula $str")
            }
            numberBuffer.clear()
            advancedBuffer.clear()
            if (symbolPre == '-') callback *= -1
            if (c == '*' || c == '/') advancedBuffer.append(callback).append(c)
            else steps.add(callback)
        }

        fun subtractHandler(c: Char) {
            if (numberBuffer.isNotEmpty()) checkCalculator(c)
            numberBuffer.append(c)
        }

        fun advancedHandler(c: Char) {
            if (advancedBuffer.isEmpty()) {
                advancedBuffer.append(numberBuffer.toString()).append(c)
                numberBuffer.clear()
            }
            else checkCalculator(c)
        }

        fun handlerArray(c: Char) {
            if (c == ')') {
                val start = numberBuffer.length - numberBuffer.toString().replace("(","").length
                val end = numberBuffer.length - numberBuffer.toString().replace(")","").length
                val count = start - end
                if (count < 0) throw CalculatorException("Unexpected symbols: $c in formula $str")
                if (count == 0) {
                    val strArray = numberBuffer.toString();
                    numberBuffer.clear().append(calculator(strArray))
                    isArray = false
                } else numberBuffer.append(c)
            } else numberBuffer.append(c)
        }

        fun startArray(c: Char) {
            if (numberBuffer.isNotEmpty()) advancedHandler(c)
            isArray = true
        }

        for (c in newStr.indices) {
            val char = newStr[c]

            if (isArray) handlerArray(char)
            else when (char) {
                '(' -> startArray(char)
                '*', '/' -> advancedHandler(char)
                '+' -> checkCalculator(char)
                '-' -> subtractHandler(char)
                else -> numberBuffer.append(char)
            }
        }
        checkCalculator('=')
        var number = 0.0
        steps.forEach {
            number += it
        }
        return number
    }

    class CalculatorException(cause: String): Exception(cause)
}
本文章以 CC BY 4.0 授權