Beispiel #1
0
        public string ToStringWithIntegerPart(Structures.Frac frac)
        {
            string result = "";

            if (frac.nom == 0)
            {
                result += "0";
            }
            else
            {
                long intPart = Math.Abs(frac.nom) / Math.Abs(frac.denom);
                if (frac.nom * frac.denom < 0)
                {
                    result += "-";
                }
                long newNom = Math.Abs(frac.nom) % Math.Abs(frac.denom);
                if (intPart != 0)
                {
                    result += intPart;
                }
                if (newNom != 0)
                {
                    result += " " + new Structures.Frac(newNom, Math.Abs(frac.denom));
                }
            }

            return(result);
        }
Beispiel #2
0
        public Structures.Frac multiply(Structures.Frac frac1, Structures.Frac frac2)
        {
            long newNom   = frac1.nom * frac2.nom;
            long newDenom = frac1.denom * frac2.denom;
            long greatestCommonDivisor = findGreatestCommonDivisor(Math.Abs(newNom), Math.Abs(newDenom));

            return(new Structures.Frac(newNom / greatestCommonDivisor, newDenom / greatestCommonDivisor));
        }
Beispiel #3
0
        public Structures.Frac divide(Structures.Frac frac1, Structures.Frac frac2)
        {
            if (frac2.nom == 0)
            {
                throw new ArithmeticException("Dividing by zero!");
            }

            return(multiply(frac1, new Structures.Frac(frac2.denom, frac2.nom)));
        }
Beispiel #4
0
        public Structures.Frac caclSum1(int n)
        {
            Structures.Frac frac = new Structures.Frac(0, 1);
            for (int i = 1; i <= n; i++)
            {
                Structures.Frac tmp = new Structures.Frac(1, i * (i + 1));
                frac = plus(frac, tmp);
            }

            return(frac);
        }
Beispiel #5
0
        public Structures.Frac caclSum2(int n)
        {
            if (n <= 1)
            {
                throw new ArgumentException("For second sum 'n' must be greater than 1!");
            }
            Structures.Frac frac = new Structures.Frac(1, 1);
            for (int i = 2; i <= n; i++)
            {
                Structures.Frac tmp = new Structures.Frac(i * i - 1, i * i);
                frac = multiply(frac, tmp);
            }

            return(frac);
        }
Beispiel #6
0
 public Structures.Frac minus(Structures.Frac frac1, Structures.Frac frac2)
 {
     return(plus(frac1, new Structures.Frac(-frac2.nom, frac2.denom)));
 }
Beispiel #7
0
 public double doubleVal(Structures.Frac frac)
 {
     return((double)frac.nom / frac.denom);
 }