Пример #1
0
 public override Block Reduce(Dictionary <string, double> unknown)
 {
     if (unknown != null && unknown.ContainsKey(name))
     {
         return(ConstBlock.Create(unknown[name]));
     }
     return(this);
 }
Пример #2
0
        public override Block Reduce(Dictionary <string, double> unknown)
        {
            var blockA = a.Reduce(unknown);

            if (blockA.IsConst())
            {
                return(ConstBlock.Create(Calculate(unknown)));
            }
            return(Create(blockA, type));
        }
Пример #3
0
 public static Block Pow(this Block block, double a)
 {
     return(TwoOperatorBlock.Create(block, ConstBlock.Create(a), TwoOperatorType.Pow));
 }
Пример #4
0
 public static Block Divide(this Block block, double a)
 {
     return(TwoOperatorBlock.Create(block, ConstBlock.Create(a), TwoOperatorType.Divide));
 }
Пример #5
0
 public static Block Multiply(this Block block, double a)
 {
     return(TwoOperatorBlock.Create(block, ConstBlock.Create(a), TwoOperatorType.Multiply));
 }
Пример #6
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)));
        }