Beispiel #1
0
 public void TestAddNumbersOfSameLength()
 {
     var val1 = new StringNumber ("123");
     var val2 = new StringNumber ("987");
     var retval = StringNumberAdder.add (val1, val2);
     Assert.AreEqual ("1110", retval.Value);
 }
Beispiel #2
0
 public void TestAddSecondNumberIsLonger()
 {
     var val1 = new StringNumber ("123");
     var val2 = new StringNumber ("12345567");
     var retval = StringNumberAdder.add (val1, val2);
     Assert.IsNotNull (retval);
     Assert.AreEqual ("12345690", retval.Value);
 }
Beispiel #3
0
 public void TestAddFirstNumberIsLonger()
 {
     var val1 = new StringNumber ("987654321");
     var val2 = new StringNumber ("1234");
     var retval = StringNumberAdder.add (val1, val2);
     Assert.IsNotNull (retval);
     Assert.AreEqual ("987655555", retval.Value);
 }
Beispiel #4
0
        public static StringNumber subtract(StringNumber first, StringNumber second)
        {
            StringNumber minuend;
            StringNumber subtrahend;
            bool sign_flip = false;

            // Padd them out
            if (first.Value.Length > second.Value.Length) {
                second = new StringNumber (second.Value.PadLeft (first.Value.Length, '0'));
                minuend = first;
                subtrahend = second;
            }
            else if (second.Value.Length > first.Value.Length)
            {
                first = new StringNumber(first.Value.PadLeft(second.Value.Length, '0'));
                minuend = second;
                subtrahend = first;
            }
            else // same length
            {
                if (String.Compare(first.Value, second.Value) > 0) {
                    minuend = first;
                    subtrahend = second;
                } else {
                    minuend = second;
                    subtrahend = first;
                }
            }

            bool addOneToNext = false;
            StringBuilder sb = new StringBuilder();
            for(int i = minuend.Value.Length-1; i >= 0; i--) {
                int m = Int32.Parse(minuend.Value[i].ToString());
                int s = Int32.Parse(subtrahend.Value[i].ToString());
                if (addOneToNext) {
                    s += 1;
                }

                if (s > m) {
                    addOneToNext = true;
                    sb.Append(m+10-s);
                } else {
                    addOneToNext = false;
                    sb.Append(m-s);
                }
            }

            char[] chars = sb.ToString().ToCharArray();
            Array.Reverse(chars);
            return new StringNumber(new String(chars));
        }
Beispiel #5
0
        public static StringNumber add(StringNumber first, StringNumber second)
        {
            StringNumber temp_first;
            StringNumber temp_second;

            if (first.Value.Length > second.Value.Length) {
                temp_first = first;
                temp_second = new StringNumber (second.Value.PadLeft (first.Value.Length, '0'));
            }
            else
            {
                temp_first = new StringNumber(first.Value.PadLeft(second.Value.Length, '0'));
                temp_second = second;
            }

            UInt32 carry = 0;
            var sb = new StringBuilder ();
            for (int i = temp_first.Value.Length-1; i >= 0; i--)
            {
                char elem1 = temp_first.Value [i];
                char elem2 = temp_second.Value [i];

                var val1 = UInt32.Parse (elem1.ToString());
                var val2 = UInt32.Parse (elem2.ToString());

                UInt32 total = val1 + val2 + carry;
                carry = total / 10;
                sb.Append ((total % 10).ToString ());
            }

            if (carry > 0) {
                sb.Append (carry.ToString ());
            }

            // This is f****d up if this is actually the state of the art for reversing strings
            // in C#. WTF?!?!?!

            char[] arr = sb.ToString().ToCharArray();
            Array.Reverse(arr);
            var value = new String(arr);
            return new StringNumber(value);
        }
Beispiel #6
0
 public void TestEqualsMethodNotEqualObjects()
 {
     var val1 = new StringNumber ("123");
     Assert.IsFalse (val1.Equals (new StringNumber ("456")));
 }
Beispiel #7
0
 public void TestEqualsMethodEqualObject()
 {
     var val1 = new StringNumber ("123");
     Assert.IsTrue (val1.Equals (new StringNumber ("123")));
 }
Beispiel #8
0
 public void TestEqualsMethodDifferentObjects()
 {
     var val1 = new StringNumber ("123");
     Assert.IsFalse (val1.Equals ("123"));
 }
Beispiel #9
0
 public void BasicInstantiation()
 {
     var testval = new StringNumber ("123");
     Assert.AreEqual ("123", testval.Value);
 }
Beispiel #10
0
 public void TestSubtraction()
 {
     var val1 = new StringNumber ("456");
     var val2 = new StringNumber ("123");
     var retval = StringNumberSubtracter.subtract (val1, val2);
     Assert.AreEqual ("333", retval.Value);
 }
Beispiel #11
0
 public void TestOverloadedNotEqualsOperatorSame()
 {
     var val1 = new StringNumber ("123");
     var val2 = new StringNumber ("123");
     Assert.IsFalse (val1 != val2);
 }
Beispiel #12
0
 public void TestOverloadedAddOperator()
 {
     var val1 = new StringNumber ("123");
     var val2 = new StringNumber ("987");
     Assert.AreEqual ("1110", (val1 + val2).Value);
 }