Пример #1
0
            internal void Append(SymbolAndPower symbolAndPower, SymbolFormat symbolFormat)
            {
                this.Append(symbolAndPower.Symbol);
                if (symbolAndPower.Power == 1)
                {
                    return;
                }

                switch (symbolFormat)
                {
                    case SymbolFormat.SignedHatPowers:
                        this.Append('^');
                        this.Append(symbolAndPower.Power.ToString(CultureInfo.InvariantCulture));
                        break;
                    case SymbolFormat.FractionHatPowers:
                        {
                            var power = Math.Abs(symbolAndPower.Power);
                            if (power == 1)
                            {
                                return;
                            }

                            this.Append('^');
                            this.Append(power.ToString(CultureInfo.InvariantCulture));
                            break;
                        }

                    case SymbolFormat.SignedSuperScript:
                        {
                            if (symbolAndPower.Power == 1)
                            {
                                return;
                            }

                            if (symbolAndPower.Power < 0)
                            {
                                this.Append(SuperScript.Minus);
                            }

                            this.Append(SuperScript.GetChar(Math.Abs(symbolAndPower.Power)));
                            break;
                        }

                    case SymbolFormat.Default:
                    case SymbolFormat.FractionSuperScript:
                        {
                            var power = Math.Abs(symbolAndPower.Power);
                            if (power == 1)
                            {
                                return;
                            }

                            this.Append(SuperScript.GetChar(Math.Abs(symbolAndPower.Power)));
                            break;
                        }

                    default:
                        throw new ArgumentOutOfRangeException(nameof(symbolFormat), symbolFormat, null);
                }
            }
Пример #2
0
        internal static bool TryRead(string text, ref int pos, out SymbolAndPower result)
        {
            if (pos == text.Length)
            {
                result = default(SymbolAndPower);
                return(false);
            }

            var start = pos;

            while (text.Length > pos && IsSymbol(text[pos]))
            {
                pos++;
            }

            if (pos == start)
            {
                result = default(SymbolAndPower);
                return(false);
            }

            var symbol = text.Substring(start, pos - start);

            WhiteSpaceReader.TryRead(text, ref pos);
            int power;

            if (!PowerReader.TryRead(text, ref pos, out power))
            {
                pos    = start;
                result = default(SymbolAndPower);
                return(false);
            }

            if (power == 0 || Math.Abs(power) > 5) // 5 > is most likely a typo right?
            {
                pos    = start;
                result = default(SymbolAndPower);
                return(false);
            }

            result = new SymbolAndPower(symbol, power);
            return(true);
        }
Пример #3
0
        internal static bool TryRead(string text, ref int pos, out IReadOnlyList <SymbolAndPower> result)
        {
            var start = pos;
            var sign  = Sign.Positive;
            List <SymbolAndPower> saps = null;

            while (pos < text.Length)
            {
                WhiteSpaceReader.TryRead(text, ref pos);
                if (!TryRead(text, ref pos, out SymbolAndPower sap))
                {
                    pos    = start;
                    result = null;
                    return(false);
                }

                if (sap.Power < 0 &&
                    sign == Sign.Negative)
                {
                    pos    = start;
                    result = null;
                    return(false);
                }

                if (sign == Sign.Negative)
                {
                    sap = new SymbolAndPower(sap.Symbol, -1 * sap.Power);
                }

                if (saps == null)
                {
                    saps = new List <SymbolAndPower>();
                }

                saps.Add(sap);

                var op = OperatorReader.TryReadMultiplyOrDivide(text, ref pos);
                if (op != MultiplyOrDivide.None)
                {
                    WhiteSpaceReader.TryRead(text, ref pos);
                    if (OperatorReader.TryReadMultiplyOrDivide(text, ref pos) != MultiplyOrDivide.None)
                    {
                        pos    = start;
                        result = null;
                        return(false);
                    }

                    if (op == MultiplyOrDivide.Division)
                    {
                        if (sign == Sign.Negative)
                        {
                            pos    = start;
                            result = null;
                            return(false);
                        }

                        sign = Sign.Negative;
                    }
                }
            }

            if (saps == null || !IsUnique(saps))
            {
                result = null;
                return(false);
            }

            result = saps;
            return(true);
        }