c#实现简单四则运算

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/slwsss/article/details/70432277

Compute("1+2*3/4")

        static decimal Compute(string expression)
        {
            int i = 0;
            var r = _Compute(expression, ref i);
            if (i != expression.Length) throw new ArgumentException("╮(︶︿︶)╭");
            return r;
        }
        static decimal _Compute(string expression, ref int i)
        {
            int j;
            char c;
            decimal? r = null;
            decimal t;
            int f = 1, f3;
            bool f2;
            g1:
            while (i < expression.Length)
            {
                c = expression[i];
                if (c >= '0' && c <= '9')
                {
                    for (j = i + 1; j < expression.Length; j++)
                    {
                        c = expression[j];
                        if ((c < '0' || c > '9') && c != '.') break;
                    }
                    t = decimal.Parse(expression.Substring(i, j - i));
                    i = j;
                    goto g2;
                }
                else if (c == '+' || c == '-')
                {
                    f3 = c == '+' ? 1 : -1;
                    i++;
                    while (i < expression.Length)
                    {
                        c = expression[i];
                        if ((c >= '0' && c <= '9') || c == '.')
                        {
                            for (j = i + 1; j < expression.Length; j++)
                            {
                                c = expression[j];
                                if ((c < '0' || c > '9') && c != '.') break;
                            }
                            t = f3 * decimal.Parse(expression.Substring(i, j - i));
                            i = j;
                            goto g2;
                        }
                        else if (c == '(')
                        {
                            i++;
                            if (i >= expression.Length) goto exception;
                            t = _Compute(expression, ref i) * f3;
                            if (i >= expression.Length) goto exception;
                            i++;
                            goto g2;
                        }
                        else if (char.IsWhiteSpace(c))
                        {
                            i++;
                            continue;
                        }
                        else goto exception;
                    }
                    goto exception;
                }
                else if (c == '(')
                {
                    i++;
                    if (i >= expression.Length) goto exception;
                    t = _Compute(expression, ref i);
                    if (i >= expression.Length) goto exception;
                    i++;
                    goto g2;
                }
                else if (char.IsWhiteSpace(c))
                {
                    i++;
                    continue;
                }
                else goto exception;
            }
            exception:
            throw new ArgumentException("╮(︶︿︶)╭");
            g2:
            while (i < expression.Length)
            {
                c = expression[i];
                if (c == '+' || c == '-')
                {
                    r = r.HasValue ? (r.Value + f * t) : (f * t);
                    f = c == '+' ? 1 : -1;
                    i++;
                    goto g1;
                }
                else if (c == '*' || c == '/')
                {
                    f2 = c == '*';
                    i++;
                    while (i < expression.Length)
                    {
                        c = expression[i];
                        if (c >= '0' && c <= '9')
                        {
                            for (j = i + 1; j < expression.Length; j++)
                            {
                                c = expression[j];
                                if ((c < '0' || c > '9') && c != '.') break;
                            }
                            if (f2) t *= decimal.Parse(expression.Substring(i, j - i));
                            else t /= decimal.Parse(expression.Substring(i, j - i));
                            i = j;
                            goto g2;
                        }
                        else if (c == '+' || c == '-')
                        {
                            f3 = c == '+' ? 1 : -1;
                            i++;
                            while (i < expression.Length)
                            {
                                c = expression[i];
                                if ((c >= '0' && c <= '9') || c == '.')
                                {
                                    for (j = i + 1; j < expression.Length; j++)
                                    {
                                        c = expression[j];
                                        if ((c < '0' || c > '9') && c != '.') break;
                                    }
                                    if (f2) t *= (f3 * decimal.Parse(expression.Substring(i, j - i)));
                                    else t /= (f3 * decimal.Parse(expression.Substring(i, j - i)));
                                    i = j;
                                    goto g2;
                                }
                                else if (c == '(')
                                {
                                    i++;
                                    if (i >= expression.Length) goto exception;
                                    if (f2) t *= (_Compute(expression, ref i) * f3);
                                    else t /= (_Compute(expression, ref i) * f3);
                                    if (i >= expression.Length) goto exception;
                                    i++;
                                    goto g2;
                                }
                                else if (char.IsWhiteSpace(c))
                                {
                                    i++;
                                    continue;
                                }
                                else goto exception;
                            }
                            goto exception;
                        }
                        else if (c == '(')
                        {
                            i++;
                            if (i >= expression.Length) goto exception;
                            if (f2) t *= _Compute(expression, ref i);
                            else t /= _Compute(expression, ref i);
                            if (i >= expression.Length) goto exception;
                            i++;
                            goto g2;
                        }
                        else if (char.IsWhiteSpace(c))
                        {
                            i++;
                            continue;
                        }
                        goto exception;
                    }
                    goto exception;
                }
                else if (c == ')') goto g2_end;
                else if (char.IsWhiteSpace(c))
                {
                    i++;
                    continue;
                }
                goto exception;
            }
            g2_end:
            if (r.HasValue) return r.Value + f * t;
            return f * t;
        }

猜你喜欢

转载自blog.csdn.net/slwsss/article/details/70432277