Exemplo n.º 1
0
        public bool SmallerThan(string compare)
        {
            StringInt reversed = new StringInt(compare);

            return(reversed.GreaterThan(this.value));
        }
Exemplo n.º 2
0
        public void Subtract(string reducer)
        {
            //Console.WriteLine("Called subtract with argument " + reducer + " and value " + this.GetValue());
            if (!this.isNegative)
            {
                if (reducer[0] != '-')
                {
                    //(+) - (+)
                    //Don't change anything
                }
                else
                {
                    //(+) - (-)
                    reducer = reducer.TrimStart('-');
                    this.Add(reducer);
                    return;
                }
            }
            else
            {
                if (reducer[0] != '-')
                {
                    //(-) - (+)
                    this.isNegative = false;
                    this.Add(reducer);
                    this.isNegative = true;
                    return;
                }
                else
                {
                    //(-) - (-)
                    //Don't change anything
                }
            }

            //Make sure the first number is greater than the reducer
            StringInt temp = new StringInt(reducer.TrimStart('-'));

            if (temp.GreaterThan(this.value.TrimStart('-')))
            {
                string tempVal = this.GetValue();
                this.Set(reducer);
                reducer         = tempVal;
                this.isNegative = !this.isNegative;
            }

            reducer = reducer.TrimStart('-');

            int numLength = (reducer.Length > this.value.Length) ? reducer.Length : this.value.Length;

            numLength++;
            string num            = this.value;
            short  c              = 0;
            string revertedResult = String.Empty;

            for (int i = 1; i <= numLength; i++)
            {
                short a = 0, b = 0;
                try
                {
                    a = Convert.ToInt16(num[num.Length - i].ToString());
                }
                catch (IndexOutOfRangeException) { }
                try
                {
                    b = Convert.ToInt16(reducer[reducer.Length - i].ToString());
                }
                catch (IndexOutOfRangeException) { }

                int result = Convert.ToInt16(a) - (Convert.ToInt16(b) + c);
                c = 0;
                if (result < 0)
                {
                    c++;
                    result += 10;
                }
                revertedResult += result.ToString();
            }

            //Revert string
            char[] resultCharArr = revertedResult.ToCharArray();
            Array.Reverse(resultCharArr);
            this.value = new string(resultCharArr).TrimStart('0');
            if (this.value.Length == 0)
            {
                this.Set("0");
            }
        }