Header file for the Rational class:
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational & ) const; // function
addition
Rational subtraction( const Rational & ) const; // function
subtraction
Rational multiplication( const Rational & ) const; // function
multi.
Rational division( const Rational & ) const; // function
division
void printRational () const; // print rational format
void printRationalAsDouble() const; // print rational as double
format
private:
int numerator; // integer numerator
int denominator; // integer denominator
void reduce(); // utility function
}; // end class Rational
#endif
Rational Class:
// Member-function definitions for class Rational.
#include "stdafx.h"
#include
#include "Rational.h" // include definition of class Rational
using namespace std;
Rational::Rational(int n, int d)
: numerator(n), denominator(d)
{
reduce(); // store the fraction in reduced form
} // end Rational constructor
Rational Rational::addition(const Rational &a) const
{
Rational t(a.numerator * denominator + a.denominator *
numerator,
a.denominator * denominator);
t.reduce(); // store the fraction in reduced form
return t;
} // end function addition
Rational Rational::subtraction(const Rational &s) const
{
Rational t(s.denominator * numerator - denominator *
s.numerator,
s.denominator * denominator);
t.reduce(); // store the fraction in reduced form
return t;
} // end function subtraction
Rational Rational::multiplication(const Rational &m)
const
{
Rational t(m.numerator * numerator, m.denominator *
denominator);
t.reduce(); // store the fraction in reduced form
return t;
} // end function multiplication
Rational Rational::division(const Rational &v) const
{
Rational t(v.denominator * numerator, denominator *
v.numerator);
t.reduce(); // store the fraction in reduced form
return t;
} // end function division
void Rational::printRational() const
{
if (denominator == 0) // validates denominator
cout << "\nDIVIDE BY ZERO ERROR!!!" << '\n';
else if (numerator == 0) // validates numerator
cout << 0;
else
cout << numerator << '/' << denominator;
} // end function printRational
oid Rational::printRationalAsDouble() const
{
cout << static_cast< double >(numerator) /
denominator;
} // end function printRationalAsDouble
void Rational::reduce()
{
int largest = numerator > denominator ? numerator :
denominator;
int gcd = 0; // greatest common divisor
for (int loop = 2; loop <= largest; ++loop)
if (numerator % loop == 0 && denominator % loop == 0)
gcd = loop;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
} // end if
} // end function reduction
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...
13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...
C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...
If you have already answered this question, please do
not repost your old solutions, you will be thumbs downed. I'm
looking for NEW solutions only!
Frac.h:
// a Fraction object holds one Fraction number, one
fraction
#ifndef FRAC_H
#define FRAC_H
#include <iostream>
using namespace std;
//Creaing a Fraction class
class Fraction {
public:
Fraction(int = 0, int = 1);
// Function Declarations which performs operations on Fraction
class
Fraction add(const Fraction &);
Fraction subtract(const Fraction& a);
Fraction multiply(const Fraction& a);...
C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...
(Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...
Make a new class called Rational to represent rational numbers in C++ language. Use a long to store each part (numerator and denominator). Be sure your main function fully test the class. 1. Create a helper function called gcd to calculate the greatest common divisor for the two given parameters. See Euclid’s algorithm (use google.com). Use this function to always store rationals in “lowest terms”. 2. Create a default, one argument and two argument constructor. For the two argument constructor,...
Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...
Please use Java language. Thanks in advance.
HOME WORK due 09. 18.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 2...
In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator; public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...
C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression could produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call...