Beispiel #1
0
 public bool compareTo(MyInt other)
 {
     if (Value == other.Value)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #2
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);
            }
        }
Beispiel #3
0
        public MyInt Min(MyInt other)
        {
            MyInt lel = this.Max(other);

            if (lel.Equals(this))
            {
                return(other);
            }
            else
            {
                return(this);
            }
        }
Beispiel #4
0
        public MyInt Sub(MyInt other)
        {
            MyInt answer;

            if (other.Value[0] != '-')
            {
                other = new MyInt("-" + other.Value);
            }
            else
            {
                other = other.abs();
            }

            answer = this.Add(other);
            return(answer);
        }
Beispiel #5
0
        public MyInt Multiply(MyInt other)
        {
            MyInt  ans = new MyInt("0");
            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 = B.ToCharArray();
            Array.Reverse(arr);
            B = new string(arr);

            int multer = 1;


            for (int i = 0; i < B.Length; i++)
            {
                int n = int.Parse(B[i].ToString());
                n = n * (int)Math.Pow(10, i);
                for (int j = 0; j < n; j++)
                {
                    ans = ans.Add(this.abs());
                    string val = ans.Value;
                }
                multer++;
            }
            if (Az != Bz)
            {
                ans = new MyInt("-" + ans.Value);
            }

            return(ans);
        }
Beispiel #6
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));
        }
Beispiel #7
0
        //Методы сравнения

        public MyInt Max(MyInt other)
        {
            string A  = 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);
            }

            if (Az == 0 && Bz == 1)
            {
                return(this);
            }
            else if (Az == 1 && Bz == 0)
            {
                return(other);
            }
            else
            {
                if (A.Length > B.Length)
                {
                    return(this);
                }
                else if (B.Length > A.Length)
                {
                    return(other);
                }
                else
                {
                    bool isAmax = true;
                    for (int i = 0; i < A.Length; i++)
                    {
                        if (int.Parse(A[i].ToString()) > int.Parse(B[i].ToString()))
                        {
                            break;
                        }
                        else if (int.Parse(A[i].ToString()) < int.Parse(B[i].ToString()))
                        {
                            isAmax = false;
                            break;
                        }
                    }

                    if (isAmax)
                    {
                        return(this);
                    }
                    else
                    {
                        return(other);
                    }
                }
            }
        }