Pith - wedge_android
wedge_android/app/src/main/java/com/vgmlr/wedge/WedgeCalculator.kt [2.1 kb]
Modified: 23:08:23 55 026 (13 May 026)
17 Days Ago
package com.vgmlr.wedge

import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Locale

object WedgeCalculator {
    fun doCalc(expr: String): String {
        return try {
            val res = object {
                var pos = -1
                var ch = 0
                fun next() { ch = if (++pos < expr.length) expr[pos].code else -1 }
                fun eat(c: Int): Boolean {
                    while (ch == ' '.code) next()
                    if (ch == c) { next(); while (ch == ' '.code) next(); return true }
                    return false
                }
                fun parse(): Double { next(); val x = pExp(); if (pos < expr.length) throw Exception(); return x }
                fun pExp(): Double {
                    var x = pTerm()
                    while (true) {
                        if (eat('+'.code)) { val y = pTerm(); x += if (pos > 0 && expr[pos - 1] == '%') x * y else y }
                        else if (eat('-'.code)) { val y = pTerm(); x -= if (pos > 0 && expr[pos - 1] == '%') x * y else y }
                        else return x
                    }
                }
                fun pTerm(): Double {
                    var x = pFact()
                    while (true) { if (eat('*'.code)) x *= pFact() else if (eat('/'.code)) x /= pFact() else return x }
                }
                fun pFact(): Double {
                    if (eat('+'.code)) return pFact()
                    if (eat('-'.code)) return -pFact()
                    var x: Double
                    val start = pos
                    if (eat('('.code)) { x = pExp(); eat(')'.code) }
                    else if (ch in '0'.code..'9'.code || ch == '.'.code) {
                        while (ch in '0'.code..'9'.code || ch == '.'.code) next()
                        x = expr.substring(start, pos).toDouble()
                    } else throw Exception()
                    if (eat('%'.code)) x /= 100.0
                    return x
                }
            }.parse()
            DecimalFormat("#.#######", DecimalFormatSymbols(Locale.US)).format(res)
        } catch (_: Exception) { "Error" }
    }
}
Updates
Shim - Android 70.026.1
Wedge - Linux 68.026.1
Wedge - Android 68.026.1
Taper - Linux 64.026.1
Ayh Extension - Chrome 63.026.1
Dev
TVShow (227) 'CSA'
TVShow (228) 'APT'
TVProgram (83) 'BXT'
Miter Update(s)
Shim (Dictation)

Menu
Calendar
Project Tin (024/029)
Miter
RSS Feed
User Avatar
@vgmlr
=SUM(parts)