Exemple #1
0
        //рекурсивная функция вычисления
        static double MyMath(string input, ref bool exception)
        {
            //Вычисляем сначало выражения в скобках
            int indexin;

            do
            {
                indexin = input.IndexOf('(');
                int indexout    = input.IndexOf(')');
                int nextindexin = input.IndexOf('(', indexin + 1);
                while (indexout > nextindexin && nextindexin != -1)
                {
                    indexout    = input.IndexOf(')', indexout + 1);
                    nextindexin = input.IndexOf('(', nextindexin + 1);
                }

                if (indexin != -1 && indexout != -1)
                {
                    //вычисляем значение в подстроке
                    string tmp    = input.Substring(indexin + 1, indexout - indexin - 1);
                    double answer = MyMath(tmp, ref exception);
                    if (exception)
                    {
                        return(0);
                    }
                    //обрезаем строку
                    tmp = "(" + tmp + ")";
                    string tmp2 = answer.ToString();
                    if (indexin != 0 && ParseClass.CheckValid(input[indexin - 1]))
                    {
                        tmp2 = "*" + tmp2;
                    }
                    input = input.Replace(tmp, tmp2);
                }
            } while (indexin != -1);
            //
            //Разбираем строку
            ListMyNumber lnum = ParseClass.ParseList(input);

            if (lnum.Count == 0)
            {
                exception = true;
                return(0);
            }
            //Выполняем операции в зависимости от их уровня
            lnum.Mathematic();
            return(lnum[0].Value);
        }
        public static ListMyNumber ParseList(string input)
        {
            string       tmp       = "";
            char         leftoper  = '1';
            char         rightoper = '1';
            ListMyNumber lnum      = new ListMyNumber();

            try
            {
                for (int i = 0; i < input.Length; i++)
                {
                    if ((!numberSymbols.Contains(input[i]) && !(i == 0 && input[i] == '-')) || (i == input.Length - 1))
                    {
                        if (i == input.Length - 1)
                        {
                            tmp      += input[i];
                            rightoper = '1';
                        }
                        else
                        {
                            rightoper = input[i];
                        }

                        tmp = tmp.Replace('.', ',');
                        if (!lnum.AddWithCheck(Convert.ToDouble(tmp), leftoper, rightoper))
                        {
                            WriteWarning();
                            lnum.Clear();
                            break;
                        }
                        tmp      = "";
                        leftoper = input[i];
                    }
                    else
                    {
                        tmp += input[i];
                    }
                }
            }
            catch
            {
                WriteWarning();
                lnum.Clear();
            }
            return(lnum);
        }