changing negative fraction to positive in c++

Hi

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++?

Thank you in advance.
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 void operator<() 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!

Well I have three files really which are all part of the same thing. My class part is shown below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// file Fract.cpp
// a fraction Abstract Data Type

#include <iostream>
#include <cstdlib> // for abs()
#include "fract.h"

using namespace 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?

Any hints would be good.

Thanks in advance.
if a is an integer, then if a < 0 then -a is > 0.

a fraction is negative if either the numerator or denominator, but not both, are negative.
Whatever num and denom are...
1
2
3
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):
 
num = abs( num) ;

1
2
3
4
5
6
7
8
9
num=1;
denom=-1;
int sign = num * denom < 0 ? -1 : 1 ;
//sign=-1
num = sign * abs(num) ;
//num=-1*1=-1
denom = abs(denom) ;
//denom=1
//(no sign inversion) 
Topic archived. No new replies allowed.