Beispiel #1
0
        /// <summary>
        /// LaTex的な数式でBackSpaceしたいときの関数
        /// </summary>
        /// <param name="NowFormula">LaTeX的な関数</param>
        /// <param name="locate">キャレットの位置</param>
        /// <returns>(BackSpace後の数式,BackSpace後のキャレットの位置)</returns>
        public static (string, int) BackSpace_LaTeX(string NowFormula, int locate)
        {
            int truelocate = CalcTrueLocate(NowFormula, locate);

            string past = "";

            if (truelocate > 0)
            {
                past = NowFormula[truelocate - 1].ToString();
            }

            //関数を消そうと画策してきたときの処理
            if (past == "{")
            {
                var bracketlocations = new List <(int, int)> {
                };

                var currentlocate = OperateBrackets.LocationofInsideofBracket(NowFormula, truelocate, "{}");
                bracketlocations.Add(currentlocate);

                var inspectionlocate = truelocate - 2;
                var backcount        = 0;
                while (true)
                {
                    if (NowFormula[inspectionlocate] == '}')
                    {
                        var nowlocate = OperateBrackets.LocationofInsideofBracket(NowFormula, inspectionlocate, "{}");
                        bracketlocations.Insert(0, nowlocate);

                        inspectionlocate = nowlocate.Item1 - 1;

                        backcount++;
                    }
                    else if (NowFormula[inspectionlocate] == '^')
                    {
                        bracketlocations.Insert(0, (inspectionlocate, inspectionlocate));

                        break;
                    }
                    else
                    {
                        bracketlocations.Insert(
                            0,
                            OperateBrackets.LocationofInsideofBracket(
                                NowFormula,
                                inspectionlocate,
                                "~" + NowFormula[inspectionlocate]
                                )
                            );

                        break;
                    }
                }

                inspectionlocate = currentlocate.Item2 + 1;

                while (true)
                {
                    if (inspectionlocate >= NowFormula.Length)
                    {
                        break;
                    }

                    if (NowFormula[inspectionlocate] == '{')
                    {
                        var nowlocate = OperateBrackets.LocationofInsideofBracket(NowFormula, inspectionlocate, "{}");
                        bracketlocations.Add(nowlocate);

                        inspectionlocate = nowlocate.Item2 + 1;
                    }
                    else
                    {
                        break;
                    }
                }

                var gap = bracketlocations[0].Item2 - bracketlocations[0].Item1 + 1;

                NowFormula = NowFormula.Remove(bracketlocations[0].Item1, gap);

                for (int i = 1; i < bracketlocations.Count(); i++)
                {
                    NowFormula = NowFormula.Remove(bracketlocations[i].Item1 - gap, 1);
                    gap++;
                    NowFormula = NowFormula.Remove(bracketlocations[i].Item2 - gap, 1);
                    gap++;
                }

                return(NowFormula, locate - backcount - 1);
            }
            else if (past == "}")
            {
                return(NowFormula, locate - 1);
            }
            else
            {
                var res = BackSpace(NowFormula.Substring(0, truelocate), locate);
                return(res.Item1 + NowFormula.Substring(truelocate), res.Item2);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 数式の指定された部分に指定された文字を追加する
        /// </summary>
        /// <param name="nowformula"></param>
        /// <param name="figure"></param>
        /// <param name="locate"></param>
        /// <returns></returns>
        public async static Task <(string, int)> AddfigureManipulation(string nowformula, string figure, int locate)
        {
            string NowFormula = nowformula;

            //一つ前と二つ前の文字を抽出する
            //存在しなかった場合は""となる
            string PastChar     = "";
            string PastPastChar = "";

            try
            {
                PastChar     = NowFormula.Last().ToString();
                PastPastChar = NowFormula.Substring(NowFormula.Length - 2, 1);
            }
            catch { }

            if (figure == "/")//割り算が押された場合は分数を挿入する
            {
                if (NowFormula == "" || PastChar == "{" || PastChar == "(")
                {
                    NowFormula += "~frac{}{}";
                    locate++;
                }
                else
                {
                    int back = 0;
                    for (int i = NowFormula.Length - 1; i >= 0; i--)
                    {
                        if (Regex.IsMatch(NowFormula[i].ToString(), "^(\\+|\\-|\\*|/|\\(|\\{)$"))
                        {
                            back = i + 1;
                            break;
                        }
                        else if (NowFormula[i] == ')')
                        {
                            var locates = OperateBrackets.LocationofInsideofBracket(NowFormula, i);
                            i = locates.Item1;

                            if (i != 0 && NowFormula[i - 1] == '#')
                            {
                                for (int j = i - 2; j >= 0; j--)
                                {
                                    if (NowFormula[j] == '#')
                                    {
                                        i = j;
                                        break;
                                    }
                                }
                            }
                        }
                        else if (NowFormula[i] == '}')
                        {
                            var locates = OperateBrackets.LocationofInsideofBracket(NowFormula, i, "{}");
                            i = locates.Item1;
                        }
                    }

                    var Numor = NowFormula.Substring(back);
                    if (NowFormula != "" && NowFormula.Length > back)
                    {
                        NowFormula = NowFormula.Remove(back);
                    }


                    NowFormula = NowFormula + "~frac{" + Numor + "}{}";

                    locate += 2;
                }
            }
            else if (figure == "root")
            {
                NowFormula += "~root{2}{}";

                locate += 3;
            }
            else if (figure == "^")
            {
                if (!Regex.IsMatch(PastChar, "^(\\+|\\-|\\*|/|\\^|\\(|\\{)$"))
                {
                    NowFormula += "^{}";

                    locate += 1;
                }
            }
            else if (PastChar == "-" && //前が-
                     Regex.IsMatch(PastPastChar, "^(\\*|/|\\^|\\()$") &&//前の前が*,/,^,(
                     Regex.IsMatch(figure, "^(\\+|\\-|\\*|/|\\^|\\))$"))//今のが+,-,*,/,^,)
            {
                switch (figure)
                {
                case "+":
                    (NowFormula, locate) = BackSpace(NowFormula, locate);
                    break;

                case "-":
                    (NowFormula, locate) = BackSpace(NowFormula, locate);
                    NowFormula          += figure;
                    break;

                case "*":
                case "/":
                case "^":
                    (NowFormula, locate) = BackSpace(NowFormula, locate);
                    (NowFormula, locate) = BackSpace(NowFormula, locate);
                    NowFormula          += figure;
                    break;

                case ")":
                    (NowFormula, locate) = BackSpace(NowFormula, locate);
                    (NowFormula, locate) = BackSpace(NowFormula, locate);
                    break;

                default:
                    Trace.WriteLine("内部エラー");
                    break;
                }

                locate++;
            }
            else if (Regex.IsMatch(PastChar, "^(\\+|\\-)$") &&//前が+,-
                     Regex.IsMatch(figure, "^(\\+|\\-|\\*|/|\\^|\\))$"))//今のが+,-,*,/,^,)
            {
                (NowFormula, locate) = BackSpace(NowFormula, locate);
                NowFormula          += figure;

                locate++;
            }
            else if (Regex.IsMatch(PastChar, "^(\\*|/|\\^|\\()$") &&//前が*,/,^,(
                     Regex.IsMatch(figure, "^(\\+|\\*|/|\\^|\\))$"))//今のが+,*,/,^,)
            {
                (NowFormula, locate) = BackSpace(NowFormula, locate);
                NowFormula          += figure;

                locate++;
            }
            else if (figure == "invert" && !Regex.IsMatch(PastChar, "^(\\+|\\-|\\*|/|\\^|\\()$"))//逆数ボタンを押した時
            {
                NowFormula += "^{-1}";

                locate += 4;
            }
            else if (figure == "invert")
            {
            }
            else if (figure.First() == '~')
            {
                if (await FuncItemService.IsExist(figure.Substring(1)))
                {
                    var count = (await FuncItemService.GetByName(figure.Substring(1))).CharCount;

                    if (count == 0)
                    {
                        NowFormula += figure + "{}";
                        locate     += 1;
                    }
                    else
                    {
                        NowFormula += figure + (new StringBuilder()).Insert(0, "{}", count).ToString();
                    }
                }

                locate += 1;
            }
            else if (figure == "." && PastChar == ".")//小数点
            {
            }
            else
            {
                NowFormula += figure;

                locate++;
            }

            return(NowFormula, locate);
        }