Exemplo n.º 1
0
        public static RationalNum operator -(RationalNum num1, RationalNum num2)
        {
            RationalNum temp = new RationalNum();

            int tempN1 = num1.negative ? -num1.n : num1.n;
            int tempN2 = num2.negative ? -num2.n : num2.n;

            int commonM = LCM(num1.m, num2.m);

            temp.m = commonM;

            tempN1 = tempN1 * commonM / num1.m;
            tempN2 = tempN2 * commonM / num2.m;

            temp.n = tempN1 - tempN2;
            if (temp.n < 0)
            {
                temp.negative = true;
                temp.n        = -temp.n;
            }

            int commonD = GCD(temp.n, temp.m);

            temp.n /= commonD;
            temp.m /= commonD;

            return(temp);
        }
Exemplo n.º 2
0
        public RationalNum(string str)
        {
            RationalNum temp = StringToRationalNum(str);

            n        = temp.n;
            m        = temp.m;
            negative = temp.negative;
        }
Exemplo n.º 3
0
        public static RationalNum operator /(RationalNum num1, RationalNum num2)
        {
            if (num2.n == 0)
            {
                throw new Exception("Division by zero");
            }
            RationalNum temp = new RationalNum();

            temp.negative = num2.negative;
            temp.n        = num2.m;
            temp.m        = num2.n;

            return(num1 * temp);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            RationalNum a1   = new RationalNum("0.(3)");
            RationalNum b1   = new RationalNum("1/3");
            RationalNum res1 = a1 + b1;

            Console.WriteLine(res1);
            Console.WriteLine(a1);
            Console.WriteLine(b1);
            Console.WriteLine(a1.ToString());
            Console.WriteLine(b1.ToString());

            Console.WriteLine();

            Console.WriteLine(a1.ToStirng("{0:C}"));
            Console.WriteLine(a1.ToStirng("{0:f4}"));
            Console.WriteLine(a1.ToStirng("{0:P1}"));

            Console.WriteLine();

            RationalNum a2   = new RationalNum("0.654(32)");
            RationalNum b2   = new RationalNum("-36.987");
            RationalNum res2 = a2 - b2;

            Console.WriteLine(res2);
            Console.WriteLine(a2);
            Console.WriteLine(b2);
            Console.WriteLine(a2.ToString());
            Console.WriteLine(b2.ToString());

            RationalNum res3 = a2 * b2;

            Console.WriteLine(res3);

            RationalNum res4 = a2 / b2;

            Console.WriteLine(res4);

            double d = a1;
            int    i = (int)a1;

            Console.WriteLine(d);
            Console.WriteLine(i);
        }
Exemplo n.º 5
0
        public static RationalNum operator *(RationalNum num1, RationalNum num2)
        {
            RationalNum temp = new RationalNum();

            if ((num1.negative && num2.negative) || (!num1.negative && !num2.negative))
            {
                temp.negative = false;
            }
            else
            {
                temp.negative = true;
            }

            temp.n = num1.n * num2.n;
            temp.m = num1.m * num2.m;

            int commonD = GCD(temp.n, temp.m);

            temp.n /= commonD;
            temp.m /= commonD;

            return(temp);
        }
Exemplo n.º 6
0
        public static RationalNum StringToRationalNum(string str)
        {
            RationalNum num = new RationalNum();

            num.negative = false;
            str.Replace(" ", "");
            if (str.Contains("."))
            {
                str = str.Replace('.', ' ');
                str = str.Replace('(', ' ');
                str = str.Replace(')', ' ');
                string[] arr = str.Split(' ');

                if (arr.Length == 2)
                {
                    int integerPart    = 0;
                    int fractionalPart = 0;
                    try
                    {
                        integerPart = Convert.ToInt32(arr[0]);
                        if (arr[0][0] == '-')
                        {
                            integerPart  = integerPart >= 0 ? integerPart : -integerPart;
                            num.negative = true;
                        }
                        fractionalPart = Convert.ToInt32(arr[1]);
                        if (fractionalPart < 0)
                        {
                            throw new Exception("Unkorrect input");
                        }
                    }
                    catch
                    {
                        throw new Exception("Unkorrect input");
                    }
                    int tempN = fractionalPart;
                    int tempM = (int)Math.Pow(10, arr[1].Length);
                    tempN = tempM * integerPart + tempN;
                    int commonD = GCD(tempN, tempM);
                    tempM /= commonD;
                    tempN /= commonD;
                    num.n  = tempN;
                    num.m  = tempM;
                }
                else if (arr.Length == 4 && arr[3] == "")
                {
                    int integerPart    = 0;
                    int fractionalPart = 0;
                    int periodicPart   = 0;
                    try
                    {
                        integerPart = Convert.ToInt32(arr[0]);
                        if (arr[0][0] == '-')
                        {
                            integerPart  = integerPart >= 0 ? integerPart : -integerPart;
                            num.negative = true;
                        }
                        if (arr[1].Length != 0)
                        {
                            fractionalPart = Convert.ToInt32(arr[1]);
                        }
                        else
                        {
                            fractionalPart = 0;
                        }
                        periodicPart = Convert.ToInt32(arr[2]);
                    }
                    catch
                    {
                        throw new Exception("Unkorrect input");
                    }
                    int tempN = (fractionalPart * (int)Math.Pow(10, arr[2].Length) + periodicPart) -
                                fractionalPart;
                    int tempM = NinesByCount(arr[2].Length) * (int)Math.Pow(10, arr[1].Length);
                    tempN = integerPart * tempM + tempN;
                    int commonD = GCD(tempN, tempM);
                    tempM /= commonD;
                    tempN /= commonD;
                    num.n  = tempN;
                    num.m  = tempM;
                }
            }
            else if (str.Contains("/"))
            {
                string[] arr   = str.Split('/');
                int      tempN = 0;
                int      tempM = 0;
                try
                {
                    tempN = Convert.ToInt32(arr[0]);
                    if (arr[0][0] == '-')
                    {
                        tempN        = tempN >= 0 ? tempN : -tempN;
                        num.negative = true;
                    }
                    tempM = Convert.ToInt32(arr[1]);
                    if (tempM == 0)
                    {
                        throw new Exception("Unkorrect input");
                    }
                }
                catch
                {
                    throw new Exception("Unkorrect input");
                }
                num.n = tempN;
                num.m = tempM;
            }
            else
            {
                throw new Exception("Unkorrect input");
            }
            return(num);
        }
Exemplo n.º 7
0
 public RationalNum(RationalNum obj)
 {
     n        = obj.n;
     m        = obj.m;
     negative = obj.negative;
 }
Exemplo n.º 8
0
        public static void RationNumberArrFiller(ref RationalNum[] rationNumbers)
        {
            string numberQuantityStr;

            //задание количества рационалных чисел
            WriteLine("How many numbers do you whant to create?");
            numberQuantityStr = ReadLine();
            int.TryParse((((string.IsNullOrEmpty(numberQuantityStr)) || (int.Parse(numberQuantityStr) <= 0)) ?
                          "0" : numberQuantityStr), out int numbersQuantity);

            if (numbersQuantity <= 0)
            {
                Write("wrong number entered");
                return;
            }

            rationNumbers = new RationalNum[numbersQuantity];

            //заполнение массива рац. чисел
            for (int i = 0; i < rationNumbers.Length; i++)
            {
                string entringNumStr;

                int numerator;
                int denumerator;

                for (; ;)
                {
                    //вводим проверяем числитель
                    WriteLine("Enter numerator please");
                    entringNumStr = ReadLine();
                    if ((string.IsNullOrEmpty(entringNumStr)) || (int.Parse(entringNumStr) < 0))
                    {
                        WriteLine("Wrong value was entered, try again please!");
                    }
                    else
                    {
                        numerator = int.Parse(entringNumStr);
                        break;
                    }
                }

                for (; ;)
                {
                    //знаменатель
                    WriteLine("Enter denumerator please");
                    entringNumStr = ReadLine();
                    if ((string.IsNullOrEmpty(entringNumStr)) || (int.Parse(entringNumStr) < 0))
                    {
                        WriteLine("Wrong value was entered, try again please!");
                    }
                    else
                    {
                        denumerator = int.Parse(entringNumStr);
                        break;
                    }
                }

                rationNumbers[i] = new RationalNum(numerator, denumerator);
            }
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            RationalNum num1 = new RationalNum(11, -2);
            RationalNum num2 = new RationalNum(11, -2);

            Console.WriteLine(num1 + num2);
            Console.WriteLine((double)(num1 - num2));
            Console.WriteLine(num1 * num2);
            Console.WriteLine(num1 / num2);
            Console.WriteLine(num1);
            Console.WriteLine(num1 > num2);
            Console.WriteLine(num1 < num2);
            Console.WriteLine(num1 >= num2);
            Console.WriteLine(num1 <= num2);
            Console.WriteLine(num1 != num2);
            Console.WriteLine(num1 == num2);
            Console.WriteLine(num1.Equals(-5.5));
            Console.WriteLine(num1.Equals(num2));
            Console.WriteLine(num1.GetHashCode());
            Console.WriteLine(num2.GetHashCode());
            Console.WriteLine(num1.ToString("D"));
            Console.WriteLine(num1.ToString("F"));
            Console.WriteLine(num2.ToString("E"));
            Console.WriteLine(num2.ToString("G"));
            Console.WriteLine();
            Console.WriteLine("{0:D}", num1);
            Console.WriteLine("{0:F}", num1);
            Console.WriteLine("{0:E}", num2);
            Console.WriteLine("{0:G}", num2);
            Console.WriteLine();
            Console.WriteLine(num1.ToString() + " [ ]");
            Console.WriteLine(Convert.ToInt32(num1));
            Console.WriteLine(Convert.ToDecimal(num1));
            Console.WriteLine(Convert.ToString(num1));
            Console.WriteLine("Bool " + Convert.ToBoolean(num1));
            List <RationalNum> listWithNumbers = new List <RationalNum>()
            {
                new RationalNum(1, 4),
                new RationalNum(2),
                new RationalNum(3, 4)
            };

            listWithNumbers.Add(RationalNum.Parse("4/-7"));
            listWithNumbers.Add(RationalNum.Parse("-2"));
            listWithNumbers.Add(RationalNum.Parse("-0.75"));
            listWithNumbers.Add(RationalNum.Parse("-0,89"));
            listWithNumbers.Add(RationalNum.Parse("1,1"));
            listWithNumbers.Add(RationalNum.Parse("-1/3"));
            foreach (RationalNum num in listWithNumbers)
            {
                Console.Write(num + " ");
            }
            Console.WriteLine();
            listWithNumbers.Sort();
            foreach (RationalNum num in listWithNumbers)
            {
                Console.Write(num + " ");
            }
            Console.WriteLine();
            RationalNum number;

            Console.WriteLine(RationalNum.TryParse("Some text", out number));
            Console.WriteLine(RationalNum.TryParse("2/5", out number));
            Console.WriteLine(RationalNum.TryParse("()v/4)", out number));
        }