Write a class Fraction which defines adding, subtracting, multiplying, and dividing fractions by overloading standard operators for these operations. Write a function member for reducing factors and overload I/O operators to input and output fractions.
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
#include <iostream>
#include <string>
class Fraction {
private:
int numerator, denominator;
public:
//accessor methods
void setNumerator(int);
void setDenominator(int);
int getNumerator();
int getDenominator();
//constructors
Fraction();
Fraction(int, int);
//operator overloading methods
Fraction operator+ (Fraction);
Fraction operator- (Fraction);
Fraction operator* (Fraction);
Fraction operator/ (Fraction);
//utility methods
std::string print();
int GCD(int, int);
void reduce();
void negateQuot();
};
//accessor method implementations
void Fraction::setNumerator(int n) { numerator = n; }
void Fraction::setDenominator(int d) { denominator = d; }
int Fraction::getNumerator() { return numerator; }
int Fraction::getDenominator() { return denominator; }
//constructor implementations
Fraction::Fraction() {
numerator = 1;
denominator = 1;
reduce();
negateQuot();
}
Fraction::Fraction(int a, int b) {
numerator = a;
denominator = b;
}
//operator overloading method implementations
Fraction Fraction::operator* (Fraction b) {
return Fraction((numerator * b.getNumerator()), (denominator *
b.getDenominator()));
}
Fraction Fraction::operator/ (Fraction b) {
return Fraction((numerator * b.getDenominator()), (denominator *
b.getNumerator()));
}
Fraction Fraction::operator+ (Fraction b) {
if (numerator == 0 || b.getNumerator() == 0) {
if (numerator != 0) {
return Fraction(numerator, denominator);
}
else {
return Fraction(b.getNumerator(), b.getDenominator());
}
}
else {
return Fraction((numerator * b.getDenominator()) + (denominator *
b.getNumerator()), denominator * b.getDenominator());
}
}
Fraction Fraction::operator- (Fraction b) {
if (numerator == 0 || b.getNumerator() == 0) {
if (numerator != 0) {
return Fraction(numerator, denominator);
}
else {
return Fraction(b.getNumerator(), b.getDenominator());
}
}
else {
return Fraction(numerator * b.getDenominator() - (denominator *
b.getNumerator()), (denominator * b.getDenominator()));
}
}
std::string to_string(int number){
std::string number_string = "";
char ones_char;
int ones = 0;
while(true){
ones = number % 10;
switch(ones){
case 0: ones_char = '0'; break;
case 1: ones_char = '1'; break;
case 2: ones_char = '2'; break;
case 3: ones_char = '3'; break;
case 4: ones_char = '4'; break;
case 5: ones_char = '5'; break;
case 6: ones_char = '6'; break;
case 7: ones_char = '7'; break;
case 8: ones_char = '8'; break;
case 9: ones_char = '9'; break;
default : break;
}
number -= ones;
number_string = ones_char + number_string;
if(number == 0){
break;
}
number = number/10;
}
return number_string;
}
//utility method implementations
std::string Fraction::print() {
return to_string(numerator) + " / " + to_string(denominator);
}
int Fraction::GCD(int a, int b) {
if (a == 0 || b == 0) {
return 0;
}
else {
int a1, b1, c = 0;
if (a >= b) {
a1 = a;
b1 = b;
}
else {
a1 = b;
b1 = a;
}
while (b1 != 0) {
c = a1 % b1;
a1 = b1;
b1 = c;
}
return a1;
}
}
void Fraction::reduce() {
if (numerator == 0 || denominator == 0) {
numerator = 0;
denominator = 0;
}
else {
int gcd = GCD(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
}
void Fraction::negateQuot() {
if ((numerator < 0 && denominator < 0) || (numerator
>= 0 && denominator < 0)) {
numerator = -numerator;
denominator = -denominator;
}
}
int main(int argc, const char *argv[]) {
int a,b;
std::cout << "Give me the first numerator: ";
std::cin >> a;
std::cout << "Give me the second denominator: ";
std::cin >> b;
Fraction a1(a,b);
std::cout << "The first fraction is " << a1.print()
<< std::endl;
std::cout << "Give me the second numerator: ";
std::cin >> a;
std::cout << "Give me the second denominator: ";
std:: cin >> b;
Fraction a2(a,b);
std::cout << "The second fraction is " << a2.print()
<< std::endl;
std::cout << "The following operations were completed:" << std::endl;
Fraction a3 = a1 + a2;
std::cout << "ADDITION: " << a1.print() << " + "
<< a2.print() << " is " << a3.print() <<
std::endl;
a3 = a1 - a2;
std::cout << "SUBTRACTION: " << a1.print() << " -
" << a2.print() << " is " << a3.print() <<
std::endl;
a3 = a1 * a2;
std::cout << "MULTIPLICATION: " << a1.print() <<
" * " << a2.print() << " is " << a3.print()
<< std::endl;
a3 = a1 / a2;
std::cout << "DIVISION: " << a1.print() << " / "
<< a2.print() << " is " << a3.print() <<
std::endl;
return 0;
}
Kindly revert for any queries
Thanks.
Get Answers For Free
Most questions answered within 1 hours.