Exemplo n.º 1
0
 public static Block Cos(this Block block)
 {
     return(FunctionBlock.Create(block, FunctionType.Cos));
 }
Exemplo n.º 2
0
 public static Block Tan(this Block block)
 {
     return(FunctionBlock.Create(block, FunctionType.Tan));
 }
Exemplo n.º 3
0
        public static Block Parse(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return(null);
            }
            str = str.Trim();

            foreach (var item in TwoOperatorBlock.priority)
            {
                if (str.IndexOfAny(item.ToCharArray()) != -1)
                {
                    List <string> subjects   = new List <string>();
                    List <char>   oper       = new List <char>();
                    int           lastIndex  = 0;
                    int           startIndex = 0;
                    int           Index      = 0;
                    string        sub;
                    int           bracket = 0;
                    bool          success = false;

                    while ((Index = str.IndexOfAny(item.ToCharArray(), startIndex)) != -1)
                    {
                        sub = str.Substring(startIndex, Index - startIndex);

                        //괄호가 있으면
                        bracket += sub.Count(e => e == '(');
                        bracket -= sub.Count(e => e == ')');

                        if (bracket == 0)
                        {
                            subjects.Add(str.Substring(lastIndex, Index - lastIndex));
                            oper.Add(str[Index]);
                            success = true;

                            lastIndex = Index + 1;
                        }
                        startIndex = Index + 1;
                    }
                    sub = str.Substring(startIndex, str.Length - startIndex);
                    //괄호가 있으면
                    bracket += sub.Count(e => e == '(');
                    bracket -= sub.Count(e => e == ')');

                    if (bracket == 0)
                    {
                        subjects.Add(str.Substring(lastIndex, str.Length - lastIndex));
                    }
                    else
                    {
                        throw new ArgumentException();
                    }

                    if (success)
                    {
                        Block block = Parse(subjects[0]);
                        for (int i = 0; i < oper.Count; i++)
                        {
                            char op = oper[i];
                            block = ParseConcat(block, Parse(subjects[i + 1]), op);
                        }
                        return(block);
                    }
                }
            }

            if (str.EndsWith(")"))
            {
                string st = str.ToLower();

                if (st.StartsWith("("))
                {
                    return /*BracketBlock.Create(*/ (Block.Parse(str.Substring(1, str.Length - 2)) /*)*/);
                }
                else
                {
                    Dictionary <FunctionType, string> funcName = new Dictionary <FunctionType, string>()
                    {
                        { FunctionType.Sin, "sin" },
                        { FunctionType.Cos, "cos" },
                        { FunctionType.Tan, "tan" },
                        { FunctionType.Root, "root" },
                    };
                    foreach (var item in funcName)
                    {
                        if (st.StartsWith(item.Value + "("))
                        {
                            string       a    = str.Substring(item.Value.Length + 1, str.Length - (2 + item.Value.Length));
                            FunctionType type = item.Key;
                            return(FunctionBlock.Create(Parse(a), type));
                        }
                    }
                }
                throw new ArgumentException();
            }

            if (char.IsLetter(str[0]) && !str.Any(e => char.IsWhiteSpace(e)))
            {
                return(UnknownBlock.Create(str));
            }

            return(ConstBlock.Create(Convert.ToDouble(str)));
        }