Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Integer integer1, integer2, integer3;
            integer1 = new Integer();
            integer2 = new Integer();
            integer3 = new Integer();

            Console.Write("Add2N ");

            Input(ref integer1, ref integer2);
            Output(integer1, integer2, ref integer3);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Ham cong 2 chuoi so nguyen
        /// </summary>
        /// <param name="integer1">doi tuong co chuoi duoc cong voi chuoi cua doi tuong this</param>
        /// <returns>tra ve 1 doi tuong moi sau khi da cong 2 chuoi so nguyen</returns>
        /// <example>12345678 + 12 = 12345690</example>
        public Integer Sum(Integer integer1)
        {
            Integer integer2 = new Integer();
            int length = this.CompareSequence(integer1);
            int remainder = 0;

            for (int i = length - 1; i >= 0; --i)
                integer2.sequence.Add(this.SumInteger(integer1, i, ref remainder));

            if (remainder == 1)
                integer2.sequence.Add('1');

            this.ReduceSequence();
            integer1.ReduceSequence();

            return integer2;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Ham so sanh do dai cua 2 chuoi
        /// </summary>
        /// <param name="integer1">doi tuong co chuoi duoc so sanh do dai voi chuoi cua doi tuong this</param>
        /// <returns>tra ve do dai cua chuoi dai nhat</returns>
        public int CompareSequence(Integer integer1)
        {
            int var = this.sequence.Count - integer1.sequence.Count;

            if (var != 0)
            {
                if (var > 0)
                {
                    integer1.ChangeSequence(var);
                    return this.sequence.Count;
                }
                else
                {
                    this.ChangeSequence(var * -1);
                    return integer1.sequence.Count;
                }
            }
            return this.sequence.Count;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Ham Xuat ket qua sau khi cong 2 so nguyen
        /// </summary>
        static void Output(Integer integer1, Integer integer2, ref Integer integer3)
        {
            string s;

            if (!integer1.CheckErrorCharacter())
            {
                if (!integer2.CheckErrorCharacter())
                {
                    s = integer1.Output() + "," + integer2.Output() + " ko hop le";
                    Console.WriteLine("{0}", s);
                    Console.ReadKey();
                }
                else
                {
                    s = integer1.Output() + " ko hop le";
                    Console.WriteLine("{0}", s);
                    Console.ReadKey();
                }
            }
            else
            {
                if (!integer2.CheckErrorCharacter())
                {
                    s = integer2.Output() + " ko hop le";
                    Console.WriteLine("{0}", s);
                    Console.ReadKey();
                }
                else
                {
                    integer3 = integer1.Sum(integer2);
                    integer3.InvertSequence();
                    integer3.ReduceSequence();
                    Console.Write("Ket qua hien thi la: ");
                    s = integer1.Output() + " +" + integer2.Output() + " =" + integer3.Output();
                    Console.WriteLine("{0}", s);
                    Console.ReadKey();
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Ham nhap 2 so nguyen
        /// </summary>
        static void Input(ref Integer integer1, ref Integer integer2)
        {
            char c;

            c = (char)Console.Read();

            //Nhap so nguyen thu 1: Nhap phan tu tu ban phim cho den khi gap phim Space
            while (c != 32)
            {
                integer1.Input(c);
                c = (char)Console.Read();
            };

            c = (char)Console.Read();

            //Nhap so nguyen thu 2: Nhap phan tu tu ban phim cho den khi gap phim Enter
            while (c != 13)
            {
                integer2.Input(c);
                c = (char)Console.Read();
            };
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Ham cong tung phan tu cua 2 chuoi so nguyen voi nhau
        /// </summary>
        /// <param name="var">phan tu thu var cua 2 chuoi so nguyen duoc cong voi nhau</param>
        /// <param name="remainder">so du</param>
        /// <returns>tra ve ma ky tu ASCII cua so nguyen sau khi cong</returns>
        /// <example>"1" + "2" = "3"------>ma ASCII cua '3' la 51 </example>
        public char SumInteger(Integer integer1, int var, ref int remainder)
        {
            //ket qua sau khi cong
            int i = Calculate((int)this.sequence[var] - 48, (int)integer1.sequence[var] - 48, ref remainder);

            return Convert.ToChar(i + 48);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Ham tao sao chep
 /// </summary>
 /// <param name="integer1">1 doi tuong can duoc sao chep</param>
 public Integer(Integer integer1)
 {
     integer1.CopySequence(out this.sequence);
 }