|
1 | 1 | package evil |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "unicode" |
| 8 | +) |
| 9 | + |
3 | 10 | var SepFunc = func(b byte) bool { |
4 | 11 | if 48 <= b && b <= 57 { |
5 | 12 | return false |
@@ -42,3 +49,191 @@ func Split(data []byte, sep func(b byte) bool, handler func([]byte) bool) { |
42 | 49 | } |
43 | 50 | return |
44 | 51 | } |
| 52 | + |
| 53 | +func calculate(expression string) (interface{}, error) { |
| 54 | + // 去除多余空格 |
| 55 | + expression = strings.TrimSpace(expression) |
| 56 | + expression = strings.ReplaceAll(expression, " ", "") |
| 57 | + |
| 58 | + // 定义运算符优先级 |
| 59 | + precedence := map[rune]int{ |
| 60 | + '+': 1, |
| 61 | + '-': 1, |
| 62 | + '*': 2, |
| 63 | + '/': 2, |
| 64 | + } |
| 65 | + |
| 66 | + // 定义两个栈,一个存操作数,一个存操作符 |
| 67 | + var numStack []interface{} |
| 68 | + var opStack []rune |
| 69 | + |
| 70 | + // 辅助函数,用于执行整数计算 |
| 71 | + applyOpInt := func(op rune, b, a *big.Int) *big.Int { |
| 72 | + result := new(big.Int) |
| 73 | + switch op { |
| 74 | + case '+': |
| 75 | + return result.Add(a, b) |
| 76 | + case '-': |
| 77 | + return result.Sub(a, b) |
| 78 | + case '*': |
| 79 | + return result.Mul(a, b) |
| 80 | + case '/': |
| 81 | + return result.Div(a, b) |
| 82 | + default: |
| 83 | + return big.NewInt(0) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // 辅助函数,用于执行浮点数计算 |
| 88 | + applyOpFloat := func(op rune, b, a *big.Float) *big.Float { |
| 89 | + result := new(big.Float) |
| 90 | + switch op { |
| 91 | + case '+': |
| 92 | + return result.Add(a, b) |
| 93 | + case '-': |
| 94 | + return result.Sub(a, b) |
| 95 | + case '*': |
| 96 | + return result.Mul(a, b) |
| 97 | + case '/': |
| 98 | + return result.Quo(a, b) |
| 99 | + default: |
| 100 | + return big.NewFloat(0) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // 判断字符串是否为整数 |
| 105 | + isInteger := func(s string) bool { |
| 106 | + _, err := strconv.ParseInt(s, 10, 64) |
| 107 | + return err == nil |
| 108 | + } |
| 109 | + |
| 110 | + // 将操作数转换为浮点数 |
| 111 | + toBigFloat := func(num interface{}) *big.Float { |
| 112 | + switch v := num.(type) { |
| 113 | + case *big.Int: |
| 114 | + return new(big.Float).SetInt(v) |
| 115 | + case *big.Float: |
| 116 | + return v |
| 117 | + default: |
| 118 | + return big.NewFloat(0) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // 解析表达式并进行计算 |
| 123 | + i := 0 |
| 124 | + for i < len(expression) { |
| 125 | + char := rune(expression[i]) |
| 126 | + |
| 127 | + if unicode.IsDigit(char) || char == '.' { |
| 128 | + // 处理数字和小数点 |
| 129 | + j := i |
| 130 | + for j < len(expression) && (unicode.IsDigit(rune(expression[j])) || rune(expression[j]) == '.') { |
| 131 | + j++ |
| 132 | + } |
| 133 | + numStr := expression[i:j] |
| 134 | + if isInteger(numStr) { |
| 135 | + num := new(big.Int) |
| 136 | + num, ok := num.SetString(numStr, 10) |
| 137 | + if !ok { |
| 138 | + return big.NewFloat(0), nil |
| 139 | + } |
| 140 | + numStack = append(numStack, num) |
| 141 | + } else { |
| 142 | + num, ok := new(big.Float).SetString(numStr) |
| 143 | + if !ok { |
| 144 | + return big.NewFloat(0), nil |
| 145 | + } |
| 146 | + numStack = append(numStack, num) |
| 147 | + } |
| 148 | + i = j |
| 149 | + } else if char == '(' { |
| 150 | + // 处理左括号 |
| 151 | + opStack = append(opStack, char) |
| 152 | + i++ |
| 153 | + } else if char == ')' { |
| 154 | + // 处理右括号 |
| 155 | + for len(opStack) > 0 && opStack[len(opStack)-1] != '(' { |
| 156 | + op := opStack[len(opStack)-1] |
| 157 | + opStack = opStack[:len(opStack)-1] |
| 158 | + b := numStack[len(numStack)-1] |
| 159 | + numStack = numStack[:len(numStack)-1] |
| 160 | + a := numStack[len(numStack)-1] |
| 161 | + numStack = numStack[:len(numStack)-1] |
| 162 | + |
| 163 | + if aInt, okA := a.(*big.Int); okA { |
| 164 | + if bInt, okB := b.(*big.Int); okB { |
| 165 | + result := applyOpInt(op, bInt, aInt) |
| 166 | + numStack = append(numStack, result) |
| 167 | + } else { |
| 168 | + result := applyOpFloat(op, toBigFloat(b), toBigFloat(a)) |
| 169 | + numStack = append(numStack, result) |
| 170 | + } |
| 171 | + } else { |
| 172 | + result := applyOpFloat(op, toBigFloat(b), toBigFloat(a)) |
| 173 | + numStack = append(numStack, result) |
| 174 | + } |
| 175 | + } |
| 176 | + // 移除左括号 |
| 177 | + if len(opStack) > 0 { |
| 178 | + opStack = opStack[:len(opStack)-1] |
| 179 | + } |
| 180 | + i++ |
| 181 | + } else if strings.ContainsRune("+-*/", char) { |
| 182 | + // 处理运算符 |
| 183 | + for len(opStack) > 0 && precedence[char] <= precedence[opStack[len(opStack)-1]] { |
| 184 | + op := opStack[len(opStack)-1] |
| 185 | + opStack = opStack[:len(opStack)-1] |
| 186 | + b := numStack[len(numStack)-1] |
| 187 | + numStack = numStack[:len(numStack)-1] |
| 188 | + a := numStack[len(numStack)-1] |
| 189 | + numStack = numStack[:len(numStack)-1] |
| 190 | + |
| 191 | + if aInt, okA := a.(*big.Int); okA { |
| 192 | + if bInt, okB := b.(*big.Int); okB { |
| 193 | + result := applyOpInt(op, bInt, aInt) |
| 194 | + numStack = append(numStack, result) |
| 195 | + } else { |
| 196 | + result := applyOpFloat(op, toBigFloat(b), toBigFloat(a)) |
| 197 | + numStack = append(numStack, result) |
| 198 | + } |
| 199 | + } else { |
| 200 | + result := applyOpFloat(op, toBigFloat(b), toBigFloat(a)) |
| 201 | + numStack = append(numStack, result) |
| 202 | + } |
| 203 | + } |
| 204 | + opStack = append(opStack, char) |
| 205 | + i++ |
| 206 | + } else { |
| 207 | + return big.NewFloat(0), nil |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + // 处理剩余的运算符 |
| 212 | + for len(opStack) > 0 { |
| 213 | + op := opStack[len(opStack)-1] |
| 214 | + opStack = opStack[:len(opStack)-1] |
| 215 | + b := numStack[len(numStack)-1] |
| 216 | + numStack = numStack[:len(numStack)-1] |
| 217 | + a := numStack[len(numStack)-1] |
| 218 | + numStack = numStack[:len(numStack)-1] |
| 219 | + |
| 220 | + if aInt, okA := a.(*big.Int); okA { |
| 221 | + if bInt, okB := b.(*big.Int); okB { |
| 222 | + result := applyOpInt(op, bInt, aInt) |
| 223 | + numStack = append(numStack, result) |
| 224 | + } else { |
| 225 | + result := applyOpFloat(op, toBigFloat(b), toBigFloat(a)) |
| 226 | + numStack = append(numStack, result) |
| 227 | + } |
| 228 | + } else { |
| 229 | + result := applyOpFloat(op, toBigFloat(b), toBigFloat(a)) |
| 230 | + numStack = append(numStack, result) |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if len(numStack) != 1 { |
| 235 | + return big.NewFloat(0), nil |
| 236 | + } |
| 237 | + |
| 238 | + return numStack[0], nil |
| 239 | +} |
0 commit comments