예제 #1
0
 public void compare_x_less_then_y(string x,string y)
 {
     var migrationA = new Migration {Number = x};
     var migrationB = new Migration {Number = y};
     var comparer = new NumberComparer();
     Assert.AreEqual(-1, comparer.Compare(migrationA, migrationB));
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter numbers, separated by a comma: ");
            var numbers = Console.ReadLine()
                .Split(new char[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => int.Parse(x))
                .OrderBy(x => x)
                .ToList();

            Console.Write("Enter number K: ");
            int maxNum = int.Parse(Console.ReadLine());

            NumberComparer comparer = new NumberComparer();
            int index = numbers.BinarySearch(maxNum, comparer);

            if (index >= 0)
                Console.WriteLine(numbers[index]);
            else
            {
                index = Math.Abs(index) - 2;
                if (index >= 0)
                    Console.WriteLine(numbers[index]);
                else
                    Console.WriteLine("No numbers less than/equal to {0} were found!", maxNum);
            }
        }
예제 #3
0
        public int Compare(IndexValue x, IndexValue y)
        {
            if (x.Type == y.Type)
            {
                switch (x.Type)
                {
                case IndexValueType.Null:
                    return(0);

                case IndexValueType.DateTime:
                    return(DateTimeComparer.Compare((DateTime)x.Value, (DateTime)y.Value));

                case IndexValueType.Integer:
                    return(IntegerComparer.Compare((Int32)x.Value, (Int32)y.Value));

                case IndexValueType.Number:
                    return(NumberComparer.Compare((double)x.Value, (double)y.Value));

                case IndexValueType.String:
                    return(StringComparer.Compare((string)x.Value, (string)y.Value));

                case IndexValueType.Bool:
                    return(BoolComparer.Compare((bool)x.Value, (bool)y.Value));

                default:
                    Verify.Argument(true, "Attempt to compare illegal values, {0} and {1}", x.Value, y.Value);
                    throw null;
                }
            }
            return(Comparer <int> .Default.Compare((int)x.Type, (int)y.Type));
        }
        public void When_CompareNumbers_Then_Success()
        {
            var mainNumber = new Number(6, 8, 0, 1);
            var numberToCompare1 = new Number(1, 8, 9, 4);
            var numberToCompare2 = new Number(6, 8, 0, 1);
            var numberToCompare3 = new Number(1, 0, 6, 2);
            var numberComparer = new NumberComparer();

            var attemptResult1 = numberComparer.Compare(mainNumber, numberToCompare1);
            var attemptResult2 = numberComparer.Compare(mainNumber, numberToCompare2);
            var attemptResult3 = numberComparer.Compare(mainNumber, numberToCompare3);

            Assert.AreEqual(numberToCompare1, attemptResult1.Number);
            Assert.AreEqual(1, attemptResult1.Goods);
            Assert.AreEqual(1, attemptResult1.Regulars);
            Assert.AreEqual(2, attemptResult1.Bads);

            Assert.AreEqual(numberToCompare2, attemptResult2.Number);
            Assert.AreEqual(4, attemptResult2.Goods);
            Assert.AreEqual(0, attemptResult2.Regulars);
            Assert.AreEqual(0, attemptResult2.Bads);

            Assert.AreEqual(numberToCompare3, attemptResult3.Number);
            Assert.AreEqual(0, attemptResult3.Goods);
            Assert.AreEqual(3, attemptResult3.Regulars);
            Assert.AreEqual(1, attemptResult3.Bads);
        }
예제 #5
0
        public void TestSpecificExampleA()
        {
            double a = 0.06 - 0.01;
            double b = 0.05;

            Assert.IsFalse(a == b);
            Assert.AreEqual(0, NumberComparer.Compare(a, b));
        }
예제 #6
0
        public void TestSpecificExampleB()
        {
            double a = 1 + 1.0028 - 0.9973;
            double b = 1.0055;

            Assert.IsFalse(a == b);
            Assert.AreEqual(0, NumberComparer.Compare(a, b));
        }
예제 #7
0
            public ListViewColumnSorter()
            {
                // Initialize the column to '0'
                ColumnToSort = 0;

                // Initialize the sort order to 'none'
                OrderOfSort = SortOrder.None;

                stringComparer   = new CaseInsensitiveComparer();
                severityComparer = new SeverityComparer();
                numberComparer   = new NumberComparer();
            }
예제 #8
0
파일: DStarRunner.cs 프로젝트: twxstar/npoi
        /**
         * Test whether a value matches a numeric condition.
         * @param valueEval Value to Check.
         * @param op Comparator to use.
         * @param condition Value to check against.
         * @return whether the condition holds.
         * @If it's impossible to turn the condition into a number.
         */
        private static bool testNumericCondition(
            ValueEval valueEval, Operator op, String condition)
        {
            // Construct double from ValueEval.
            if (!(valueEval is NumericValueEval))
            {
                return(false);
            }
            double value = ((NumericValueEval)valueEval).NumberValue;

            // Construct double from condition.
            double conditionValue = 0.0;

            try
            {
                int intValue = Int32.Parse(condition);
                conditionValue = intValue;
            }
            catch (FormatException e)
            { // It's not an int.
                try
                {
                    conditionValue = Double.Parse(condition);
                }
                catch (FormatException e2)
                { // It's not a double.
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
            }

            int result = NumberComparer.Compare(value, conditionValue);

            switch (op)
            {
            case Operator.largerThan:
                return(result > 0);

            case Operator.largerEqualThan:
                return(result >= 0);

            case Operator.smallerThan:
                return(result < 0);

            case Operator.smallerEqualThan:
                return(result <= 0);

            case Operator.equal:
                return(result == 0);
            }
            return(false); // Can not be reached.
        }
예제 #9
0
        public void Initialize()
        {
            List <int> numbers = new List <int> {
                6, 9, 5, 8, 2
            };
            NumberComparer numberComparer = new NumberComparer();

            NumberTree = new Tree <int>(numberComparer);

            foreach (int number in numbers)
            {
                NumberTree.Add(number);
            }
        }
예제 #10
0
    /// <summary>
    /// Class constructor.  Initializes various elements
    /// </summary>
    public ListViewColumnSorter()
    {
        // Initialize the column to '0'
        ColumnToSort = 0;

        // Initialize the sort order to 'none'
        OrderOfSort = SortOrder.None;

        // Initialize the CaseInsensitiveComparer object
        ObjectCompare = new CaseInsensitiveComparer();

        NumberCompare = new NumberComparer();
        Mode          = ListSortMode.String;
    }
예제 #11
0
        private static bool Confirm(int i, double a, double b, int expRes)
        {
            int actRes = NumberComparer.Compare(a, b);

            int sgnActRes = actRes < 0 ? -1 : actRes > 0 ? +1 : 0;

            if (sgnActRes != expRes)
            {
                Console.WriteLine("Mismatch example[" + i + "] ("
                                  + FormatDoubleAsHex(a) + ", " + FormatDoubleAsHex(b) + ") expected "
                                  + expRes + " but got " + sgnActRes);
                return(false);
            }
            return(true);
        }
예제 #12
0
        public void OrderUniqueList()
        {
            var orderlist1 = new UniqueList <int> {
                2, 9, 5, 3, 0, 1
            };
            var orderlist2 = new UniqueList <int> {
                1, 8, 4, 2, 9, 7
            };
            var myComparer = new NumberComparer();

            orderlist1.Sort();
            orderlist2.Sort(myComparer);
            Assert.AreEqual(2, orderlist1[2]);
            Assert.AreEqual(4, orderlist2[2]);
            orderlist1.Clear();
            Assert.AreEqual(0, orderlist1.Count);
        }
예제 #13
0
        public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2)
        {
            ValueEval veText1;

            try
            {
                veText1 = OperandResolver.GetSingleValue(arg1, srcRowIndex, srcColumnIndex);
            }
            catch (EvaluationException e)
            {
                return(e.GetErrorEval());
            }
            String strText1 = OperandResolver.CoerceValueToString(veText1);
            Double number1  = OperandResolver.ParseDouble(strText1);

            if (double.IsNaN(number1))
            {
                return(ErrorEval.VALUE_INVALID);
            }

            ValueEval veText2;

            try
            {
                veText2 = OperandResolver.GetSingleValue(arg2, srcRowIndex, srcColumnIndex);
            }
            catch (EvaluationException e)
            {
                return(e.GetErrorEval());
            }

            String strText2 = OperandResolver.CoerceValueToString(veText2);
            Double number2  = OperandResolver.ParseDouble(strText2);

            if (double.IsNaN(number2))
            {
                return(ErrorEval.VALUE_INVALID);
            }

            //int result = new BigDecimal(number1).CompareTo(new BigDecimal(number2));
            int result = NumberComparer.Compare(number1, number2);

            return(result == 0 ? ONE : ZERO);
        }
예제 #14
0
파일: VLogCfgDir.cs 프로젝트: VCDH/vve
        public Dictionary <string, byte[]> GetDefaults()
        {
            Dictionary <string, byte[]> configuraties = new Dictionary <string, byte[]>();

            List <string>  nrs    = Vris.Keys.ToList();
            NumberComparer sorter = new NumberComparer();

            nrs.Sort(sorter);

            foreach (string nr in nrs)
            {
                if (Vris[nr].Configs.Length > 0)
                {
                    configuraties.Add(nr, Vris[nr].Configs[0].Data);
                }
            }

            return(configuraties);
        }
        public void When_AnalyzeRandomNumber_Then_Success()
        {
            var numberComparer = new NumberComparer();
            var numberBuilder = new NumberBuilder(unitsLength: 3);
            var numberAnalyzer = new NumberAnalyzer(numberBuilder);

            var mainNumber = numberBuilder.Build ();
            var firstTryNumber = numberBuilder.Build ();
            var attempt = numberComparer.Compare(mainNumber, firstTryNumber);

            var numberGuessed = false;
            var attemptsLimit = 20;
            var attemptsCount = 1;

            do
            {
                var nextNumber = numberAnalyzer.GetNextNumber(lastAttempt: attempt);

                attempt = numberComparer.Compare(mainNumber, nextNumber);
                attemptsCount++;

                if (attempt.Goods == 3)
                {
                    numberGuessed = true;
                }
            }
            while (!numberGuessed);

            if (attemptsCount > attemptsLimit)
            {
                Assert.Fail("The number has been guessed in {0} attempts. The maximum allowed was {1}", attemptsCount, attemptsLimit);
            }
            else
            {
                Assert.IsTrue(numberGuessed, "The number has been guessed in {0} attempts", attemptsCount);
            }
        }
예제 #16
0
 private static int CompareBlank(ValueEval v)
 {
     if (v == BlankEval.instance)
     {
         return(0);
     }
     if (v is BoolEval)
     {
         BoolEval boolEval = (BoolEval)v;
         return(boolEval.BooleanValue ? -1 : 0);
     }
     if (v is NumberEval)
     {
         NumberEval ne = (NumberEval)v;
         //return ne.NumberValue.CompareTo(0.0);
         return(NumberComparer.Compare(0.0, ne.NumberValue));
     }
     if (v is StringEval)
     {
         StringEval se = (StringEval)v;
         return(se.StringValue.Length < 1 ? 0 : -1);
     }
     throw new ArgumentException("bad value class (" + v.GetType().Name + ")");
 }
예제 #17
0
        /// <summary>
        /// Oyuna tahmin girişi yapar.
        /// </summary>
        /// <param name="guess">Tahmin.</param>
        public void EnterGuess(string guess)
        {
            if (!IsGameStarted)
            {
                return;
            }
            NumberComparer compareresults = NumberComparer.Compare(this.activeNumber, guess);

            TotalMinus += compareresults.MinusScore;
            TotalPlus  += compareresults.PlusScore;
            TotalGuessCount++;
            if (compareresults.Success)
            {
                OnEnteredGuess(guess, compareresults);
                FinishGame(true, GameFinishType.GameFinish_Win);
                return;
            }
            else
            {
                if (this.LifeLeft > 0)
                {
                    if (--this.LifeLeft <= 0)
                    {
                        OnLifeDecreased();
                        OnEnteredGuess(guess, compareresults);
                        FinishGame(false, GameFinishType.GameFinish_LOseLifeOver);
                        return;
                    }
                    else
                    {
                        OnLifeDecreased();
                    }
                }
            }
            OnEnteredGuess(guess, compareresults);
        }
예제 #18
0
 public void VersionIsNullOrEmpty()
 {
     var migrationA = new Migration { Number = "111" };
     var comparer = new NumberComparer();
     Assert.AreEqual(true, comparer.IsMigrationAfterVersion(migrationA, null));
     Assert.AreEqual(true, comparer.IsMigrationAfterVersion(migrationA, ""));
 }
예제 #19
0
 public void compare_x_not_greater_then_version_y_lesser(string x, string y)
 {
     var migrationA = new Migration { Number = x };
     var comparer = new NumberComparer();
     Assert.AreEqual(false, comparer.IsMigrationAfterVersion(migrationA, y));
 }
예제 #20
0
 public void InvalidFormatX()
 {
     var migrationA = new Migration { Number = "ABC" };
     var migrationB = new Migration { Number = "111" };
     var comparer = new NumberComparer();
     Assert.Throws<FormatException>(() => Assert.AreNotEqual(0, comparer.Compare(migrationA, migrationB)));
 }
예제 #21
0
 public EnteredGuessEventArgs(string guess, NumberComparer guessResults)
 {
     Guess        = guess;
     GuessResults = guessResults;
 }
예제 #22
0
        private static int DoCompare(ValueEval va, ValueEval vb)
        {
            // special cases when one operand is blank
            if (va == BlankEval.instance)
            {
                return(CompareBlank(vb));
            }
            if (vb == BlankEval.instance)
            {
                return(-CompareBlank(va));
            }

            if (va is BoolEval)
            {
                if (vb is BoolEval)
                {
                    BoolEval bA = (BoolEval)va;
                    BoolEval bB = (BoolEval)vb;
                    if (bA.BooleanValue == bB.BooleanValue)
                    {
                        return(0);
                    }
                    return(bA.BooleanValue ? 1 : -1);
                }
                return(1);
            }
            if (vb is BoolEval)
            {
                return(-1);
            }
            if (va is StringEval)
            {
                if (vb is StringEval)
                {
                    StringEval sA = (StringEval)va;
                    StringEval sB = (StringEval)vb;
                    return(string.Compare(sA.StringValue, sB.StringValue, StringComparison.OrdinalIgnoreCase));
                }
                return(1);
            }
            if (vb is StringEval)
            {
                return(-1);
            }
            if (va is NumberEval)
            {
                if (vb is NumberEval)
                {
                    NumberEval nA = (NumberEval)va;
                    NumberEval nB = (NumberEval)vb;
                    if (nA.NumberValue == nB.NumberValue)
                    {
                        // Excel considers -0.0 == 0.0 which is different to Double.compare()
                        return(0);
                    }
                    return(NumberComparer.Compare(nA.NumberValue, nB.NumberValue));
                }
            }
            throw new ArgumentException("Bad operand types (" + va.GetType().Name + "), ("
                                        + vb.GetType().Name + ")");
        }
예제 #23
0
 private void OnEnteredGuess(string guess, NumberComparer compareresults)
 {
     EnteredGuess?.Invoke(this, new EnteredGuessEventArgs(guess, compareresults));
 }