⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datatypecheck.cs

📁 字符串表达式的计算。本程序是一个Window Forms应用程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace EvaluationEngine.Support
{
    public class DataTypeCheck
    {

        public static string[] ReservedWords = { "int", "long", "float", "decimal", "currency", "date", "value", "while", "for", "do", "break", "continue", "foreach", "next", "else", "end", "endif", "string", "text", "char", "list", "rule", "expression", "function", "macro", "express", "int", "integer", "list", "sub", "set", ":=" };
        public static string[] OperandFunctions = { "avg", "abs", "iif", "lcase", "left", "len", "mid", "right", "round", "sqrt", "ucase", "isnullorempty", "istrueornull", "isfalseornull", "trim", "rtrim", "ltrim", "dateadd", "concat", "date", "rpad", "lpad", "join", "searchstring", "day", "month", "year", "substring", "numericmax", "numericmin", "datemax", "datemin", "stringmax", "stringmin", "contains", "between", "indexof", "now", "replace", "eval", "remove", "quote", "pcase", "sin", "cos", "not", "isalldigits" };
        public static string[] ArithOperators = { "^", "*", "/", "%", "+", "-" };
        public static string[] LogicalOperators = { "and", "or" };
        public static string[] ComparisonOperators = { "<", "<=", ">", ">=", "<>", "=" };
        public static string[] AssignmentOperators = { ":=" };


        /// <summary>
        /// All digits....no decimals, commas, or -
        /// </summary>
        /// <param name="CheckString"></param>
        /// <returns></returns>
        public static bool IsAllDigits(string CheckString)
        {
            if (String.IsNullOrEmpty(CheckString) == true) return false;

            CheckString = CheckString.Trim();

            bool allDigits = true;
            foreach (char c in CheckString)
            {
                if (Char.IsDigit(c) == false)
                {
                    allDigits = false;
                    break;
                }
            }

            return allDigits;
        }

        /// <summary>
        /// The number of '.' in a string
        /// </summary>
        /// <param name="CheckString"></param>
        /// <returns></returns>
        public static int DecimalCount(string CheckString)
        {
            if (String.IsNullOrEmpty(CheckString) == true) return 0;

            CheckString = CheckString.Trim();

            int decimalCount = 0;
            foreach (char c in CheckString)
            {
                if (c == '.') decimalCount++;
            }

            return decimalCount;
        }


        /// <summary>
        /// Text items are surronded by strings.
        /// </summary>
        /// <param name="CheckString"></param>
        /// <returns></returns>
        public static bool IsText(string CheckString)
        {
            if (String.IsNullOrEmpty(CheckString) == true) return false;

            CheckString = CheckString.Trim();

            if (CheckString.Length == 1) return false;
            if (CheckString.StartsWith("\"") == false) return false;
            if (CheckString.EndsWith("\"") == false) return false;

            return true;
        }


        /// <summary>
        /// integers have all digits and no decimals.  they can also start with -
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        public static bool IsInteger(string CheckString)
        {
            if (String.IsNullOrEmpty(CheckString) == true) return false;

            CheckString = CheckString.Trim();
            
            bool isInteger = true;  // assume the value is an integer
            int intPosition = 0; // the current position we are checking

            foreach (char c in CheckString)
            {
                if (Char.IsNumber(c) == false)
                {
                    if (c != '-')
                    {
                        isInteger = false;
                        break;
                    }
                    else if (intPosition != 0)
                    {
                        isInteger = false;
                        break;
                    }
                }

                intPosition++;
            }

            return isInteger;
        }

        public static bool IsDate(string CheckString)
        {

            if (String.IsNullOrEmpty(CheckString) == true) return false;
            CheckString = CheckString.Trim();            

            DateTime d = DateTime.MinValue;
            return DateTime.TryParse(CheckString, out d);
        }

        public static bool IsDouble(string CheckString)
        {

            if (String.IsNullOrEmpty(CheckString) == true) return false;

            CheckString = CheckString.Trim();
            

            bool isDouble = true; // assume the item is a double
            int decimalCount = 0; // the number of decimals

            int intPosition = 0;

            foreach (char c in CheckString)
            {
                if (Char.IsNumber(c) == false)
                {
                    if (c == '.')
                    {
                        decimalCount++;
                    }
                    else if ((c == '-') && (intPosition == 0))
                    {
                        // this is valid, keep going
                    }
                    else
                    {
                        isDouble = false;
                        break;
                    }
                }

                intPosition++;
            }

            if (isDouble == true)
            {
                // make sure there is only 1 decimal

                if (decimalCount <= 1)
                    return true;
                else
                    return false;

            }
            else
                return false;
        }

        public static bool IsReservedWord(string OperandText)
        {
            bool isReservedWord = false;
            if (String.IsNullOrEmpty(OperandText) == true) return false;
            OperandText = OperandText.Trim().ToLower();
            if (String.IsNullOrEmpty(OperandText) == true) return false;

            for (int i = 0; i < ReservedWords.Length; i++)
            {
                if (OperandText == ReservedWords[i])
                {
                    isReservedWord = true;
                    break;
                }
            }

            return isReservedWord;
        }

        public static bool AnyPuncuation(string Text, out Char PuncMark)
        {
            PuncMark = ' ';
            foreach (char c in Text)
            {
                if (Char.IsPunctuation(c) == true)
                {
                    PuncMark = c;
                    return true;
                }
            }

            return false;
        }

        public static bool IsOperandFunction(string OperandText)
        {
            if (String.IsNullOrEmpty(OperandText) == true) return false;

            OperandText = OperandText.Trim().ToLower();

            bool isOperandFunnction = false;

            for (int i = 0; i < OperandFunctions.Length; i++)
            {
                if (OperandText == OperandFunctions[i])
                {
                    isOperandFunnction = true;
                    break;
                }
            }

            return isOperandFunnction;
        }

        public static bool IsOperator(string OperatorText)
        {

            if (String.IsNullOrEmpty(OperatorText) == true) return false;

            OperatorText = OperatorText.Trim().ToLower();
            bool isOperator = false;

            for (int i = 0; i < ArithOperators.Length; i++)
            {
                if (OperatorText == ArithOperators[i])
                {
                    isOperator = true;
                    break;
                }
            }
            if (isOperator == true) return isOperator;



            for (int i = 0; i < LogicalOperators.Length; i++)
            {
                if (OperatorText == LogicalOperators[i])
                {
                    isOperator = true;
                    break;
                }
            }
            if (isOperator == true) return isOperator;



            for (int i = 0; i < ComparisonOperators.Length; i++)
            {
                if (OperatorText == ComparisonOperators[i])
                {
                    isOperator = true;
                    break;
                }
            }
            if (isOperator == true) return isOperator;


            for (int i = 0; i < AssignmentOperators.Length; i++)
            {
                if (OperatorText == AssignmentOperators[i])
                {
                    isOperator = true;
                    break;
                }
            }
            if (isOperator == true) return isOperator;


            return false;
        }

        public static bool IsBoolean(string CheckString)
        {
            if (String.IsNullOrEmpty(CheckString) == true) return false;

            CheckString = CheckString.Trim().ToLower();

            if (CheckString == "true") return true;
            if (CheckString == "false") return true;

            return false;
                    
        }

        public static string RemoveTextQuotes(string CheckString)
        {
            if (IsText(CheckString) == false) return CheckString;

            return CheckString.Substring(1, CheckString.Length - 2);


        }

        public static void FunctionDescription(string FunctionName, out string Syntax, out string Description, out string Example)
        {
            Syntax = "Unknown function";
            Description = "";
            Example = "";
                
            switch (FunctionName.Trim().ToLower())
            {
                case "cos":
                    Description = "Calculates the cosine of a number.";
                    Syntax = "cos[p1] where p1 can be converted to doubles.";
                    Example = "cos[90] < 0";
                    break;

                case "avg":
                    Description = "Calculates the average of a list of numbers.  The list items must be able to convert to doubles.";
                    Syntax = "avg[p1, ..., pn] where p1,...,pn can be converted to doubles.";
                    Example = "avg[1, 2, 3] = 2";
                    break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -