XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Collections.ObjectModel;

namespace XFEToolBox.QuickTools;

public sealed record CalculationRecord(string Expression, string Result) { public string Display => Expression + " = " + Result; }

/// <summary>Keypad state is separate from WPF, so pointer and keyboard input share tested semantics.</summary>
public sealed class CalculatorPad
{
    public string Expression { get; private set; } = "0";
    public string Display { get; private set; } = "0";
    public ObservableCollection<CalculationRecord> History { get; } = [];
    private bool evaluated;
    public void SetExpression(string value) { Expression = value; evaluated = false; }
    public void Press(string key)
    {
        if (key == "C") { Expression = Display = "0"; evaluated = false; return; }
        if (key == "=")
        {
            string result = Calculator.Calculate(Expression);
            History.Insert(0, new(Expression, result)); while (History.Count > 30) History.RemoveAt(History.Count - 1);
            Display = result; evaluated = true; return;
        }
        if (key is "1/x" or "x²" or "√" or "+/−" or "%")
        {
            string value = evaluated ? Display : Expression;
            Expression = key switch { "1/x" => $"1/({value})", "x²" => $"({value})^2", "√" => $"sqrt({value})", "+/−" => $"-({value})", _ => $"({value})/100" };
            evaluated = false; Press("="); return;
        }
        if (key == "CE")
        {
            if (evaluated) { Press("C"); return; }
            int start = Expression.Length;
            while (start > 0 && (char.IsDigit(Expression[start - 1]) || Expression[start - 1] == '.')) start--;
            Expression = start == Expression.Length ? "0" : Expression[..start] + "0"; Display = "0"; return;
        }
        if (key == "⌫")
        { Expression = evaluated ? Display : Expression; evaluated = false; Expression = Expression.Length > 1 ? Expression[..^1] : "0"; Display = Expression; return; }
        string token = key == "−" ? "-" : key;
        bool op = token is "+" or "-" or "×" or "÷" or "^";
        if (evaluated) { Expression = op ? Display : "0"; evaluated = false; }
        if (op)
        {
            if (Expression.Length == 0) Expression = "0";
            if ("+-×÷*/^".Contains(Expression[^1])) Expression = Expression[..^1];
            Expression += token;
        }
        else
        {
            if (token == ".")
            {
                string current = new(Expression.Reverse().TakeWhile(c => char.IsDigit(c) || c == '.').Reverse().ToArray());
                if (current.Contains('.')) return;
                if (current.Length == 0) token = "0.";
            }
            Expression = Expression == "0" && token != "." ? token : Expression + token;
            Display = Expression;
        }
        if (Expression.Length > 2048) { Expression = Expression[..2048]; throw new FormatException("表达式已达到长度上限。"); }
    }
}