Example #1
0
        //НОД
        public MyInt Gcd(MyInt other)
        {
            string what   = "";
            MyInt  answer = new MyInt("0");

            this.abs();
            other = other.abs();
            if (this.abs().Value == this.abs().Max(other.abs()).Value)
            {
                answer = this;
                while (answer.Value != "0")
                {
                    answer = answer.Sub(other.abs());
                    what   = answer.Value;
                    if (answer.abs().Value == answer.abs().Min(other.abs()).Value)
                    {
                        MyInt raz = other;
                        other  = answer;
                        answer = raz;
                    }
                    if (other.Value == answer.Value)
                    {
                        break;
                    }
                }
                return(answer);
            }
            else if (other.abs().Value == (this.abs().Max(other.abs())).Value)
            {
                MyInt subber = this;
                answer = other;
                while (answer.abs().Value != "0")
                {
                    answer = answer.Sub(subber.abs());
                    what   = answer.Value;
                    if (answer.abs().Value == answer.abs().Min(subber.abs()).Value)
                    {
                        MyInt raz = subber;
                        subber = answer;
                        answer = raz;
                    }
                    if (subber.Value == answer.Value)
                    {
                        break;
                    }
                }
                return(answer);
            }
            else
            {
                return(this);
            }
        }
Example #2
0
        public MyInt Add(MyInt other) //сумма
        {
            string answer = "";
            string A      = this.Value;
            string B      = other.Value;
            int    Az     = 0;
            int    Bz     = 0;

            if (A[0] == '-')
            {
                Az = 1;
                A  = A.Substring(1);
            }
            if (B[0] == '-')
            {
                Bz = 1;
                B  = B.Substring(1);
            }
            char[] arr = A.ToCharArray();
            Array.Reverse(arr);
            A   = new string(arr);
            arr = B.ToCharArray();
            Array.Reverse(arr);
            B = new string(arr);

            int r = A.Length - B.Length;

            if (r > 0)
            {
                for (int i = 0; i < r; i++)
                {
                    B = B + "0";
                }
            }
            else if (r < 0)
            {
                r = -r;
                for (int i = 0; i < r; i++)
                {
                    A = A + "0";
                }
            }


            if (Az != Bz)
            {
                if (this.compareTo(other))
                {
                    return(new MyInt("0"));
                }
                else
                {
                    if (this.abs().Value == this.abs().Max(other.abs()).Value)
                    {
                        answer = subs(A, B);
                        bool isOver = false;
                        int  cc     = 0;
                        for (int i = 0; i < answer.Length; i++)
                        {
                            if (answer[i] != '0')
                            {
                                isOver = true;
                            }
                            else
                            {
                                cc++;
                            }

                            if (isOver)
                            {
                                answer = answer.Substring(cc);
                                break;
                            }
                        }
                        if (Az == 1)
                        {
                            answer = "-" + answer;
                        }
                    }
                    else if (other.abs().Value == (this.abs().Max(other.abs())).Value)
                    {
                        answer = subs(B, A);
                        bool isOver = false;
                        int  cc     = 0;
                        for (int i = 0; i < answer.Length; i++)
                        {
                            if (answer[i] != '0')
                            {
                                isOver = true;
                            }
                            else
                            {
                                cc++;
                            }

                            if (isOver)
                            {
                                answer = answer.Substring(cc);
                                break;
                            }
                        }
                        if (Bz == 1)
                        {
                            answer = "-" + answer;
                        }
                    }
                }
            }
            else
            {
                answer = adds(A, B);
                if (Az == 1)
                {
                    answer = "-" + answer;
                }
            }

            return(new MyInt(answer));
        }