c#实现简单四则运算

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;
        }

 

 

 

 

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现四则运算需要掌握以下技术: 1. 字符串的处理,包括字符串的分割、替换、截取等操作; 2. 控件的使用,如文本框、按钮、标签等; 3. 事件的处理,如按钮的点击事件、文本框的文本变化事件等; 4. 表达式的求值,可以使用栈来实现。 下面是一个简单实现步骤: 1. 在窗体中添加文本框、按钮等控件,用于输入表达式和显示结果; 2. 给按钮添加点击事件处理程序,当按钮被点击时,获取文本框中的表达式; 3. 对表达式进行分割和处理,将其转化为后缀表达式; 4. 对后缀表达式进行求值,得到结果; 5. 将结果显示在结果文本框中。 下面是一个示例代码,供参考: ```csharp private void btnCalculate_Click(object sender, EventArgs e) { string expression = txtExpression.Text; string postfixExpression = ConvertToPostfix(expression); double result = EvaluatePostfix(postfixExpression); txtResult.Text = result.ToString(); } private string ConvertToPostfix(string expression) { // TODO: 实现中缀表达式转后缀表达式的算法 } private double EvaluatePostfix(string postfixExpression) { Stack<double> operandStack = new Stack<double>(); string[] tokens = postfixExpression.Split(' '); foreach (string token in tokens) { if (IsOperator(token)) { double operand2 = operandStack.Pop(); double operand1 = operandStack.Pop(); double result = Evaluate(token, operand1, operand2); operandStack.Push(result); } else { double operand = double.Parse(token); operandStack.Push(operand); } } return operandStack.Pop(); } private bool IsOperator(string token) { return token == "+" || token == "-" || token == "*" || token == "/"; } private double Evaluate(string op, double operand1, double operand2) { switch (op) { case "+": return operand1 + operand2; case "-": return operand1 - operand2; case "*": return operand1 * operand2; case "/": return operand1 / operand2; default: throw new ArgumentException("Invalid operator: " + op); } } ``` 需要注意的是,这只是一个简单实现,没有考虑操作符优先级和括号等情况。如果需要实现更完整的四则运算,需要进一步完善算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值