I'm recently doing an assignment for fraction problem. I've implemented classes for the programme to accept fractions. And i've overloaded operator +, -, *, / and ==...
But here is some strange thing.
Before that i show some of my code here regarding operator -:

Fraction operator-( Fraction a, Fraction b )
{
    Fraction result;
    result.numerator = a.numerator * b.denominator - b.numerator * a.denominator;
    result.denominator = a.denominator * b.denominator;
    result.simplify();
    return result;
}

The code is simple, it will minus directly with 2 fraction which is fraction a and b.

but here is a strange thing:

2/5 - 1/2 = 1/-10

actually it will have user to cin the answer, if i gv the answer -1/10 the bool will have to check but fortunately the compiler is clever enough knowing that 1/-10 == -1/10

but sometimes if the user put wrong answer and i would like the program to show the correct answer, but not 1/-10, i want it to show -1/10

is there anything i should modify?

Thx

Recommended Answers

All 3 Replies

but if the question is:

1/10 - 1/2 = -2/5

the answer show the negative infront.

Weird...

sorry but your post doesnt make sense. if we use 2/5 - 1/2 with your formula we get:
numerator = (2 * 2) - (1 * 5) = 4 - 5 = -1
denominator = (2 * 5) = 10
so your fraction should be -1/10. not sure why you are not getting it unless it has something to do with your simplify function

Re-factor the simplify function which finally converts a/-b to -a/b. This will maintain uniformity and thus the program will be unambiguous.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.