Question Fraction Calculator problem

Angel

New member
Joined
Dec 15, 2013
Messages
1
Programming Experience
Beginner
Hello would someone please help me with my program, I'm stuck all day with this thing. The program runs, but I think the problem is a logical error. The fraction 1/4 + 2 1/2 should be equal to 2 3/4 but the program's result is 2 3/8. Another thing is the expression 1/8 + 2 1/2 should be equals to 2 5/8. Please help me how to fix the codes. Thank you very much!

C#:
using System;
 
class FractionDemo
{
    static void Main(string[] args)
    {
        Fraction firstfraction = new Fraction();
        Fraction secondfraction = new Fraction();
        firstfraction.Numerator = 1;
        firstfraction.Denominator = 4;
        secondfraction.Numerator = 1;
        secondfraction.Denominator = 8;
        secondfraction.WholeNumber = 2;
        Fraction add = new Fraction();
        add = firstfraction + secondfraction;
 
        Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
        Console.WriteLine(" + {0}/{1} = {2}/{3}", secondfraction.Numerator, secondfraction.Denominator, add.Numerator, add.Denominator);
        Console.Write("\n {0}/{1}", firstfraction.Numerator, firstfraction.Denominator);
        Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator,secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
        Console.Write("\n {0}/{1}", firstfraction.Numerator, secondfraction.Denominator);
        Console.WriteLine(" + {0} {1}/{2} = {3} {4}/{5}", secondfraction.WholeNumber, secondfraction.Numerator, secondfraction.WholeNumber, add.WholeNumber, add.Numerator, add.Denominator);
        Console.ReadLine();
 
    }
 
    public class Fraction
    {
        private int wholenumber;
        private int numerator;
        private int denominator;
 
        public int WholeNumber
        {
            get
            {
                return wholenumber;
            }
            set
            {
                wholenumber = value;
            }
        }
 
        public int Numerator
        {
            get
            {
                return numerator;
            }
            set
            {
                numerator = value;
            }
        }
 
        public int Denominator
        {
            get
            {
                return denominator;
            }
            set
            {
                denominator = value;
                if (denominator > 0)
                {
                    denominator = value;
                }
                else
                {
                    denominator = 1;
                }
            }
        }
 
        public Fraction(int wholenumber, int numerator, int denominator)
            : this(numerator, denominator)
        {
            WholeNumber = wholenumber;
        }
 
        public Fraction(int numerator, int denominator)
        {
            WholeNumber = 0;
            Numerator = numerator;
            Denominator = denominator;
        }
 
        public Fraction()
        {
            WholeNumber = 0;
            Numerator = 0;
            Denominator = 1;
        }
 
        public int gcd()
        {
            int x = Numerator;
            int y = Denominator;
            int m;
 
            if (x > y)
                m = y;
            else
                m = x;
 
            for (int i = m; i >= 1; i--)
            {
                if (x % i == 0 && y % i == 0)
                {
 
                    return i;
 
                }
            }
            return 1;
        }
 
        public void Reduce()
        {
            int gcdNum = gcd();
 
            if (gcdNum != 0)
            {
                Numerator = Numerator / gcdNum;
                Denominator = Denominator / gcdNum;
            }
            if (Denominator < 0)
            {
                Denominator = Denominator * -1;
                Numerator = Numerator * -1;
            }
            convertFraction();
        }
 
        public void convertFraction()
        {
            WholeNumber = Numerator / Denominator;
            Numerator = Numerator % Denominator;
        }
 
        public static Fraction operator +(Fraction firstfraction, Fraction secondfraction)
        {
            int firstNum = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator;
            int secondNum = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator;
 
            Fraction Result = new Fraction();
 
            Result.Numerator = firstNum * secondfraction.Denominator + firstfraction.Denominator * secondNum;
            Result.Denominator = firstfraction.Denominator * secondfraction.Denominator;
            Result.Reduce();
            return Result;
        }
    }
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,278
Location
Sydney, Australia
Programming Experience
10+
Have you actually debugged your code or have you just read it? Obviously reading the code is the first step but, if that doesn't yield a cause, the next step is to debug and watch your code in action. Set a breakpoint and then step through your code line by line. At each step, you can check the state of all your variables, etc. As soon as the state of the app differs from your expectations, you know you've found an issue and you can hone in on it right there.
 
Top Bottom