Exemplo n.º 1
0
 public static Fraction operator -(Fraction b, Fraction c)
 {
     Fraction fr = new Fraction();
     fr.Numerator = b.Numerator - c.Numerator;
     fr.Denominator = b.Denominator - c.Denominator;
     return fr;
 }
Exemplo n.º 2
0
        public Pylonomial(String pylonomial, Boolean positive)
        {
            pyCoefficient = (Fraction.Fraction) 1;
            if (!(pylonomial.StartsWith("+") || pylonomial.StartsWith("-")))
            {
                pylonomial = "+" + pylonomial;
            }
            List <String> temp             = new List <String>();
            Int64         previousOperator = 0;

            for (Int64 i = 1; i < pylonomial.Length; i += 1)
            {
                if (pylonomial[(Int32)i].Equals('+') || pylonomial[(Int32)i].Equals('-'))
                {
                    if (i - previousOperator >= 2)
                    {
                        temp.Add(pylonomial.Substring((Int32)previousOperator, (Int32)i - (Int32)previousOperator));
                        previousOperator = i;
                    }
                }
            }
            temp.Add(pylonomial.Substring((Int32)previousOperator, pylonomial.Length - (Int32)previousOperator));
            foreach (var i in temp)
            {
                moList.Add(new Monomial(i));
            }
        }
        // Takes two fractions, adds them and returns the results
        public Fraction.Fraction Addition(Fraction.Fraction first, Fraction.Fraction second)
        {
            Fraction.Fraction results = new Fraction.Fraction();

            // check for errors
            if (!IsNoneZeroDenominator(first.GetDenominator()) || !IsNoneZeroDenominator(second.GetDenominator()))
            {
                return(results);
            }

            // if not a common denominator do the thing
            if (first.GetDenominator() != second.GetDenominator())
            {
                int firstMultiple = first.GetDenominator();
                first.SetDenominator(first.GetDenominator() * second.GetDenominator());
                first.SetNumerator(first.GetNumerator() * second.GetDenominator());
                second.SetDenominator(firstMultiple * second.GetDenominator());
                second.SetNumerator(firstMultiple * second.GetNumerator());
            }

            // Do the math
            results.SetNumerator(first.GetNumerator() + second.GetNumerator());
            results.SetDenominator(first.GetDenominator());

            return(results);
        }
Exemplo n.º 4
0
        public Fraction(Fraction frac)
        {
            this.Num = frac.Num;
            this.Denom = frac.Denom;

            setNum = true;
            setDenom = true;
        }
Exemplo n.º 5
0
 public static void Main(string[] args)
 {
     Fraction fraction1 = new Fraction(22, 7);
     Fraction fraction2 = new Fraction(40, 4);
     Fraction result = fraction1 + fraction2;
     Console.WriteLine(result.Numerator);
     Console.WriteLine(result.Denominator);
     Console.WriteLine(result);
 }
Exemplo n.º 6
0
        public static void Main()
        {
            Fraction fra1 = new Fraction(3, 2);
            Fraction fra2 = new Fraction(2, 4);
            Fraction fra3 = new Fraction();
            Fraction fra4 = new Fraction(3, 2);

            Console.Write("Fraction #1:\t {0}", fra1);
            Console.WriteLine("\tFraction #2:\t {0}", fra2);
            Console.Write("Fraction #3:\t {0}", fra3);
            Console.WriteLine("\tFraction #4:\t {0}", fra4);

            Console.WriteLine();
            Console.WriteLine("fra1 simplified:\t{0}", fra1.simplify());
            Console.WriteLine("fra2 simplified:\t{0}", fra2.simplify());
            Console.WriteLine();

            fra3 = fra1 + fra2;
            Console.WriteLine("fra3 = fra1 + fra2\t {0}", fra3.simplify());

            fra4 += fra2;
            Console.WriteLine("fra4 += fra2\t\t {0}", fra4.simplify());

            fra3 = fra1 - fra2;
            Console.WriteLine("fra3 = fra1 - fra2\t {0}", fra3.simplify());

            fra3 = fra1 * fra2;
            Console.WriteLine("fra3 = fra1 * fra2\t {0}", fra3.simplify());

            fra3 = fra1 / fra2;
            Console.WriteLine("fra3 = fra1 / fra2\t {0}", fra3.simplify());

            Console.WriteLine();
            Console.WriteLine("fra1 == fra4\t{0}", fra1 == fra4);
            Console.WriteLine("fra1 == fra2\t{0}", fra1 == fra2);

            Console.WriteLine();
            Console.WriteLine("fra1 != fra4\t{0}", fra1 != fra4);
            Console.WriteLine("fra1 != fra2\t{0}", fra1 != fra2);

            Console.WriteLine();
            Console.WriteLine("fra1 > fra2?\t{0}", fra1 > fra2);
            Console.WriteLine("fra1 < fra2?\t{0}", fra1 < fra2);

            Console.WriteLine();
            Console.WriteLine("(int)fra1:\t{0}", (int)fra1);
            Console.WriteLine("(int)fra2:\t{0}", (int)fra2);

            Console.WriteLine();
            Console.WriteLine("(double)fra1:\t{0}", (double)fra1);
            Console.WriteLine("(double)fra2:\t{0}", (double)fra2);

            Console.WriteLine();

            Console.ReadKey();
        }
        // Takes two fractions, divides the first with the second and returns the results
        public Fraction.Fraction Division(Fraction.Fraction first, Fraction.Fraction second)
        {
            Fraction.Fraction results = new Fraction.Fraction();

            // check for errors
            if (!IsNoneZeroDenominator(first.GetDenominator()) || !IsNoneZeroDenominator(second.GetDenominator()))
            {
                return(results);
            }

            // Do the math
            results.SetNumerator(first.GetNumerator() * second.GetDenominator());
            results.SetDenominator(first.GetDenominator() * second.GetNumerator());

            // Return the reduced version of the results
            return(results);
        }
Exemplo n.º 8
0
        public Pylonomial(String pylonomial)
        {
            const String pattern1 = @"^[+-](\d+)?[(]";
            const String pattern2 = @"[+-]\d+";
            const String pattern3 = @"[+-]\d{0,65536}[A-Za-z]?(\^\d+\^)?";
            const String pattern4 = @"[+-]";

            if (!(pylonomial.StartsWith("+") || pylonomial.StartsWith("-")))
            {
                pylonomial = "+" + pylonomial;
            }
            Match match = Regex.Match(pylonomial, pattern1);

            if (Regex.IsMatch(match.Value, pattern2))
            {
                pyCoefficient = Fraction.Fraction.Parse(Regex.Match(match.Value, pattern2).Value);
            }
            else
            {
                switch (Regex.Match(match.Value, pattern4).Value)
                {
                case "+":
                    pyCoefficient = (Fraction.Fraction) 1;
                    break;

                case "-":
                    pyCoefficient = (Fraction.Fraction)(-1);
                    break;

                default:
                    break;
                }
            }
            pylonomial = Regex.Replace(pylonomial, pattern1, "");
            pylonomial = pylonomial.Replace(")", "");
            if (!(pylonomial.StartsWith("+") || pylonomial.StartsWith("-")))
            {
                pylonomial = "+" + pylonomial;
            }
            MatchCollection matchCollection = Regex.Matches(pylonomial, pattern3);

            for (Int64 i = 0; i < matchCollection.Count; i += 1)
            {
                moList.Add(new Monomial(matchCollection[(Int32)i].Value));
            }
        }
        // Reduce the fraction down to its lowest terms before my teacher gets mad at me
        public Fraction.Fraction ReducedFraction(Fraction.Fraction fractionToReduce)
        {
            // Protection against 0
            if (fractionToReduce.GetDenominator() == 0)
            {
                return(fractionToReduce);
            }

            int gcd = GreatestCommonDenominator(fractionToReduce.GetNumerator(), fractionToReduce.GetDenominator());

            if (gcd != 0)
            {
                fractionToReduce.SetNumerator(fractionToReduce.GetNumerator() / gcd);
                fractionToReduce.SetDenominator(fractionToReduce.GetDenominator() / gcd);
            }
            if (gcd < 0)
            {
                fractionToReduce.SetNumerator(fractionToReduce.GetNumerator() * -1);
                fractionToReduce.SetDenominator(fractionToReduce.GetDenominator() * -1);
            }
            return(fractionToReduce);
        }
Exemplo n.º 10
0
 Fraction AddFraction(Fraction target)
 {
     return(Add(this, target));
 }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            //different constructors and string formats
            List <Fraction> fractions = new List <Fraction>()
            {
                new Fraction(5),
                new Fraction(2, 3),
                new Fraction(1.45),
                Fraction.GetFraction("5/3"),
                Fraction.GetFraction("-3(1/2)"),
                Fraction.GetFraction("15,69"),
                Fraction.GetFraction("21")
            };

            Console.WriteLine("YES");

            //overload operations
            fractions.Add(fractions[0] + fractions[2]);
            fractions.Add(fractions[3] - fractions[1]);
            fractions.Add(fractions[6] * fractions[4]);
            fractions.Add(fractions[5] / fractions[7]);

            //use Clone
            fractions.Add(fractions[3].Clone() as Fraction);


            Fraction testFraction = new Fraction(-42, 13);

            Console.WriteLine("Try to convert fraction to unsuported format: ");
            try
            {
                testFraction.ToString("The string");
            }
            catch (FormatException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("\nTry get fraction using unssuported format:");
            try
            {
                fractions.Add(Fraction.GetFraction("The string"));
            }
            catch (FormatException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("\nTry to add fraction with denominator = 0 :");
            try
            {
                fractions.Add(new Fraction(12, 0));
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("\nTry to add fraction with denominator < 0 :");
            try
            {
                fractions.Add(new Fraction(12, -3));
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }


            Console.WriteLine("\nList of all fractions:");
            fractions.ForEach(i => Console.WriteLine("{0:S}", i));


            Console.WriteLine("\noutput fraction in different formats:");
            Console.WriteLine("{0:S}", testFraction);
            Console.WriteLine("{0:M}", testFraction);
            Console.WriteLine("{0:D}", testFraction);
            Console.WriteLine("{0:I}", testFraction);


            Console.WriteLine("\nCoversion:");
            Console.WriteLine((short)testFraction);
            Console.WriteLine((int)testFraction);
            Console.WriteLine((long)testFraction);
            Console.WriteLine((float)testFraction);
            Console.WriteLine((double)testFraction);
            Console.WriteLine(Convert.ToBoolean(testFraction));
            Console.WriteLine(Convert.ToString(testFraction));

            Console.WriteLine("\nComparison:");
            Console.WriteLine(fractions[0] == fractions[9]);
            Console.WriteLine(fractions[3] != fractions[5]);
            Console.WriteLine(fractions[6] < fractions[8]);
            Console.WriteLine(fractions[1] >= fractions[7]);
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            Fraction f, d;
            Fraction rez = new Fraction();

            Console.WriteLine("Введите числитель, знаменатель первой дроби");
            long num1 = long.Parse(Console.ReadLine());
            long den1 = long.Parse(Console.ReadLine());

            try
            {
                f = new Fraction(num1, den1);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return;
            }
            Console.WriteLine("Введите числитель, знаменатель второй дроби");
            long num2 = long.Parse(Console.ReadLine());
            long den2 = long.Parse(Console.ReadLine());

            try
            {
                d = new Fraction(num2, den2);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            char choice;

            while (true)
            {
                choice = option();
                Console.Clear();
                if (choice != '0')
                {
                    switch (choice)
                    {
                    case '1':
                        rez = f + d;
                        Console.WriteLine("Сумма = {0}", (rez).ToString());
                        break;

                    case '2':
                        rez = f - d;
                        Console.WriteLine("Разность = {0}", (rez).ToString());
                        break;

                    case '3':
                        rez = f * d;
                        Console.WriteLine("Произведение = {0}", (rez).ToString());
                        break;

                    case '4':
                    {
                        try
                        {
                            rez = f / d;
                        }
                        catch (ArgumentException e)
                        {
                            Console.WriteLine(e.Message);
                            break;
                        }
                        Console.WriteLine("Деление = {0:F}", (rez).ToString());
                    }
                    break;

                    case '5':
                        rez = -rez;
                        Console.WriteLine("Унарный минус = {0:F}", (rez).ToString());
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            Console.WriteLine("Знаменатель первой дроби до изменения равен {0:D}", f.numerator);
            f.numerator = 1;
            Console.WriteLine("Знаменатель первой дроби после изменения на 1 равен {0:D}", f.numerator);
            Console.WriteLine("Десятичная дробь {0:F} ", f.getValue());
        }
Exemplo n.º 13
0
 private static void isInit(Fraction fra1, Fraction fra2)
 {
     fra1.isInit();
     fra2.isInit();
 }
Exemplo n.º 14
0
 protected bool Equals(Fraction other)
 {
     return(Numerator * other.Denominator == Denominator * other.Numerator);
 }
Exemplo n.º 15
0
 public bool Equals(Fraction obj)
 {
     return(ToString() == obj.ToString());
 }
Exemplo n.º 16
0
 Fraction DivFraction(Fraction target)
 {
     return(Divide(this, target));
 }
Exemplo n.º 17
0
 Fraction MulFraction(Fraction target)
 {
     return(Multiply(this, target));
 }
Exemplo n.º 18
0
 public Fraction(BigInteger numer, Fraction deno)
 {
     Reduct();
 }
Exemplo n.º 19
0
 public Fraction(Fraction numer, BigInteger deno)
 {
     Reduct();
 }
Exemplo n.º 20
0
 public Fraction(Fraction numer, Fraction deno)
 {
     //BigInteger lcm = GCD.GCD.GetGCD(numer.Denominator, deno.Denominator).LCM;
     //numerator = lcm / numer.Denominator;
     Reduct();
 }
Exemplo n.º 21
0
 Fraction SubFraction(Fraction target)
 {
     return(Subtract(this, target));
 }
Exemplo n.º 22
0
        static void Main(string[] args)
        {
            bool endApp = false;

            Console.WriteLine("Fraction calculator in C#\r");
            Console.WriteLine("-------------------------\n");

            while (!endApp)
            {
                //First fraction enter and check
                Console.WriteLine("Enter first fraction:");

                string n1 = Console.ReadLine();
                int    cleanN1;

                while (!int.TryParse(n1, out cleanN1))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    n1 = Console.ReadLine();
                }

                string d1 = Console.ReadLine();
                int    cleanD1;

                while (!int.TryParse(d1, out cleanD1) || cleanD1 == 0)
                {
                    Console.Write("This is not valid input. Please enter an integer not zero value: ");
                    d1 = Console.ReadLine();
                }

                //Second fraction enter and check
                Console.WriteLine("Enter second fraction:");

                string n2 = Console.ReadLine();
                int    cleanN2;

                while (!int.TryParse(n2, out cleanN2))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    n2 = Console.ReadLine();
                }

                string d2 = Console.ReadLine();
                int    cleanD2;

                while (!int.TryParse(d2, out cleanD2) || cleanD2 == 0)
                {
                    Console.Write("This is not valid input. Please enter an integer not zero value: ");
                    d2 = Console.ReadLine();
                }

                //Create objects of Fraction class
                Fraction a = new Fraction(cleanN1, cleanD1);
                Fraction b = new Fraction(cleanN2, cleanD2);

                Console.WriteLine("Choose an operator from the following list:");
                Console.WriteLine("\ta - Add");
                Console.WriteLine("\ts - Subtract");
                Console.WriteLine("\tm - Multiply");
                Console.WriteLine("\td - Divide");
                Console.Write("Your option? ");

                string op     = Console.ReadLine();
                string result = "";

                Fraction c;

                switch (op)
                {
                case "a":
                    c      = Fraction.Add(a, b);
                    result = a.Output() + "+" + b.Output() + "=" + c.Output();
                    break;

                case "s":
                    c      = Fraction.Subtract(a, b);
                    result = a.Output() + "+" + b.Output() + "=" + c.Output();
                    break;

                case "m":
                    c      = Fraction.Multiply(a, b);
                    result = a.Output() + "+" + b.Output() + "=" + c.Output();
                    break;

                case "d":
                    c      = Fraction.Divide(a, b);
                    result = a.Output() + "+" + b.Output() + "=" + c.Output();
                    break;

                default:
                    break;
                }

                if (string.IsNullOrEmpty(result))
                {
                    Console.WriteLine("This operation will result in a mathematical error.\n");
                }
                else
                {
                    Console.WriteLine("Result: " + result);
                }

                Console.WriteLine("------------------------\n");

                Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
                if (Console.ReadLine() == "n")
                {
                    endApp = true;
                }

                Console.WriteLine("\n");
            }
            return;
        }