Exemple #1
0
        public void PowTest()
        {
            //implement both Power and Root of a number in this method because of the possibility to express one as the other
            //which means the user can call Power, but depending on the arguments, the method should calculate Root and vice versa
            math_pack cs = new math_pack();

            //the error range shouldn't be required for most of these, but because this is a complicated method, I'm being nice :)
            Assert.AreEqual(16, cs.fce_mocnina(2, 4), EPS, "2^4 != 16");
            Assert.AreEqual(1, cs.fce_mocnina(2, 0), EPS, "2^0 != 1");
            Assert.AreEqual(2, cs.fce_mocnina(2, 1), EPS, "2^1 != 2");
            Assert.AreEqual(16, cs.fce_mocnina(-2, 4), EPS, "(-2)^4 != 16");
            Assert.AreEqual(-8, cs.fce_mocnina(-2, 3), EPS, "(-2)^3 != -8");
            Assert.AreEqual(0.25, cs.fce_mocnina(2, -2), EPS, "2^(-2) != 1/4 (0.25)");
            Assert.AreEqual(1000000, cs.fce_mocnina(10, 6), EPS, "10^6 != 1,000,000");
            Assert.IsTrue(Double.IsInfinity(cs.fce_mocnina(Double.MaxValue, 2)));
            Assert.IsTrue(Double.IsInfinity(cs.fce_mocnina(Double.MinValue, 2)));
        }
        /**
         * \brief Výpočet výsledku
         * \param input Kontrolní string, kde je celý příklad
         */

        private void InputProcess(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                textInput.Text = "0";
            }
            else
            {
                int index = 0;

                try
                {
                    #region výpočet

                    while (ExampleList.Count > 1)   //výpočet celého příkladu
                    {
#if DEBUG
                        Console.WriteLine("//-------------------------");
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.WriteLine("ExampleList.Count = {0}", ExampleList.Count);
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        for (int i = 0; i < ExampleList.Count; i++)
                        {
                            Console.WriteLine("\tExampleList[{0}]\t=\t{1}", i, ExampleList[i]);
                        }
                        Console.ResetColor();
                        Console.WriteLine("//-------------------------");
#endif

                        //prioritník
                        if (ExampleList.Contains("!"))                                                                                                      //pokud stále existuje
                        {
                            index = ExampleList.IndexOf("!");                                                                                               //nalezení indexu

                            ExampleList[index - 1] = M.fce_fakt(double.Parse(ExampleList[index - 1])).ToString();                                           //výpočet

                            ExampleList.RemoveAt(index);                                                                                                    //smazaní nadbytečného
                        }
                        else if (ExampleList.Contains("√"))                                                                                                 //jinak pokud stále  existuje
                        {
                            index = ExampleList.IndexOf("√");                                                                                               //nalezení indexu
                            //if (int.Parse(ExampleList[index - 1]) > 15) throw new ArgumentOutOfRangeException();
                            ExampleList[index] = M.fce_odmocnina(double.Parse(ExampleList[index + 1]), int.Parse(ExampleList[index - 1]), iter).ToString(); //výpočet

                            ExampleList.RemoveAt(index + 1);                                                                                                //smazání nadbytečného
                            ExampleList.RemoveAt(index - 1);                                                                                                //smazaní nadbytečného
                        }
                        else if (ExampleList.Contains("^"))                                                                                                 //pokud stále existuje
                        {
                            index = ExampleList.IndexOf("^");                                                                                               //nalezení indexu

                            ExampleList[index] = M.fce_mocnina(double.Parse(ExampleList[index - 1]), int.Parse(ExampleList[index + 1])).ToString();         //výpočet

                            ExampleList.RemoveAt(index + 1);                                                                                                //smazání nadbytečného
                            ExampleList.RemoveAt(index - 1);                                                                                                //smazaní nadbytečného
                        }
                        else if (ExampleList.Contains("÷"))                                                                                                 //pokud stále existuje
                        {
                            index = ExampleList.IndexOf("÷");                                                                                               //nalezení indexu

                            ExampleList[index] = M.fce_deleno(double.Parse(ExampleList[index - 1]), double.Parse(ExampleList[index + 1])).ToString();       //výpočet

                            ExampleList.RemoveAt(index + 1);                                                                                                //smazání nadbytečného
                            ExampleList.RemoveAt(index - 1);                                                                                                //smazaní nadbytečného
                        }
                        else if (ExampleList.Contains("×"))                                                                                                 //pokud stále existuje
                        {
                            index = ExampleList.IndexOf("×");                                                                                               //nalezení indexu

                            ExampleList[index] = M.fce_krat(double.Parse(ExampleList[index - 1]), double.Parse(ExampleList[index + 1])).ToString();         //výpočet

                            ExampleList.RemoveAt(index + 1);                                                                                                //smazaní nadbytečného
                            ExampleList.RemoveAt(index - 1);                                                                                                //smazání nadbytečného
                        }
                        else if (ExampleList.Contains("-"))                                                                                                 //pokud stále existuje
                        {
                            index = ExampleList.IndexOf("-");                                                                                               //nalezení indexu

                            ExampleList[index] = M.fce_minus(double.Parse(ExampleList[index - 1]), double.Parse(ExampleList[index + 1])).ToString();        //výpočet

                            ExampleList.RemoveAt(index + 1);                                                                                                //smazaní nadbytečného
                            ExampleList.RemoveAt(index - 1);                                                                                                //smazání nadbytečného
                        }
                        else if (ExampleList.Contains("+"))                                                                                                 //pokud stále existuje
                        {
                            index = ExampleList.IndexOf("+");                                                                                               //nalezení indexu

                            ExampleList[index] = M.fce_plus(double.Parse(ExampleList[index - 1]), double.Parse(ExampleList[index + 1])).ToString();         //výpočet

                            ExampleList.RemoveAt(index + 1);                                                                                                //smazaní nadbytečného
                            ExampleList.RemoveAt(index - 1);                                                                                                //smazání nadbytečného
                        }
                        else
                        {
                            textOutput.Text = "CALC ERROR"; //CHYBA
                            ExampleList.Clear();            //vyčištění
                            Example = string.Empty;         //vyčištění
                            InputShow(Example);             //vyčištění
                            return;
                        }
                    }

                    #endregion výpočet

                    textOutput.Text = ExampleList[0]; //výpis výsledku
                    ANSWER          = ExampleList[0]; //uložení výsledku
                    GotResult       = true;           //dostal jsem výsledek
                    MadeFakt        = false;
                }
                catch (Exception ex)
                {
#if DEBUG
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Exception: {0}", ex.TargetSite.Name);
                    Console.ResetColor();
#endif

                    switch (ex.TargetSite.Name)
                    {
                    case "fce_deleno":
                        textOutput.Text = "DIV BY ZERO";
                        break;

                    case "fce_odmocnina":
                        textOutput.Text = "BAD ROOT NUMBER";
                        break;

                    case "StringToNumber":
                        textOutput.Text = "NO DOUBLE EXP";
                        break;

                    default:
                        textOutput.Text = "CALC ERROR";
                        break;
                    }
                    NumberMode     = false;
                    GotError       = true;
                    ANSWER         = "0";
                    textInput.Text = "0";
                }
                Example = string.Empty; //vyčištění
                ExampleList.Clear();    //vyčištění
            }
        }