C#实现根据输入的代数式,计算代数式的结果

简易界面如下:
在这里插入图片描述
代码如下:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace yunsuan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            #region 利用datetable的compute指令
            //DataTable dt = new DataTable();      
            //var Result = dt.Compute(textBox1.Text, "");//将运算字符串转换成表达式运算
            //label_Result.Text = Result.ToString();
            #endregion
            #region 利用栈
            Stack<float> s_data = new Stack<float>();
            Stack<char> s_opera = new Stack<char>();
            String InputExpression = textBox1.Text;
            //String InputExpression = "120+205+3*(4-2)";
            int i;
            int temp = 0;

            int priority(char c)    //得到每个符号的优先级
            {
                int pr = 0;
                switch (c)
                {
                    case '+':
                    case '-': pr = 1; break;
                    case '*':
                    case '/': pr = 2; break;
                    case '(': pr = 3; break;
                    case ')': pr = 0; break;
                }
                return pr;
            }


            bool is_digital(char d)     //判断是否为数字
            {
                if (d >= '0' && d <= '9')
                    return true;
                else
                    return false;
            }

            void execution()    //将数字栈的前两个数字进行运算,并将结果压栈
            {
                float temp1, temp2;
                char operation;
                temp2 = s_data.Pop();
                temp1 = s_data.Pop();
                operation = s_opera.Pop();
                switch (operation)
                {
                    case '+':
                        s_data.Push(temp1 + temp2);
                        break;
                    case '-':
                        s_data.Push(temp1 - temp2);
                        break;
                    case '*':
                        s_data.Push(temp1 * temp2);
                        break;
                    case '/':
                        s_data.Push(temp1 / temp2);
                        break;
                }
            }

            for (i = 0; i < InputExpression.Length; i++)
            {
                if (is_digital(InputExpression[i]))   //如果字符为数字
                {
                    if (temp == 0)
                        temp = InputExpression[i] - '0';  //将字符转换为数字
                    else
                        temp = temp * 10 + (InputExpression[i] - '0'); //将数字转换为多位数
                    if (i == InputExpression.Length - 1)   //将末尾的最后一个数字压进数字栈
                        s_data.Push(temp);
                }


                else
                {
                    //如果碰到字符了,首先把前一个数字压入数字栈
                    if (temp != 0)
                    {
                        s_data.Push(temp);
                        temp = 0;
                    }

                    if (s_opera.Count == 0)   //如果栈s_opera的元素个数为零
                    {
                        s_opera.Push(InputExpression[i]);  //把运算符存入栈s_opera中
                    }

                    else     //如果栈s_opera中已经有了运算符
                    {
                        int pr_top = priority(s_opera.Peek());  //栈顶运算符的优先级
                        if ((priority(InputExpression[i]) > pr_top) || (pr_top == 3 && priority(InputExpression[i]) != 0))    //如果当前运算符优先级大于栈顶运算符,或者栈顶运算符优先级为3且当前运算符优先级不为0
                            s_opera.Push(InputExpression[i]);   //将当前运算符存入栈s_opera中

                        else if (pr_top == 3 && priority(InputExpression[i]) == 0)  //当右括号遇到左括号
                        {
                            s_opera.Pop();
                            continue;   //考虑下一个字符
                        }

                        else
                        {
                            execution();
                            i = i - 1;

                        }

                    }
                }
            }
            for (int j = s_opera.Count; j > 0; j--)
            {
                execution();
            }
            Result.Text = Convert.ToString(s_data.Pop());

            #endregion

            #region 利用递归
            //DataTable dt = new DataTable();      
            //var Result = dt.Compute(textBox1.Text, "");//将运算字符串转换成表达式运算
            //label_Result.Text = Result.ToString();
            #endregion
        }
    }
}

另外还有一种简单的方法,就是利用datetable的compute指令,代码如下:

DataTable dt = new DataTable();      
var Result = dt.Compute(textBox1.Text, "");//将运算字符串转换成表达式运算
label_Result.Text = Result.ToString();
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值