I am having a little difficulty in knowing how to change a fraction into positive if it is currently negative. Does anyone know how to achieve this in c++?
If a fraction is some custom class you have several options:
* implement an void abs() function
* think of how you could change (-2/3) to (2/3)? You are only allowed the operations +,-,*,/. Apply the same to your fraction class!
If you want to know if the fraction is negative:
* either consult the voidoperator<() of your fraction class
* think of how you find out the sign in the four cases (-2)/(-3), (-2)/(3), (2)/(-3), (2)/(3) and apply the same to your fraction class!
// file Fract.h
// a fraction Abstract Data Type
#ifndef FRACTION_H
#define FRACTION_H
// The DOMAIN OF VALUES
class Fraction
{
private:
int num;
int denom;
public:
// The SET OF OPERATIONS
Fraction(); // default constructor
// initialises fraction with numerator up,
// denominator down
// returns 0 if no error
int create(int up, int down);
// displays in form up/down
void display();
// reduce to lowest terms
void simplify();
// adds another to produce third in simplified form
void add(Fraction, Fraction&);
// and now the others
void subtract(Fraction, Fraction&);
void multiply(Fraction, Fraction&);
void divide(Fraction, Fraction&);
};
#endif
In another part called fract.cc I already have the abs() function but this is part of Euclid's algorithm (as shown below)
// file Fract.cpp
// a fraction Abstract Data Type
#include <iostream>
#include <cstdlib> // for abs()
#include "fract.h"
usingnamespace std;
int GreatestCommonDivisor(int, int);
Fraction::Fraction()
{
num = 0;
denom = 0;
}
int Fraction::create(int up, int down)
{
// temporary solution (no error checking)
int errorCode = 0;
num = up;
denom = down;
return errorCode;
}
void Fraction::display()
{
cout << num << '/' << denom;
}
void Fraction::simplify()
{
// use Euclid's algorithm to find gcd
int gcd = GreatestCommonDivisor(num,
denom);
num /= gcd;
denom /= gcd;
}
void Fraction::add(Fraction f2, Fraction& f3)
{
int a_denom = denom * f2.denom ;
int a_num = (num * f2.denom) + (f2.num * denom) ;
f3.create(a_num,a_denom) ;
f3.simplify();
}
void Fraction::subtract(Fraction f2,
Fraction& f3)
{
//f3 =
}
void Fraction::multiply(Fraction f2,
Fraction& f3)
{
//f3 =
}
void Fraction::divide(Fraction f2,
Fraction& f3)
{
//f3 =
}
// Euclid's algorithm
int GreatestCommonDivisor(int a, int b)
{
a = abs(a);
b = abs(b);
int temp = a % b;
while (temp > 0)
{
a = b;
b = temp;
temp = a % b;
}
return b;
}
All i need to do is shange the create() function so that it can do the following:
# If both numerator and denominator are positive then use them as they are in the fraction.
# If both are negative then make them both positive in the fraction.
# If only one is negative then the fraction should be negative with a negative numerator and a positive denominator.
But as I have no real idea on how change negative fractions to positive, I am stuck. Can you please point me in the right direction as to what I need to be doing?
int sign = num * denom < 0 ? -1 : 1 ; // Sign of the fraction
num = sign * abs(num) ; // sign is appended to the numerator if necessary
denom = abs(denom) ; // denom is always positive
... now they have the correct signs!
To make a fraction positive afterwards (if wanted):