/// <summary> /// 式を展開する /// </summary> /// <param name="f"></param> /// <returns></returns> public async static Task <Formula> ExpandCharFormula(string f) { f = SolveNumFormulas.LaTeXtoFormula(f); var ret = await Expand(f); Trace.WriteLine("Expanded:" + ret.GetString()); return(ret); }
/// <summary> /// 文字式(未整理)の累乗を処理 /// </summary> /// <param name="f"></param> /// <returns></returns> public static async Task <string> ExpandPowAsync(string f) { if (f.Contains("^")) { f = Regex.Replace(f, "^\\^(\\+|-)?(\\d|\\.)*", ""); //頭の累乗記号+数字は消す f = Regex.Replace(f, "\\^$", ""); //末尾の累乗記号も消す f = Regex.Replace(f, "\\^([a-z|A-Z])", "$1"); //文字で累乗されている場合は累乗をなかったことにする f = Regex.Replace(f, "[a-z|A-Z]\\^(\\d)+", Match => { return(new string(Match.Value.First(), int.Parse(Regex.Replace(Match.Value, "(.*)(\\d+)(.*)", "$2")))); }); while (f.Contains("^")) { int PowIndex = f.IndexOf('^'); if (PowIndex == 0) { throw new Exception(AppResources.PowerMustTop); } string Powed = "1"; if (f[PowIndex - 1] == ')') { //Xamarin.Forms.DependencyService.Get<IToastService>() //.Show("括弧に'^'は付けられません"); Powed = "(" + OperateBrackets.TakeoutInsideofBracket(f, PowIndex - 2) + ")"; throw new Exception(AppResources.CantAddPowerAfterParenthes); } else if (Regex.IsMatch(f[PowIndex - 1].ToString(), "(\\+|-)?(\\d|\\.)")) { Powed = Regex.Match(f.Substring(0, PowIndex), "(\\+|-)?(\\d+|\\.)$").Value; } else { Powed = f[PowIndex - 1].ToString(); } Double Powist = 1.0; if (f[PowIndex + 1] == '(') { var content = OperateBrackets.TakeoutInsideofBracket(f, PowIndex + 1); if (Regex.IsMatch(content, "[a-z]")) { throw new Exception(AppResources.CantAddPowerAfterChar); } Powist = await SolveNumFormulas.SolveNumFormula(content); f = f.Remove(PowIndex - 1, content.Length + 4);//^の分(1)+カッコの分(2)+ } else { var content = Regex.Match(f.Substring(PowIndex), "^(\\+|-)?(\\d|\\.)+").Value; Powist = double.Parse(content); f = f.Remove(PowIndex - 1, content.Length + 2); } var repeated = ""; if (double.TryParse(Powed, out double Poweddouble)) { repeated = Math.Pow(Poweddouble, Powist).ToString(); } else { repeated = string.Concat(Enumerable.Repeat(Powed, (int)Powist)); } f = f.Insert(PowIndex - 1, repeated); } } return(f); }