Beispiel #1
0
        private void ReadParams()
        {
            if (File.Exists(WCFParams))
            {
                IniFile Ini = new IniFile(WCFParams);

                nDaysCount = Int32.Parse(Ini.Read("all", "nDaysCount"));

                sStartingDate = Ini.Read("all", "sStartingDate");

                nHoursBeforeEat = Int32.Parse(Ini.Read("all", "nHoursBeforeEat"));

                nEatPeriod = Int32.Parse(Ini.Read("all", "nEatPeriod"));

                dWeights = new Double[nDaysCount];

                string localDate = sStartingDate;

                for (int i = 0; i < nDaysCount; i++)
                {
                    //dWeights[i] = Double.Parse(Ini.Read("all", localDate));

                    Double.TryParse(Ini.Read("all", localDate), out dWeights[i]);

                    localDate = NextDay(localDate);
                }
            }
            else
            {
                var myFile = File.Create(WCFParams);

                myFile.Close();


                IniFile Ini = new IniFile(WCFParams);

                Ini.Write("all", "nDaysCount", nDaysCount.ToString());
                Ini.Write("all", "sStartingDate", sStartingDate);
                Ini.Write("all", "nHoursBeforeEat", nHoursBeforeEat.ToString());
                Ini.Write("all", "nEatPeriod", nEatPeriod.ToString());

                dWeights = new Double [nDaysCount];

                dWeights[0] = 0;

                for (int i = 1; i < nDaysCount; i++)
                {
                    dWeights[i] = 0;
                }

                string localDate = sStartingDate;

                for (int i = 0; i < nDaysCount; i++)
                {
                    Ini.Write("all", localDate, dWeights[i].ToString());

                    localDate = NextDay(localDate);
                }
            }
        }
Beispiel #2
0
 public static bool TryParse(string s, out Vector4D result)
 {
     if (s == null)
     {
         result = Zero;
         return(false);
     }
     string[] vals = ParseHelper.SplitStringVector(s);
     if (vals.Length != Count)
     {
         result = Zero;
         return(false);
     }
     if (Scalar.TryParse(vals[0], out result.X) &&
         Scalar.TryParse(vals[1], out result.Y) &&
         Scalar.TryParse(vals[2], out result.Z) &&
         Scalar.TryParse(vals[3], out result.W))
     {
         return(true);
     }
     else
     {
         result = Zero;
         return(false);
     }
 }
Beispiel #3
0
        private void ListWeightElem_LostFocus(object sender, RoutedEventArgs e)
        {
            Control ctrl = sender as Control;

            if (ctrl != null)
            {
                // Get the control name
                string sName = ctrl.Name;

                int iIndex = Int32.Parse(sName.Substring(1));

                Double.TryParse(iItemsW[iIndex].Text, out dWeights[iIndex]);

                if (dWeights[iIndex] == 0)
                {
                    iItemsW[iIndex].Text = "";
                }
            }
        }
Beispiel #4
0
 public static bool TryParse(string s, out Clamped result)
 {
     if (s != null)
     {
         string[] vals = s.Split(new char[] { '<', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
         if (vals.Length == 3)
         {
             Scalar min, value, max;
             if (Scalar.TryParse(vals[0], out min) &&
                 Scalar.TryParse(vals[1], out value) &&
                 Scalar.TryParse(vals[2], out max))
             {
                 result = new Clamped(value, min, max);
                 return(true);
             }
         }
     }
     result = null;
     return(false);
 }
 public static bool TryParse <T>(string s, ref T valueType, string leftParenth, string rightParenth, char divider)
     where T : IAdvanceValueType
 {
     if (s == null)
     {
         return(false);
     }
     string[] parts = SpritString(s, divider, new string[] { leftParenth, rightParenth });
     if (parts.Length != valueType.Count)
     {
         return(false);
     }
     Scalar[] result = new Scalar[valueType.Count];
     for (int index = 0; index < valueType.Count; ++index)
     {
         if (!Scalar.TryParse(parts[index], out result[index]))
         {
             return(false);
         }
     }
     valueType.CopyFrom(result, 0);
     return(true);
 }
Beispiel #6
0
        private static Node ParseGroup(IEnumerator <Token> lexics)
        {
            // eliminating subgroups
            var expressionNodes = new LinkedList <object>();

            do
            {
                Token token = lexics.Current;
                switch (token.Type)
                {
                case TokenType.Literal:
                    if (NumberType.TryParse(token.Value, out NumberType value))
                    {
                        expressionNodes.AddLast(new NumberNode(value));
                    }
                    else
                    {
                        throw new SyntacticException(token, "Incorrect literal " + token);
                    }
                    break;

                case TokenType.ConstantName:
                    expressionNodes.AddLast(new NumberNode(Constants.Mathematical[token.Value]));
                    break;

                case TokenType.Variable:
                    expressionNodes.AddLast(new VariableNode(token.Value));
                    break;

                case TokenType.OpeningParenthesis:

                    if (!lexics.MoveNext())
                    {
                        throw new SyntacticException(token, "Expression expected");
                    }

                    expressionNodes.AddLast(ParseGroup(lexics));

                    if (lexics.Current == null || lexics.Current.Type != TokenType.ClosingParenthesis)
                    {
                        throw new SyntacticException(token, "Closing paranthesis expected");
                    }

                    break;

                case TokenType.FunctionName:

                    if (!lexics.MoveNext() || lexics.Current.Type != TokenType.OpeningParenthesis)
                    {
                        throw new SyntacticException(token, "Argument list expected");
                    }

                    Token lastTokenBeforeArgument = lexics.Current;
                    var   arguments = new List <Node>();
                    while (lexics.Current.Type == TokenType.OpeningParenthesis ||
                           lexics.Current.Type == TokenType.Comma)
                    {
                        if (!lexics.MoveNext())
                        {
                            throw new SyntacticException(lastTokenBeforeArgument, "Argument expected");
                        }

                        arguments.Add(ParseGroup(lexics));
                        lastTokenBeforeArgument = lexics.Current;
                    }
                    if (lexics.Current == null || lexics.Current.Type != TokenType.ClosingParenthesis)
                    {
                        throw new SyntacticException(token, "Closing paranthesis expected");
                    }

                    int expectedParamsCount = Constants.Functional[token.Value].GetParameters().Length;
                    if (arguments.Count != expectedParamsCount)
                    {
                        throw new SyntacticException(token, $"{expectedParamsCount} parameters expected, got {arguments.Count}");
                    }

                    expressionNodes.AddLast(new FunctionNode(token.Value, arguments));
                    break;

                case TokenType.Operator:
                    expressionNodes.AddLast(token);
                    break;

                default:
                    throw new SyntacticException(token, "Unexpected token");
                }
            } while (lexics.MoveNext() &&
                     lexics.Current.Type != TokenType.ClosingParenthesis &&
                     lexics.Current.Type != TokenType.Comma);

            // eliminate power operations
            for (var i = expressionNodes.First; i != null; i = i.Next)
            {
                Token current;
                if ((current = i.Value as Token) != null && current.Value == "^")
                {
                    bool rightSignIsNegative = false;
                    Node left, right;
                    if (i.Previous == null || (left = i.Previous.Value as Node) == null)
                    {
                        throw new SyntacticException(current, "Left expression expected");
                    }

                    while (true)
                    {
                        var   j = i.Next;
                        Token signToken;
                        if ((signToken = j.Value as Token) != null)
                        {
                            if (signToken.Value == "+")
                            {
                                expressionNodes.Remove(j);
                                continue;
                            }
                            if (signToken.Value == "-")
                            {
                                rightSignIsNegative = !rightSignIsNegative;
                                expressionNodes.Remove(j);
                                continue;
                            }
                        }
                        else if ((right = j.Value as Node) != null)
                        {
                            expressionNodes.Remove(j);
                            break;
                        }
                        throw new SyntacticException(current, "Right expression expected");
                    }

                    if (rightSignIsNegative)
                    {
                        right = new NegationNode(right);
                    }

                    var newI = i.Previous;
                    newI.Value = new BinaryOperationNode(left, right, BinaryOperation.Pow);
                    expressionNodes.Remove(i);
                    i = newI;
                }
            }

            // eliminate multiplication groups (like "2x" = "2*x" or "1 x 2" = "1*x*2")
            for (var i = expressionNodes.First; i != null; i = i.Next)
            {
                Node current, next;
                if ((current = i.Value as Node) != null)
                {
                    var j = i.Next;
                    while (j != null && (next = j.Value as Node) != null)
                    {
                        current = new BinaryOperationNode(current, next, BinaryOperation.Mul);
                        var nextJ = j.Next;
                        expressionNodes.Remove(j);
                        j = nextJ;
                    }
                    i.Value = current;
                }
            }

            // eliminate multiplication operations
            for (var i = expressionNodes.First; i != null; i = i.Next)
            {
                Token current;
                if ((current = i.Value as Token) != null && (current.Value == "*" || current.Value == "/"))
                {
                    bool rightSignIsNegative = false;
                    Node left, right;
                    if (i.Previous == null || (left = i.Previous.Value as Node) == null)
                    {
                        throw new SyntacticException(current, "Left expression expected");
                    }

                    while (true)
                    {
                        var   j = i.Next;
                        Token signToken;
                        if ((signToken = j.Value as Token) != null)
                        {
                            if (signToken.Value == "+")
                            {
                                expressionNodes.Remove(j);
                                continue;
                            }
                            if (signToken.Value == "-")
                            {
                                rightSignIsNegative = !rightSignIsNegative;
                                expressionNodes.Remove(j);
                                continue;
                            }
                        }
                        else if ((right = j.Value as Node) != null)
                        {
                            expressionNodes.Remove(j);
                            break;
                        }
                        throw new SyntacticException(current, "Right expression expected");
                    }

                    if (rightSignIsNegative)
                    {
                        right = new NegationNode(right);
                    }

                    var newI = i.Previous;
                    newI.Value = new BinaryOperationNode(left, right,
                                                         current.Value == "*" ? BinaryOperation.Mul : BinaryOperation.Div);
                    expressionNodes.Remove(i);
                    i = newI;
                }
            }

            // eliminate additive operations
            int sign = 0;

            for (var i = expressionNodes.First; i != null;)
            {
                Token token;
                if ((token = i.Value as Token) != null)
                {
                    if (sign == 0)
                    {
                        sign = 1;
                    }
                    if (token.Value == "-")
                    {
                        sign = -sign;
                    }
                    var newI = i.Next;
                    expressionNodes.Remove(i);
                    i = newI;
                }
                else
                {
                    Node right = (Node)i.Value;

                    if (i == expressionNodes.First)
                    {
                        i.Value = sign == -1 ? new NegationNode(right) : right;
                        i       = i.Next;
                    }
                    else
                    {
                        expressionNodes.First.Value = new BinaryOperationNode((Node)expressionNodes.First.Value, right,
                                                                              sign == -1 ? BinaryOperation.Sub : BinaryOperation.Add);
                        var newI = i.Next;
                        expressionNodes.Remove(i);
                        i = newI;
                    }

                    sign = 0;
                }
            }
            if (sign != 0)
            {
                throw new SyntacticException(message: "Expression is expected in the end of text");
            }

            if (expressionNodes.Count != 1)
            {
                throw new NotImplementedException();
            }

            return((Node)expressionNodes.First.Value);
        }
Beispiel #7
0
        /// <summary>转为浮点数</summary>
        /// <param name="value">待转换对象</param>
        /// <param name="defaultValue">默认值。待转换对象无效时使用</param>
        /// <returns></returns>
        public virtual Double ToDouble(Object value, Double defaultValue)
        {
            if (value == null || value == DBNull.Value)
            {
                return(defaultValue);
            }

            // 特殊处理字符串,也是最常见的
            if (value is String str)
            {
                str = ToDBC(str).Trim();
                if (str.IsNullOrEmpty())
                {
                    return(defaultValue);
                }

                if (Double.TryParse(str, out var n))
                {
                    return(n);
                }
                return(defaultValue);
            }
            else if (value is Byte[] buf)
            {
                if (buf == null || buf.Length < 1)
                {
                    return(defaultValue);
                }

                switch (buf.Length)
                {
                case 1:
                    return(buf[0]);

                case 2:
                    return(BitConverter.ToInt16(buf, 0));

                case 3:
                    return(BitConverter.ToInt32(new Byte[] { buf[0], buf[1], buf[2], 0 }, 0));

                case 4:
                    return(BitConverter.ToInt32(buf, 0));

                default:
                    // 凑够8字节
                    if (buf.Length < 8)
                    {
                        var bts = new Byte[8];
                        Buffer.BlockCopy(buf, 0, bts, 0, buf.Length);
                        buf = bts;
                    }
                    return(BitConverter.ToDouble(buf, 0));
                }
            }

            try
            {
                return(Convert.ToDouble(value));
            }
            catch { return(defaultValue); }
        }
        /// <summary>
        /// String to Double(字符串 转换成 数值、浮点)
        /// </summary>
        /// <remarks>
        ///  2014-06-23 16:31 Created By iceStone
        /// </remarks>
        /// <param name="s">String</param>
        /// <param name="def">Default</param>
        /// <returns>Byte</returns>
        public static double ToDouble(this string s, double def = default(double))
        {
            double result;

            return(Double.TryParse(s, out result) ? result : def);
        }