-
Notifications
You must be signed in to change notification settings - Fork 0
/
79.cpp
84 lines (71 loc) · 1.42 KB
/
79.cpp
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
#include <iostream>
#include <iomanip>
using namespace std;
class Fraction
{
friend void addFraction(Fraction , Fraction);
private:
int iNumerator;
int iDenominator;
public:
Fraction(void);
Fraction(int, int);
void getData(void);
void showData(void);
void reduceFraction(void);
};
//void addFraction(Fraction , Fraction); WILL RESULT IN ERROR addFraction should be friend
Fraction ::Fraction(void)
{
iNumerator = 1;
iDenominator = 1;
}
Fraction ::Fraction(int i, int j)
{
iNumerator = i;
iDenominator = j;
}
void Fraction :: getData(void)
{
cout << "Enter numerator and denominator of the Fraction" << endl;
cin >> iNumerator >> iDenominator;
}
void Fraction :: showData(void)
{
cout << iNumerator << "/" << iDenominator << endl;
}
void Fraction :: reduceFraction(void)
{
int iVal1,iVal2,iRem;
iVal1 = iNumerator;
iVal2 = iDenominator;
while(iVal2!=0)
{
iRem = iVal1 % iVal2;
iVal1 = iVal2;
iVal2 = iRem;
}
iNumerator /= iVal1;
iDenominator /= iVal1;
}
void addFraction(Fraction a, Fraction b)
{
Fraction sum;
sum.iNumerator = (a.iNumerator * b.iDenominator) + (b.iNumerator * a.iDenominator);
sum.iDenominator = a.iDenominator * b.iDenominator;
cout << "\nSum is: ";
sum.showData();
sum.reduceFraction();
cout << "\nReduced form is: ";
sum.showData();
}
int main(void)
{
Fraction c1(5,3),c2(6,4);
addFraction(c1,c2);
c1.getData();
c2.getData();
addFraction(c1,c2);
cin.get();
return 0;
}