C# 利用dt.Compute计算表达式的值

代码的功能是计算表达式。

比如

true and false and true


使用的是dataset里面的dt.Compute函数。

也是在网上看到还有这种用法。

这样计算表达式的值就不用自己写算法了。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        public enum MotionFlowItemLogicSymbolEnum
        {
            no = 0, and, or
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Queue<bool> operationData = new Queue<bool>();
            Queue<MotionFlowItemLogicSymbolEnum> operationSymbol = new Queue<MotionFlowItemLogicSymbolEnum>();

            //IO,a,低电平,与
            //IO,b,低电平,无

            operationData.Enqueue(true);
            operationData.Enqueue(false);
            operationData.Enqueue(true);
            operationSymbol.Enqueue(MotionFlowItemLogicSymbolEnum.and);
            operationSymbol.Enqueue(MotionFlowItemLogicSymbolEnum.or);

            bool res = false;
            var dt = new DataTable();
            var list1 = new List<bool>();
            try
            {
                if (operationData.Count == 1)
                {
                    res = operationData.Dequeue();
                    return;
                }
                while (true)
                {
                    if (operationData.Count < 1)
                    {
                        if (list1.Count > 0)
                        {
                            throw new ArgumentException("运算数据数量不对!");
                        }
                        break;
                    }
                    list1.Add(operationData.Dequeue());
                    if (list1.Count == 2)
                    {
                        if (operationSymbol.Count < 1)
                        {
                            throw new ArgumentException("逻辑运算符数量不对!");
                        }
                        res = (bool)dt.Compute($"{list1[0].ToString()} {operationSymbol.Dequeue().ToString()} {list1[1].ToString()}", "");

                        list1.Clear();
                        if (operationData.Count > 0)
                        {
                            list1.Add(res);
                        }
                    }
                }
                MessageBox.Show(res.ToString());
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           

        }
    }
}



--------------------- 

作者:hackpig

来源:www.skcircle.com

版权声明:本文为博主原创文章,转载请附上博文链接!



本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年5月    »
12345
6789101112
13141516171819
20212223242526
2728293031
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864