/// <summary> /// 寻找解,找不到时返回"answer not found\r\n" /// <para>每个解占一行</para> /// </summary> /// <param name="equation">要找的方程</param> /// <returns></returns> public async Task <string> get_results(BaseEquation equation) { string ret = "answer not found\r\n"; LinkedList <SubEquation> sorted_equation_list = new LinkedList <SubEquation>(); List <SubEquation> calculated_equation_list = new List <SubEquation>(); SubEquation root = new SubEquation(equation); sorted_equation_list = sorted_insert(sorted_equation_list, calculated_equation_list, root); SubEquation first_ans = null; bool already_first = false; while (true) { if (sorted_equation_list.Count == 0) { break; } ConsoleMazeMain.BaseEquationEventArgs ee = new ConsoleMazeMain.BaseEquationEventArgs(); ee.be = sorted_equation_list.First.Value; callback(this, ee); bool bk = false; await Task.Run(() => { if (sorted_equation_list.First.Value.ans_found) { if (ret == "answer not found\r\n") { first_ans = sorted_equation_list.First.Value; ret = ""; } ret += sorted_equation_list.First.Value.equation_console + "\r\n"; bk = true; return; } LinkedListNode <SubEquation> now = sorted_equation_list.First; sorted_equation_list.RemoveFirst(); List <char> ava_l = now.Value.available_letters_from_last(1); List <int> ava_n = now.Value.available_nums(); for (int i = 0; i < ava_n.Count; i++) { if (ava_n[i] == 0) { int first_l = now.Value.get_first_in_each_line().IndexOf(ava_l[0]); if (first_l != -1 && now.Value.spilt_string_without_operator[first_l].Length > 1) { continue; } } string new_eva = now.Value.replace(ava_l[0], ava_n[i]); SubEquation new_eq = new SubEquation(new_eva); if (new_eq.ans_found) { if (ret == "answer not found\r\n") { first_ans = new_eq; ret = ""; } ret += new_eq.equation_console + "\r\n"; continue; } sorted_equation_list = sorted_insert(sorted_equation_list, calculated_equation_list, new_eq); } }); if (first_ans != null && !already_first) { ConsoleMazeMain.BaseEquationEventArgs ee2 = new ConsoleMazeMain.BaseEquationEventArgs(); ee2.be = first_ans; ee2.is_ans = true; callback(this, ee2); already_first = true; } if (bk) { break; } } return(ret); }
private static LinkedList <SubEquation> sorted_insert(LinkedList <SubEquation> list, List <SubEquation> calculated, SubEquation to_insert) { if (to_insert.node_point.compare_to(0) < 0) { return(list); } if (list.Count == 0) { list.AddFirst(to_insert); calculated.Add(to_insert); } else { for (int i = 0; i < calculated.Count; i++) { if (to_insert.same_as(calculated[i])) { return(list); } } LinkedListNode <SubEquation> now = list.First; bool inserted = false; for (int i = 0; i < list.Count; i++) { if (to_insert.node_point.compare_to(now.Value.node_point) < 0) { list.AddBefore(now, to_insert); calculated.Add(to_insert); inserted = true; break; } now = now.Next; } if (!inserted) { list.AddLast(to_insert); calculated.Add(to_insert); } } return(list); }