All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.skillw.pouvoir.util.calculate.CalcOperator.kt Maven / Gradle / Ivy

There is a newer version: 1.6.7-beta-6
Show newest version
package com.skillw.pouvoir.util.calculate

import kotlin.math.pow

/**
 * @className A
 *
 * @author Glom
 * @date 2023/1/16 3:33 Copyright 2024 Glom.
 */
internal enum class CalcOperator(
    private val symbol: Char,
    val priority: Int,
    val calc: (Double, Double) -> Double = { _, _ -> 0.0 },
) {
    PLUS('+', 1, { a, b -> a + b }),
    MINUS('-', 1, { a, b -> b - a }),

    MULTIPLY('*', 2, { a, b -> a * b }),

    DIVIDE('/', 2, { a, b -> b / a }),

    REMAIN('%', 2, { a, b -> b % a }),

    POWER('^', 3, { a, b -> b.pow(a) }),
    LEFT_BRACKET('(', 3),

    RIGHT_BRACKET(')', 3);

    override fun toString(): String {
        return symbol.toString()
    }

    companion object {
        private val symbols = CalcOperator.values().map { it.symbol }.toHashSet()
        fun Char.isCalcOperator(): Boolean = this in symbols
        fun Char.toCalcOperator(): CalcOperator {
            return when (this) {
                '+' -> PLUS
                '-' -> MINUS
                '*' -> MULTIPLY
                '/' -> DIVIDE
                '%' -> REMAIN
                '^' -> POWER
                '(' -> LEFT_BRACKET
                ')' -> RIGHT_BRACKET
                else -> error("No such Operator $this")
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy