public PolynomialTrie tryLoad(string FileName)
        {
            if (IsFileLocked(new FileInfo(FileName)))
            {
                return(new PolynomialTrie());
            }
            if (!correctFileHeader(FileName))
            {
                return(new PolynomialTrie());
            }
            Process compressor = new Process();

            compressor.StartInfo.CreateNoWindow = true;
            compressor.StartInfo.FileName       = "xwrt32.exe";
            compressor.StartInfo.Arguments      = " -o " + FileName;
            compressor.Start();
            compressor.WaitForExit();
            string tmp = FileName + ".xml";

            if (FileName.Contains('.') && FileName.LastIndexOf('.') != 0)
            {
                tmp = FileName.Substring(0, FileName.LastIndexOf('.'));
            }
            PolynomialTrie result = load(tmp);

            File.Delete(tmp);
            return(result);
        }
        public void save(PolynomialTrie toBeSaved, string FileName)
        {
            XDocument xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));

            xDoc.Add(dfs(toBeSaved.main.root));
            xDoc.Save(FileName);
        }
        public void saveExcel(PolynomialTrie toBeSaved, string fileName)
        {
            DataTable sub, defint;

            paraDT(toBeSaved, out sub, out defint);
            using (XLWorkbook saver = new XLWorkbook(XLEventTracking.Disabled))
            {
                saver.Worksheets.Add(OneOperationDT(toBeSaved));
                saver.Worksheets.Add(sub);
                saver.Worksheets.Add(defint);
                saver.Worksheets.Add(TwoPolynomialsDT(toBeSaved));
                saver.SaveAs(fileName);
            }
        }
        DataTable TwoPolynomialsDT(PolynomialTrie trie)
        {
            DataTable result = new DataTable("Operations on two polynomials");

            foreach (var title in Constants.TwoPolynomialsColumnsName)
            {
                result.Columns.Add(title.Value, typeof(string));
            }
            Polynomial SoFar = new Polynomial();
            Stack <Tuple <Node, Term, bool> > dfsStack = new Stack <Tuple <Node, Term, bool> >();

            dfsStack.Push(new Tuple <Node, Term, bool>(trie.main.root, new Term(0, 0), false));
            while (dfsStack.Count != 0)
            {
                if (dfsStack.Peek().Item3)
                {
                    dfsStack.Pop();
                    if (SoFar.Count != 0)
                    {
                        SoFar.Remove(SoFar.Back.Degree);
                    }
                    continue;
                }
                Node current = dfsStack.Peek().Item1;
                Term edge    = dfsStack.Peek().Item2;
                if (edge.Coefficient != 0)
                {
                    SoFar.Add(edge);
                }
                dfsStack.Pop();
                dfsStack.Push(new Tuple <Node, Term, bool>(current, edge, true));
                foreach (var child in current._children)
                {
                    dfsStack.Push(new Tuple <Node, Term, bool>(child.Value, new Term(child.Key), false));
                }
                if (current.isEnd)
                {
                    foreach (var special in current._special)
                    {
                        if (Constants.operationsName[special.Key] == "Tree")
                        {
                            SecondTreeExtraction(special.Value as Trie, SoFar, ref result);
                        }
                    }
                }
            }
            return(result);
        }
 public bool trySave(PolynomialTrie toBeSaved, string FileName)
 {
     if (FileName.Substring(FileName.Length - 5) != ".plcl")
     {
         FileName += ".plcl";
     }
     if (File.Exists(FileName))
     {
         File.Delete(FileName);
     }
     try
     {
         saveCompressed(toBeSaved, FileName);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        public void saveCompressed(PolynomialTrie toBeSaved, string FileName)
        {
            if (!File.Exists("xwrt32.exe"))
            {
                throw new FileNotFoundException("Compressor was not found");
            }
            string temp = FileName + ".tmp";

            save(toBeSaved, temp);
            Process compressor = new Process();

            compressor.StartInfo.FileName       = "xwrt32.exe";
            compressor.StartInfo.Arguments      = " -l3 +d -o " + temp;
            compressor.StartInfo.CreateNoWindow = true;
            compressor.Start();
            compressor.WaitForExit();
            File.Delete(temp);
            temp += ".xwrt";
            File.Copy(temp, FileName, true);
            File.Delete(temp);
        }
        void paraDT(PolynomialTrie trie, out DataTable substitutaions, out DataTable definiteIntegral)
        {
            substitutaions   = new DataTable("subtitutions in polynomial");
            definiteIntegral = new DataTable("Definite integral on polynomial");
            foreach (var title in Constants.SubstitutionColumnsName)
            {
                substitutaions.Columns.Add(title, typeof(string));
            }
            foreach (var title in Constants.DefiniteIntColumnsName)
            {
                definiteIntegral.Columns.Add(title, typeof(string));
            }
            Polynomial SoFar = new Polynomial();
            Stack <Tuple <Node, Term, bool> > dfsStack = new Stack <Tuple <Node, Term, bool> >();

            dfsStack.Push(new Tuple <Node, Term, bool>(trie.main.root, new Term(0, 0), false));
            while (dfsStack.Count != 0)
            {
                if (dfsStack.Peek().Item3)
                {
                    dfsStack.Pop();
                    if (SoFar.Count != 0)
                    {
                        SoFar.Remove(SoFar.Back.Degree);
                    }
                    continue;
                }
                Node current = dfsStack.Peek().Item1;
                Term edge    = dfsStack.Peek().Item2;
                if (edge.Coefficient != 0)
                {
                    SoFar.Add(edge);
                }
                dfsStack.Pop();
                dfsStack.Push(new Tuple <Node, Term, bool>(current, edge, true));
                foreach (var child in current._children)
                {
                    dfsStack.Push(new Tuple <Node, Term, bool>(child.Value, new Term(child.Key), false));
                }
                if (current.isEnd)
                {
                    substitutaions.Rows.Add(substitutaions.NewRow());
                    definiteIntegral.Rows.Add(definiteIntegral.NewRow());
                    int indexSub    = substitutaions.Rows.Count - 1;
                    int indexDefint = definiteIntegral.Rows.Count - 1;
                    substitutaions.Rows[indexSub]["Polynomial"]      = SoFar.ToString();
                    definiteIntegral.Rows[indexDefint]["Polynomial"] = SoFar.ToString();
                    bool foundSubstitution = false;
                    bool foundDefint       = false;
                    foreach (var special in current._special)
                    {
                        if (special.Key == 's')
                        {
                            foundSubstitution = true;
                            foreach (var substitutaion in special.Value as paraStore)
                            {
                                substitutaions.Rows[indexSub]["X"]      = ComplexUtils.ComplexToString(substitutaion.Key[0]);
                                substitutaions.Rows[indexSub]["Result"] = ComplexUtils.ComplexToString(substitutaion.Value);
                            }
                        }
                        else if (special.Key == 'd')
                        {
                            foundDefint = true;
                            foreach (var defint in special.Value as paraStore)
                            {
                                definiteIntegral.Rows[indexSub]["A"]      = ComplexUtils.ComplexToString(defint.Key[0]);
                                definiteIntegral.Rows[indexSub]["B"]      = ComplexUtils.ComplexToString(defint.Key[1]);
                                definiteIntegral.Rows[indexSub]["Result"] = ComplexUtils.ComplexToString(defint.Value);
                            }
                        }
                    }
                    if (!foundSubstitution)
                    {
                        substitutaions.Rows.RemoveAt(substitutaions.Rows.Count - 1);
                    }
                    if (!foundDefint)
                    {
                        definiteIntegral.Rows.RemoveAt(definiteIntegral.Rows.Count - 1);
                    }
                }
            }
        }
        DataTable OneOperationDT(PolynomialTrie trie)
        {
            DataTable result = new DataTable("Operations on one polynomial");

            foreach (var title in Constants.OnePolynomialColumnsName)
            {
                result.Columns.Add(title.Value, typeof(string));
            }
            Polynomial SoFar = new Polynomial();
            Stack <Tuple <Node, Term, bool> > dfsStack = new Stack <Tuple <Node, Term, bool> >();

            dfsStack.Push(new Tuple <Node, Term, bool>(trie.main.root, new Term(0, 0), false));
            while (dfsStack.Count != 0)
            {
                if (dfsStack.Peek().Item3)
                {
                    dfsStack.Pop();
                    if (SoFar.Count != 0)
                    {
                        SoFar.Remove(SoFar.Back.Degree);
                    }
                    continue;
                }
                Node current = dfsStack.Peek().Item1;
                Term edge    = dfsStack.Peek().Item2;
                if (edge.Coefficient != 0)
                {
                    SoFar.Add(edge);
                }
                dfsStack.Pop();
                dfsStack.Push(new Tuple <Node, Term, bool>(current, edge, true));
                foreach (var child in current._children)
                {
                    dfsStack.Push(new Tuple <Node, Term, bool>(child.Value, new Term(child.Key), false));
                }
                if (current.isEnd)
                {
                    result.Rows.Add(result.NewRow());
                    int index = result.Rows.Count - 1;
                    result.Rows[index]["Polynomial"] = SoFar.ToString();
                    bool foundOperation = false;
                    foreach (var special in current._special)
                    {
                        if (Constants.operationsName[special.Key] == "Result")
                        {
                            foundOperation = true;
                            result.Rows[index][Constants.OnePolynomialColumnsName[special.Key]] = (special.Value as Polynomial).ToString();
                        }
                        else if (Constants.operationsName[special.Key] == "listComplex")
                        {
                            foundOperation = true;
                            result.Rows[index][Constants.OnePolynomialColumnsName[special.Key]] = ComplexUtils.ComplexListToString(special.Value as List <Complex>);
                        }
                    }
                    if (!foundOperation)
                    {
                        result.Rows.RemoveAt(result.Rows.Count - 1);
                    }
                }
            }
            return(result);
        }