Question

Write a class Fraction which defines adding, subtracting, multiplying, and dividing fractions by overloading standard operators...

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.

Homework Answers

Answer #1

`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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to...
Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to create a data type SimpleCalculator capable of performing arithmetic operations. • Creating const member functions to enforce the principle of least privilege. The follow-up questions and activities also will give you practice: • Using constructors to specify initial values for data members of a programmer-defined class. Description of the Problem Write a SimpleCalculator class that has public methods for adding, subtracting, multiplying and dividing...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
Write a C++ program and algorithm for a class inheritance game where a kitten with random...
Write a C++ program and algorithm for a class inheritance game where a kitten with random position in the gameboard is to be caught by its mother cat whose initial position is center to board and speed to particular direction a little higher than the kitten. The class member functions include a draw function where the mother cat stays at the middle. The direction is measured by the input function when say press ‘4’ means LEFT, ‘6’ means RIGHT, ‘8’...
1.What is the difference between class/object? 2.Did you think about classes/objects around you since the last...
1.What is the difference between class/object? 2.Did you think about classes/objects around you since the last session? 3.What library do we need for processing file I/O? 4.What is the class for the input file stream? Give an example 5.What is the class for the output file stream? Give an example 6.Why do you want to use files instead of using input? 7.How do you read from a file? give an example 8.How do you write to a file? give an...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
c++ Program Description You are going to write a computer program/prototype to process mail packages that...
c++ Program Description You are going to write a computer program/prototype to process mail packages that are sent to different cities. For each destination city, a destination object is set up with the name of the city, the count of packages to the city and the total weight of all the packages. The destination object is updated periodically when new packages are collected. You will maintain a list of destination objects and use commands to process data in the list....
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file. Specifically, you will implement the following function: /* * Reads one arithmetic "expression" at a time from a file stream, computes, then * returns the result. If there are additional expressions in the file, they are * read and computed by successive calls to “calculator”. * * “Expressions” are groups of operations (add, subtract, multiply, divide). Your * calculator will read and...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT