Example #1
0
        private void Evaluation(object sender, RoutedEventArgs e)
        {
            // get the value.
            string value = Rbox1.Text;

            if (value == "" || value == null)
            {
                return;
            }
            string valuex = "(" + value + ")";

            switch (method)
            {
            case Method.PostFix:
                Rbox2.Text = Evaluate.PostFix(value).ToString();
                break;

            case Method.PreFix:
                var handler = new Input(value);
                Rbox2.Text = handler.GetPreFix();
                Rbox1.Text = "";
                break;

            case Method.Infix:
                Rbox2.Text = Infix.Evaluate(valuex).ToString();
                Rbox1.Text = "";
                break;

            default:
                break;
            }
        }
        private double Integral(List <string> nonOpComponents, string a, int cycle = 0)
        {
            double returnValue = 0;
            //int cycles = nonOpComponents.Count / 2;
            //double bottom = Convert.ToDouble(nonOpComponents[0]);
            //double top = Convert.ToDouble(nonOpComponents[1]);
            double bottom = tokenToDouble(nonOpComponents[0], cycle);
            double top    = tokenToDouble(nonOpComponents[1], cycle);
            //Console.WriteLine("top: " + top);
            double w = 5000;
            double n = 2 * w;
            double h = (top + bottom) / n;

            for (int j = 1; j <= Regex.Matches(Regex.Escape(a), Regex.Escape(@"\" + TokenLibrary.tokenLibrary["summation"])).Count + 1; j++)
            {
                a = a.Replace("&" + j, "&" + j + ":");
            }

            //Console.WriteLine("h: " + h);
            double fa = new Infix(a.Replace("&1:", "(" + bottom + ")")).ToPostfix().SolvedExpression();
            double fb = new Infix(a.Replace("&1:", "(" + top + ")")).ToPostfix().SolvedExpression();
            //Console.WriteLine(fa);
            //Console.WriteLine("Fb:" + fb);

            List <string> twoSummation = new List <string>()
            {
                "1", Convert.ToString(n / 2 - 1)
            };
            List <string> fourSummation = new List <string>()
            {
                "1", Convert.ToString(n / 2)
            };
            //foreach(string s in twoSummation)
            //{
            //    Console.WriteLine("test: " + s);
            //}
            //\I,0,3\[&x]
            string twoSummationInfix  = a.Replace("&1:", "(" + bottom + "+" + "2*&1*" + h + ")");
            string fourSummationInfix = a.Replace("&1:", "(" + bottom + "+" + "(2*&1-1)*" + h + ")");

            //Console.WriteLine(summationInfix);
            returnValue = (h / 3) * (fa + fb + 2 * Summation(twoSummation, twoSummationInfix) + 4 * Summation(fourSummation, fourSummationInfix));

            return(returnValue);
        }
        private double RecursiveSummation(string workingInfix, List <int> ns, List <int> tops, /*List<int> index, List<int> limit,*/ int level, int cycles, int cycle = 0)
        {
            double returnValue = 0;

            if (level == cycles)
            {
                //if (!workingInfix.Contains("&" + (level + 1)))
                //{
                //Console.WriteLine("Done:                " + workingInfix);
                returnValue += new Infix(workingInfix).ToPostfix().SolvedExpression(cycle);
                //Console.WriteLine(returnValue);
                //}
            }
            else
            {
                int currentN = ns[level];

                //Console.WriteLine();
                //Console.WriteLine("You good? No              " + workingInfix);
                //Console.WriteLine();
                //for (index[level] = ns[level]; index[level] < tops[level]; index[level]++)
                for (; currentN <= tops[level]; currentN++)
                {
                    //Console.WriteLine("Current N:               " + currentN);
                    //Console.WriteLine("Level:              " + (level + 1));//"Loop:              " + workingInfix);
                    string newRawInfix = workingInfix.Replace("&" + (level + 1) + ":", "&" + currentN + ";").Replace(";;", ";");//.Replace("&&&", "&&");
                    //Console.WriteLine(newRawInfix);
                    returnValue += RecursiveSummation(newRawInfix, ns, tops, /*index,*/ level + 1, cycles, cycle + 1);
                    //Console.WriteLine(returnValue);
                }



                //Console.WriteLine();
                //Console.WriteLine("You good?              " + workingInfix);
                //Console.WriteLine();

                //\E,1,2,1,2\|&1&2|
            }

            return(returnValue);
        }
        static void Main(string[] args)
        {
            //Console.WriteLine(breakLine);
            Console.WriteLine();
            //do
            while (true)
            {
                Console.Write("Input Calculation: ");
                Infix   infix   = new Infix(Console.ReadLine());
                Postfix postfix = infix.ToPostfix();

                if (infix.RawInfix == "")
                {
                    break;
                }

                bool debug = false;
                //debug = true;

                bool checkInfix = false;
                //checkInfix = true;
                //Console.WriteLine(infix.TokenizedInfix.ToArray());
                if (debug)
                {
                    if (checkInfix)
                    {
                        foreach (IToken token in infix.InfixTokens)
                        {
                            //if (token is Operand)
                            //{
                            //    Operand operand = (Operand)token;
                            //    if (operand.Negative)
                            //        Console.WriteLine("-" + token.Token);
                            //    else
                            //        Console.WriteLine(token.Token);
                            //}
                            //else
                            Console.WriteLine("Debug:               " + token.Token);
                            //if (token is Operator)
                            //{
                            //    Console.WriteLine("operator:                        " + token.Token);
                            //}
                        }
                    }
                    else
                    {
                        foreach (IToken token in postfix.PostfixExpression)
                        {
                            //Console.WriteLine("bazinga");
                            Console.WriteLine("Debug:                " + token.Token);
                        }

                        //Console.WriteLine();
                        //Console.WriteLine("Answer: " + postfix.SolvedExpression());
                    }
                }
                Console.WriteLine("Answer: " + postfix.SolvedExpression());

                //if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                //{
                //    break;
                //}
                //else
                //{
                //Console.WriteLine();
                Console.WriteLine(breakLine);
                //Console.WriteLine("--------------------------------------------------");
                Console.WriteLine();
                //}
            }// while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape));
        }
        private double Summation(List <string> nonOpComponents, string a, int cycle = 0)
        {
            double returnValue = 0;
            int    cycles      = nonOpComponents.Count / 2;
            //while (i <= top)
            //{
            //    i++;
            //}
            //\I,0,#p\[\sin\&x]
            double addValue = 0;

            //Console.WriteLine(a);

            //\E,0,2,0,2\[2^\E,0,2\[&i&j&k]]

            //for (int i = 1; i <= Regex.Matches(Regex.Escape(a), Regex.Escape(@"\" + TokenLibrary.tokenLibrary["summation"])).Count + 1; i++)
            //{
            //    a = a.Replace("&" + i, "&" + i + ":");
            //}
            //foreach (Match match in Regex.Matches(Regex.Escape(a), Regex.Escape("&")))
            //{
            //    int index = match.Index + 2;
            //    Console.WriteLine("Index         " + index);
            //    a.Insert(match.Index + 2, ":");
            //}
            //int endVariableIndexBuffer = 0;
            //Console.WriteLine("A            " + a);
            //foreach (int index in Extensions.StringExtensions.AllIndexesOf(Regex.Escape(a), "&"))
            //{
            //    //Console.WriteLine("Index1               " +index);
            //    Console.WriteLine("EndIndex             " + a[index - 1]);// + 0 + endVariableIndexBuffer]);
            //    a.Insert(index - 0 + endVariableIndexBuffer, ":");
            //    endVariableIndexBuffer++;
            //}

            if (cycles <= 1)
            {
                int i   = (int)tokenToDouble(nonOpComponents[0], cycle);
                int top = (int)tokenToDouble(nonOpComponents[1], cycle);
                //\E,0,2\[\E,0,&i\[4]]
                //\E,0,4,0,5\[&i^&j]
                //if (!a.Contains("&"))
                //{
                //    addValue = new Infix(a).ToPostfix().SolvedExpression();
                //}
                for (int j = 1; j <= Regex.Matches(Regex.Escape(a), Regex.Escape(@"\" + TokenLibrary.tokenLibrary["summation"])).Count + 1; j++)
                {
                    a = a.Replace("&" + j, "&" + j + ":");
                }

                //int nestedSummations = 1;

                for (; i <= top; i++)
                {
                    string parseString = a;
                    string newRawInfix = a;
                    //Console.WriteLine("ParseString1:              " + parseString);
                    //do
                    //{
                    //    int index = parseString.IndexOf(@"\" + TokenLibrary.tokenLibrary["summation"]);
                    //    if (index == -1)
                    //        index = 0;
                    //    else
                    //        index += 2;
                    //    //Console.WriteLine(index);
                    //    Console.WriteLine("Substring             "+parseString.Substring(index));
                    //    parseString = parseString.Substring(index);
                    //    newRawInfix = newRawInfix  .Replace(",&" + nestedSummations + ":,", "," + i + ",")
                    //                            .Replace(",&" + nestedSummations + @":\", "," + i + @"\")
                    //                            .Replace("&" + nestedSummations + ":", "&" + i + ";");
                    //    Console.WriteLine("NestedSummation             "+ nestedSummations);
                    //    nestedSummations++;
                    //    Console.WriteLine("ParseString:              " + parseString);
                    //    //Console.WriteLine()
                    //}
                    //while (parseString.Contains(@"\" + TokenLibrary.tokenLibrary["summation"]));

                    //for (int j = 1; j <= Regex.Matches(Regex.Escape(a), Regex.Escape(@"\" + TokenLibrary.tokenLibrary["summation"])).Count + 1; j++)
                    //{
                    //    //Console.WriteLine("h");
                    //    int index = parseString.IndexOf(@"\" + TokenLibrary.tokenLibrary["summation"]);
                    //    if (index == -1)
                    //        index = 0;
                    //    else
                    //        index += 2;
                    //    //Console.WriteLine(index);
                    //    //Console.WriteLine("Substring             " + parseString.Substring(index));
                    //    parseString = parseString.Substring(index);
                    //Console.WriteLine("Summation Raw Infix          " + newRawInfix);
                    //newRawInfix = newRawInfix   .Replace(",&" + j + ":,", "," + i + ",")
                    //                            .Replace(",&" + j + @":\", "," + i + @"\")
                    //                            .Replace("&" + j + ":", "&" + i + ";")
                    //                            .Replace(";;",";")
                    //                            .Replace(";:", ";");
                    newRawInfix = newRawInfix.Replace(",&" + Convert.ToString(cycle + 1) + ":,", "," + i + ",")
                                  .Replace(",&" + Convert.ToString(cycle + 1) + @":\", "," + i + @"\")
                                  .Replace("&" + Convert.ToString(cycle + 1) + ":", "&" + i + ";")
                                  .Replace(";;", ";")
                                  .Replace(";:", ";")
                                  .Replace(":;", ";");
                    //Console.WriteLine("Summation Raw Infix New          " + newRawInfix);
                    //Console.WriteLine("NestedSummation             " + j);
                    //nestedSummations++;
                    //Console.WriteLine("ParseString:              " + parseString);
                    //}

                    //{
                    //\E,0,2\[\E,0,4\[&i&j]]
                    //\E,0,4\[&i]
                    //}
                    //Console.WriteLine("Value added");
                    addValue = new Infix(newRawInfix).ToPostfix().SolvedExpression(cycle + 1);

                    //\E,0,4\[\E,0,5\[&i^&j]]
                    //if (a.Contains("&1"))
                    //{
                    //    //Console.WriteLine(a);
                    //    string newRawInfix = a  .Replace(",&1,", "," + i + ",")
                    //                            .Replace(@",&1\", "," + i + @"\")
                    //                            .Replace("&1", "&" + i + ";");
                    //    //Console.WriteLine(newRawInfix);
                    //    //Console.WriteLine(newRawInfix);
                    //    addValue = new Infix(newRawInfix).ToPostfix().SolvedExpression();
                    //}

                    returnValue += addValue;
                }

                return(returnValue);
            }
            else
            {
                List <int> ns    = new List <int>();
                List <int> tops  = new List <int>();
                List <int> index = new List <int>();
                for (int c = 0; c < cycles; c++)
                {
                    //{E,0,({I,0,#p}[{sin}&i]) }     [&i^3]
                    //Console.WriteLine("Testing:               " + nonOpComponents[2 * c]);
                    ns.Add((int)tokenToDouble(nonOpComponents[2 * c]));
                    tops.Add((int)tokenToDouble(nonOpComponents[2 * c]));
                }
                //\E,1,2,1,2\|&1&2|

                string parsedInfix = a;
                //Console.WriteLine("Parsed Infix Recursive              " + parsedInfix);
                for (int i = cycles + 1; i > 0; i--)
                {
                    parsedInfix = parsedInfix.Replace("&" + i, "&" + i + ":");
                }

                returnValue = RecursiveSummation(parsedInfix, ns, tops, /*index,*/ 0, cycles, cycle);
                //if (!a.Contains("&"))
                //{
                //    addValue = new Infix(a).ToPostfix().SolvedExpression();
                //}

                //for (int c = 0; c < cycles; c++)
                //{
                //    ns.Add(Convert.ToInt32(nonOpComponents[2 * c]));
                //    tops.Add(Convert.ToInt32(nonOpComponents[2 * c + 1]));
                //    string currentCycle = c.ToString();

                //    int currentN = ns[c];
                //    int currentTop = tops[c];

                //    if (!a.Contains("&" + c))
                //    {
                //        addValue = new Infix(a).ToPostfix().SolvedExpression();
                //    }

                //    for (; currentN <= currentTop; currentN++)
                //    {
                //        if (a.Contains("&" + c))
                //        {
                //            string newRawInfix = a.Replace("&" + c, "&" + currentN + "&");

                //        }
                //    }
                //}

                //returnValue = 1;
            }

            return(returnValue);
        }