예제 #1
0
        public void DivideTest()
        {
            math_pack cs = new math_pack();

            try
            {
                cs.fce_deleno(5, 0);
                Assert.Fail("No exception thrown when dividing by 0");
            }
            catch (DivideByZeroException ex)
            {
                Assert.AreEqual("Cannot divide by zero", ex.Message);
            }
            Assert.AreEqual(1, cs.fce_deleno(6, 6), 0, "6 / 6 != 1");
            Assert.AreEqual(5, cs.fce_deleno(15, 3), 0, "15 / 3 != 5");
            Assert.AreEqual(-5, cs.fce_deleno(-15, 3), 0, "-15 / 3 != -5");
            Assert.AreEqual(-5, cs.fce_deleno(15, -3), 0, "15 / (-3) != -5");
            Assert.AreEqual(5, cs.fce_deleno(-15, -3), 0, "-15 / (-3) != 5");
            Assert.AreEqual(1.2, cs.fce_deleno(6, 5), EPS, "6 / 5 != 1.2");
            Assert.AreEqual(200000, cs.fce_deleno(1000000, 5), 0, "1,000,000 / 5 != 200,000");
        }
예제 #2
0
        /**
         * \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í
            }
        }