-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path11.cpp
114 lines (95 loc) · 2.51 KB
/
11.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
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
/*
Modify the four-function fraction calculator of Exercise 12 in Chapter 5 to use a
fraction class rather than a structure. There should be member functions for input and
output, as well as for the four arithmetical operations. While you’re at it, you might as
well install the capability to reduce fractions to lowest terms. Here’s a member function
that will reduce the fraction object of which it is a member to lowest terms. It finds the
greatest common divisor (gcd) of the fraction’s numerator and denominator, and uses this
gcd to divide both numbers.
*/
// author @Nishant
#include<iostream>
using namespace std;
class calF{
int num, den;
public:
calF():num(0), den(0)
{ }
void getFraction();
void arithOperation(calF, calF, char);
void dispFraction() const;
void lowTerms();
};
void calF::getFraction() {
char temp;
cout << "\nEnter a fraction in a/b form: ";
cin >> num >> temp >> den;
}
void calF::arithOperation(calF frac1, calF frac2, char operatr) {
switch(operatr) {
case '+':
cout << "\nAddition of two fraction is: ";
num = (frac1.num * frac2.den) + (frac1.den * frac2.num);
den = frac1.den * frac2.den;
break;
case '-':
cout <<"\nSubtraction of two fraction is: ";
num = (frac1.num * frac2.den) - (frac1.den * frac2.num);
den = frac1.den * frac2.den;
break;
case '*':
cout <<"\nMultiplication of two fraction is: ";
num = frac1.num * frac2.num;
den = frac1.den * frac2.den;
break;
case '/':
cout <<"\nDivision of two fraction is: ";
num = frac1.num * frac2.den;
den = frac1.den * frac2.num;
break;
}
}
void calF::dispFraction() const{
cout << num << "/" << den;
}
void calF::lowTerms() {
long tnum, tden, temp, gcd;
tnum = labs(num);
tden = labs(den);
if(tden == 0) {
cout <<"Illegal fraction: division by 0";
exit(1);
} else if (tnum == 0) {
num = 0;
den = 1;
return;
}
while(tnum != 0) {
if(tnum <tden) {
temp = tnum;
tnum = tden;
tden = temp;
}
tnum = tnum - tden;
}
gcd = tden;
num = num/gcd;
den = den/gcd;
cout <<num <<"/" <<den;
cout <<endl;
}
int main(){
calF fraction1, fraction2, fraction3;
char oprt;
cout <<"\nEnter first fraction: ";
fraction1.getFraction();
cout <<"\nEnter second fraction: ";
fraction2.getFraction();
cout <<"\nEnter an operator for arithmatic operation: ";
cin >>oprt;
fraction3.arithOperation(fraction1, fraction2, oprt);
fraction3.dispFraction();
cout <<"\nLoswest terms of fraction is: ";
fraction3.lowTerms();
return 0;
}