determining the sign of the operator

Ive finished writing my code (besides the addition and subtraction functions being right) and now I have to calculate the sign to put with the fraction. I was given the proper print function but I need help figuring out how to assign positive or negative to the inputted values and determine if the sign is to be + or -
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
 
class fraction

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

 void fraction::printFrac()
{
    if (!positive)
    {
    cout << "-";
    }
    cout << numerator << " / " << denom;
}
void fraction::inputFrac()
{    
 char tempchar1;
    
    cout<<"Please input the numerator ";
    cin>>numerator;
    cout<< "Please input the denominator ";
    cin>>denom;
    cout<<"Is the fraction positive? (Y or N);
    cin>> tempchar1; //This is where i think i need to assign a temporary character to Y and then have it return positive

   if(tempchar1=='Y')
{
positive;
}
}
 fraction fraction::fracMult(fraction& b)
{
     fraction temp;
     
     
     temp.numerator = numerator * b.numerator;
     temp.denom = denom * b.denom;
     
     return temp;
}
 fraction fraction::fracAdd(fraction& b)
 {
     fraction temp; 
     temp.numerator=numerator + b.numerator;
     temp.denom=denom + b.denom;
     
     return temp;
 }
 fraction fraction::fracDiv(fraction& b)
 {
     fraction temp;
     temp.numerator = numerator * b.denom;
     temp.denom = denom * b.numerator;

     
     return temp;

 }
 fraction fraction::fracSub (fraction& b)
 {
     
    fraction temp;
     temp.numerator = numerator - b.numerator;
     temp.denom = denom - b.denom;

     return temp;   
 }
int main(int argc, char** argv) {

    fraction f1, f2, fresult;
    
    f1.inputFrac(); //input the first fraction
    f2.inputFrac(); //input the second fraction
    cout<<endl;
    f1.printFrac();
    cout<<endl;
    f2.printFrac();
    cout<<endl;

    cout << "The result of a * b is: ";
    
    fresult = f1.fracMult(f2); // calculate a * b
    fresult.printFrac(); // print out the result
    
    cout<<endl;
    
    cout << "The result of a + b is: ";// calculate a + b
    fresult = f1.fracAdd(f2); // print out the result
    fresult.printFrac();
    
    cout<<endl;
    
    cout << "The result of a / b is: ";// calculate a / b
    fresult = f1.fracDiv(f2);// print out the result
    fresult.printFrac();
    
    cout<<endl;
    
    cout << "The result of a-b is: ";// calculate a - b
    fresult = f1.fracSub(f2); // print out the result
    fresult.printFrac();
    
    cout<<endl;
    
    return 0;
} 
Last edited on
closed account (48T7M4Gy)
if numerator/denominator < 0
then
positive = false
otherwise
positive = true
Im trying to give them the option to select by enter Y or N if the fraction is positive Im having trouble passing the result from the input function to the print function
closed account (48T7M4Gy)
select by enter Y or N if the fraction is positive

OK - I missed that.

Since positive is one of the fraction classes properties why not have a
void setSign( bool aSign) method and accompanying bool getSign() method?

Also your "constructor" could have an extra bool aSign parameter along with the numerator and denominator parameters you already have.

(This raises the question why not have a formal class constructor, it's only about 1 or 2 extra lines of code, 3 or 4 if you include a destructor)

how would that look in my code if you dont mind me asking? or would there be anyway to process the positive in the inputfrac function that can then be printed out by the print function?
Last edited on
closed account (48T7M4Gy)
Give me a few minutes, you'll see it's not too complicated
Thank you and I would perfer the method of updating the positive variable in the input frac function cuz thats what my assignment calls for if thats possible

Note: I just updated the code to show you what i mean by the positive in the input frac function
Last edited on
closed account (48T7M4Gy)
See if this makes sense to your assignment requirement :)
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
#include <iostream>
#include <cmath>

using namespace std;

class fraction
{
private:
	int numerator;
	int denominator;
	bool positive;

public:
	fraction(int = 0, int = 1);
	
	~fraction() {};

	fraction fracAdd(fraction, fraction);
	void print();
	void setSign(int, int);
	bool getSign();
};

fraction::fraction(int aNumerator, int aDenominator)
	{
		numerator = aNumerator;
		denominator = aDenominator;
		setSign(numerator, denominator);
	}

void fraction::print()
{
	if (positive == false)
		cout << "-";

	cout << abs(numerator) << " / " << abs(denominator) << endl;
}

fraction fraction::fracAdd(fraction a, fraction b)
{
	fraction result;
	result.numerator = a.numerator * b.denominator + b.numerator * a.denominator;
	result.denominator = a.denominator * b.denominator;
	result.setSign(result.numerator, result.denominator);

	return result;
}

bool fraction::getSign()
{
	return positive;
}

void fraction::setSign(int aNumerator, int aDenominator)
{
	if (aNumerator * aDenominator < 0)
		positive = false;
	else
		positive = true;
}

int main()
{
	fraction f1(1, 7);
	f1.print();

	fraction f2(-3, 4);
	f2.print();

	fraction f3 = f3.fracAdd(f1, f2);
	f3.print();

	if (f3.getSign() == true)
		cout << "f3 is positive" << endl;
	else
		cout << "f3 is negative" << endl;
        
        return 0;
}


Integer divison made the test very tedious. But fixed now I reckon.
Last edited on
closed account (48T7M4Gy)
updating the positive variable in the input frac function


You can put the same test I have just about anywhere that suits. The advantage with the class/object oriented approach is the fraction object carries the sign with itself explicitly once the object property/attribute is updated just once. :)
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/179741/
Topic archived. No new replies allowed.