コード例 #1
0
ファイル: UnitOfMeasure.cs プロジェクト: point85/CaliperSharp
            // add a UOM and exponent pair to the map of reduced Terms
            private void AddTerm(UnitOfMeasure uom, bool invert)
            {
                int unitPower = 1;
                int power     = 0;

                if (!invert)
                {
                    // get existing power
                    if (!Terms.ContainsKey(uom))
                    {
                        // add first time
                        power = unitPower;
                    }
                    else
                    {
                        // increment existing power
                        if (!uom.Equals(MeasurementSystem.GetSystem().GetOne()))
                        {
                            power = Terms[uom] + unitPower;
                        }
                    }
                }
                else
                {
                    // denominator with negative powers
                    if (!Terms.ContainsKey(uom))
                    {
                        // add first time
                        power = -unitPower;
                    }
                    else
                    {
                        // decrement existing power
                        if (!uom.Equals(MeasurementSystem.GetSystem().GetOne()))
                        {
                            power = Terms[uom] - unitPower;
                        }
                    }
                }

                if (power == 0)
                {
                    Terms.Remove(uom);
                }
                else
                {
                    if (!uom.Equals(MeasurementSystem.GetSystem().GetOne()))
                    {
                        Terms[uom] = power;
                    }
                }
            }
コード例 #2
0
ファイル: UnitOfMeasure.cs プロジェクト: point85/CaliperSharp
            // compose the base symbol
            internal String BuildBaseString()
            {
                StringBuilder numerator   = new StringBuilder();
                StringBuilder denominator = new StringBuilder();

                int numeratorCount   = 0;
                int denominatorCount = 0;

                // sort units by symbol (ascending)
                SortedDictionary <UnitOfMeasure, int> keys = new SortedDictionary <UnitOfMeasure, int>(Terms);

                foreach (KeyValuePair <UnitOfMeasure, int> pair in keys)
                {
                    UnitOfMeasure unit  = pair.Key;
                    int           power = pair.Value;

                    if (power < 0)
                    {
                        // negative, put in denominator
                        if (denominator.Length > 0)
                        {
                            denominator.Append(MULT);
                        }

                        if (!unit.Equals(MeasurementSystem.GetSystem().GetOne()))
                        {
                            denominator.Append(unit.Symbol);
                            denominatorCount++;
                        }

                        if (power < -1)
                        {
                            if (power == -2)
                            {
                                denominator.Append(SQ);
                            }
                            else if (power == -3)
                            {
                                denominator.Append(CUBED);
                            }
                            else
                            {
                                denominator.Append(POW).Append(Math.Abs(power));
                            }
                        }
                    }
                    else if (power >= 1 && !unit.Equals(MeasurementSystem.GetSystem().GetOne()))
                    {
                        // positive, put in numerator
                        if (numerator.Length > 0)
                        {
                            numerator.Append(MULT);
                        }

                        numerator.Append(unit.Symbol);
                        numeratorCount++;

                        if (power > 1)
                        {
                            if (power == 2)
                            {
                                numerator.Append(SQ);
                            }
                            else if (power == 3)
                            {
                                numerator.Append(CUBED);
                            }
                            else
                            {
                                numerator.Append(POW).Append(power);
                            }
                        }
                    }
                    else
                    {
                        // unary, don't add a '1'
                    }
                }

                if (numeratorCount == 0)
                {
                    numerator.Append(ONE_CHAR);
                }

                String result = null;

                if (denominatorCount == 0)
                {
                    result = numerator.ToString();
                }
                else
                {
                    if (denominatorCount == 1)
                    {
                        result = numerator.Append(DIV).Append(denominator).ToString();
                    }
                    else
                    {
                        result = numerator.Append(DIV).Append(LP).Append(denominator).Append(RP).ToString();
                    }
                }

                return(result);
            }             // end unit of measure iteration