Exemplo n.º 1
0
        public static decimal CalculateFractionFromString(this string expression)
        {
            Regex regex = new Regex(@"\s*(\d+)\s*\/(\d+)\s*([-+/*])\s*(\d+)\s*\/(\d+)\s*");
            Match m = regex.Match(expression);

            long f1Numerator = Int64.Parse(m.Groups[1].Value);
            long f1Denomimator = Int64.Parse(m.Groups[2].Value);
            string oper = m.Groups[3].Value;
            long f2Numerator = Int64.Parse(m.Groups[4].Value);
            long f2Denominator = Int64.Parse(m.Groups[5].Value);

            Fraction f1 = new Fraction(f1Numerator, f1Denomimator);
            Fraction f2 = new Fraction(f2Numerator, f2Denominator);
            Fraction result = new Fraction();

            if (oper == "+")
            {
                result = f1 + f2;
            }
            else if (oper == "-")
            {
                result = f1 - f2;
            }
            else if (oper == "*")
            {
                result = f1 - f2;
            }
            else if (oper == "/")
            {
                result = f1 - f2;
            }

            return Fraction.ToDecimal(result);
        }
Exemplo n.º 2
0
        public static void Main()
        {
            Fraction f1 = new Fraction(Int64.MaxValue, 31);
            Fraction f2 = new Fraction(40, 4);

            Fraction result = f1 * f2;

            Console.WriteLine(result.Numerator);
            Console.WriteLine(result.Denominator);
            Console.WriteLine(result);

            //Console.WriteLine("Please enter a fraction expression (i.e 1/4 + 4/5):");
            //string expression = Console.ReadLine();

            //Console.WriteLine(expression.CalculateFractionFromString());
        }
Exemplo n.º 3
0
 public static decimal ToDecimal(Fraction f)
 {
     return (decimal) f.Numerator / f.Denominator;
 }