private static bool IsSeperator(char c)
        {
            switch (c)
            {
            case '_':
            case '.':
            case ',':
            case ' ': return(true);

            default: return(Helfer.GetNumber(c) != -1);
            }
        }
Ejemplo n.º 2
0
        private static void Solve(string input, InOut.Ergebnis erg)
        {
            Stack <int> stack = new Stack <int>();

            input += " ";
            char      c;
            Operation op = null;

            for (int i = 0, number = -1, num2; i < input.Length; i++)
            {
                c = input[i];
                if (Char.IsDigit(c))
                {
                    number = (number == -1 ? Helfer.GetNumber(c) : number * 10 + Helfer.GetNumber(c));
                }
                else if (c == '(')
                {
                    stack.Push(number);
                    number = -1;
                }
                else if (c == ')')
                {
                    number = stack.Pop();
                }
                else
                {
                    if (number > -1)
                    {
                        if (op != null)
                        {
                            number = op(number, number);
                        }
                        else
                        {
                            num2   = number;
                            number = -1;
                        }
                        op = null;
                    }
                    if (operators.Keys.Contains(c))
                    {
                        op = operators[c];
                    }
                }
            }

            erg.Setze(stack.Pop());
        }
        private static void EvalRPN(string input, InOut.Ergebnis erg)
        {
            Stack stack = new Stack();

            char c;
            bool num = false;

            for (int i = 0, number = 0, sign = 0; i < input.Length; i++)
            {
                c = input[i];
                if ((c == '-' || c == '+') && i + 1 < input.Length && char.IsDigit(input[i + 1]))
                {
                    sign = c == '-' ? -1 : 1;
                }
                else if (char.IsDigit(c))
                {
                    if (sign == 0)
                    {
                        sign = 1;
                    }
                    if (num)
                    {
                        number = number * 10 + Helfer.GetNumber(c);
                    }
                    else
                    {
                        number = Helfer.GetNumber(c);
                    }
                    num = true;
                }
                else         //  Not A Digit
                {
                    if (num) // Push parsed number
                    {
                        stack.Push(number * sign);
                        sign = 0;
                        num  = false;
                    }
                    if (operators.Keys.Contains(c))
                    {
                        stack.Push(operators[c](stack));
                    }
                }
            }

            erg.Setze(stack.Pop <int>());
        }
        private static void ConvertToInt(string input, InOut.Ergebnis erg)
        {
            int number = 0;

            try
            {
                if (input.Length == 0)
                {
                    throw wrongFormat;
                }

                int  sign   = 1;
                bool signed = true;
                if (input[0] == '-')
                {
                    sign = -1;
                }
                else if (input[0] == '+')
                {
                    sign = 1;
                }
                else if (Helfer.GetNumber(input[0]) != -1)
                {
                    signed = false;
                }
                else
                {
                    throw wrongFormat;
                }

                char seperator = ' ';
                bool sepSet    = false;

                for (int i = input.Length - 1, multi = 0, curr; i >= (signed ? 1:0); i--)
                {
                    curr = Helfer.GetNumber(input[i]);

                    if (multi > 0 && multi % 3 == 0)
                    {
                        if (!sepSet)
                        {
                            seperator = input[i];
                            if (!IsSeperator(input[i]))
                            {
                                throw wrongFormat;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (input[i] != seperator)
                            {
                                throw wrongFormat;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }

                    if (curr == -1)
                    {
                        throw wrongFormat;
                    }
                    if (multi >= 10)
                    {
                        throw wrongFormat;
                    }
                    number += curr * (int)Math.Pow(10, multi++);
                }
                number *= sign;
            }
            catch (FormatException fe)
            {
                erg.Setze(fe);
                return;
            }
            erg.Setze(number);
        }