bool positive in Fraction class

I got everything implemented and set to finish. However, I have no clue how to implement the bool positive data member in the Function class. Does anyone have any idea? Thanks in advance!

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  #include <cstdlib>
#include <cmath>
#include <iostream>

using namespace std;

class Fraction
{
private:
    int numerator;
    int denom;
    bool positive;
public:
    Fraction fracAdd(Fraction b);
    Fraction fracSub(Fraction b);
    Fraction fracMult(Fraction b);
    Fraction fracDiv(Fraction b);
    
    void inputFrac();
    void printFrac();
};

Fraction Fraction::fracAdd(Fraction b)
{
    Fraction result;
    
    result.numerator = (numerator * b.denom) + (denom * b.numerator);
    result.denom = denom * b.denom;
    
    return result;
}

Fraction Fraction::fracSub(Fraction b)
{
    Fraction result;
    
    result.numerator = (numerator * b.denom) - (denom * b.numerator);
    result.denom = denom * b.denom;
    
    return result;
}

Fraction Fraction::fracMult(Fraction b)
{
    Fraction result;
    
    result.numerator = numerator * b.numerator;
    result.denom = denom * b.denom;
    
    return result;
}

Fraction Fraction::fracDiv(Fraction b)
{
    Fraction result;
    
    result.numerator = numerator * b.denom;
    result.denom = denom * b.numerator;
    
    return result;
}

void Fraction::inputFrac()
{
    cout << "Input the numerator: ";
    cin >> numerator;
    cout << "Input the denominator: ";
    cin >> denom;
}

void Fraction::printFrac()
{
    if (!positive)
    {
        cout << "-";
    }
    cout << numerator << " / " << denom << endl;
}

int main()
{
    Fraction f1, f2, fresult;
    
    f1.inputFrac();
    f2.inputFrac();
    
    cout << "The result of a + b is: ";
    fresult = f1.fracAdd(f2);
    fresult.printFrac();
    
    cout << "The result of a - b is: ";
    fresult = f1.fracSub(f2);
    fresult.printFrac();
    
    cout << "The result of a * b is: ";
    fresult = f1.fracMult(f2);
    fresult.printFrac();
    
    cout << "The result of a / b is: ";
    fresult = f1.fracDiv(f2);
    fresult.printFrac();

    return 0;
}
This is probably the way you want to do it - make sure that everything is positive, and set the 'positive' flag based on other things. For example, if you subtract a Fraction and it becomes negative, set the numerator to be positive again and set the 'positive' flag to false. When multiplying, set the 'positive' flag to be something like this->positive = (this->positive == other.positive);. And so on. hope this helps!
Topic archived. No new replies allowed.