C++ A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers.
Details:
or when b is 0, then it should display #div0
The operations that should be implemented for the rational numbers are:
Addition |
Subtraction |
Multiplication |
Division |
Invert |
Mixed fraction |
Reduce |
Less than |
Less than or equal to |
Greater Than |
####################################### rational.cpp ####################################### #include <iostream> #include "rational.h" using namespace std; // A rational constructor which allows the numerator and denominator // to be specified. rational::rational(int n, int d){ numerator = n; if (d != 0) { denominator = d; } else { denominator = 1; } } // Another constructor. If no arguments specified, default to 0/1. rational::rational() { numerator = 0; denominator = 1; } // Get a rational from standard input, in the form "numerator/denominator." istream & operator>>(istream & in, rational & this_rational) { char divSign; // used to consume the '/' character during input in >> this_rational.numerator >> divSign >> this_rational.denominator; return in; } // Display a rational, in the form "numerator/denominator." ostream & operator<<(ostream & out, rational & f){ if (f.denominator == 1) { out << f.numerator; } else { out << f.numerator << '/' << f.denominator; } return out; } bool rational::operator< (const rational &f2) const{ return (f2 - *this).Evaluate() > 0; } bool rational::operator<= (const rational &f2) const{ return (f2 - *this).Evaluate() >= 0; } bool rational::operator> (const rational &f2) const{ return (f2 - *this).Evaluate() < 0; } // Evaluate returns the decimal equivalent of the rational double rational::Evaluate() { double n = numerator; // convert numerator to double double d = denominator; // convert denominator to double return (n / d); // compute and return double representation } // This function reduces a rational to its lowest terms. void rational::Reduce() { //Use the Euclidian algorithm to find the Greatest Common Denominator int dividend; // the number to be divided int divisor; // the number to divide into the dividend int remainder; if (numerator == 0) { return; } else if (numerator > denominator) { dividend = numerator; divisor = denominator; } else { dividend = denominator; divisor = numerator; } do { remainder = dividend % divisor; if (remainder != 0) { dividend = divisor; divisor = remainder; } } while (remainder != 0); //reduce by dividing numerator and denominator by the GCD which is currently in the divisor if (divisor != 0) { numerator /= divisor; denominator /= divisor; } } // Overload of operator "+" for rational addition rational rational::operator + (rational const &f2) const{ rational r; // the return value of f1 + f2 r.numerator = (this->numerator * f2.denominator) + (f2.numerator * this->denominator);// compute numerator r.denominator = this->denominator * f2.denominator;// compute denominator r.Reduce(); return r; // return the result } // Overload of operator "*" for rational addition rational rational::operator * (rational const &f2) const{ rational r; // the return value of f1 + f2 r.numerator = (this->numerator * f2.numerator);// compute numerator r.denominator = this->denominator * f2.denominator;// compute denominator r.Reduce(); return r; // return the result } // Overload of operator "+" for rational addition rational rational::operator - (rational const &f2) const{ rational r; // the return value of f1 + f2 r.numerator = (this->numerator * f2.denominator) - (f2.numerator * this->denominator);// compute numerator r.denominator = this->denominator * f2.denominator;// compute denominator r.Reduce(); return r; // return the result } // Overload of operator "*" for rational addition rational rational::operator / (rational const &f2) const{ rational r; // the return value of f1 + f2 r.numerator = (this->numerator * f2.denominator);// compute numerator r.denominator = this->denominator * f2.numerator;// compute denominator r.Reduce(); return r; // return the result } bool rational::operator==(const rational& l) const{ return (l.numerator == numerator) && (l.denominator == denominator); } ####################################### rational.h ####################################### #ifndef rational_H #define rational_H #include <iostream> using namespace std; class rational { // operator overload, so we can use standard C++ notation // Get a rational from keyboard. friend istream & operator >> (istream & in, rational & this_rational); public: // The counstructors rational(int n, int d = 1); // Set numerator = n, denominator = d. // if no second argument, default to 1 rational(); // Set numerator = 0, denominator = 1. // The print function friend ostream & operator << (ostream & out, rational & this_rational); rational operator + (rational const &f2) const; rational operator * (rational const &f2) const; rational operator - (rational const &f2) const; rational operator / (rational const &f2) const; bool operator==(const rational& lhs) const; // Overloading operators for addition and incrementing //rational operator++ (); // prefix only //Overloading operators for comparison bool operator< (const rational &f2) const; bool operator<= (const rational &f2) const; bool operator> (const rational &f2) const; double Evaluate(); // Return the decimal value of a rational void Reduce(); // Reduce the rational to lowest terms. private: int numerator; // top part int denominator; // bottom part }; #endif ####################################### main.cpp ####################################### #include <iostream> // for cout #include "rational.h" // for rational declarations using namespace std; int main() { // Try all three possible rational constructors // and the input/output routines. rational f1(3, 2), f2(4), f3(68, 153), f4; // four test rationals // Output the pre-initialized rationals. cout << "\nThe rational f1 is " << f1; cout << "\nThe rational f2 is " << f2; cout << "\nThe rational f3 is " << f3; f3.Reduce(); cout << "\nThe rational f3 reduced is " << f3; cout << "\nThe rational f4 is " << f4; // Replace add with the overloaded operator +. f4 = f1 + f2; cout << "\n\nThe sum of " << f1 << " and " << f2 << " is " << f4 << endl; // Find the floating-point value of f4 cout << "\nThe value of this rational is " << f4.Evaluate() << '\n'; // Read a rational from the keyboard and print it. cout << "\nNow enter a rational of your own: "; cin >> f3; cout << "\nYou entered " << f3 << endl; // Test the ++ operator /*cout << endl << endl; f3.Show(); cout << " incremented by one is "; ++f3; f3.Show(); cout << endl;*/ //system("PAUSE"); //Test the * operator cout << f3 << " * " << f4; rational result = f3 * f4; cout << " is " << result << endl; //Test the == operator rational x(3, 2); cout << "Testing " << x << " & " << f1 << " equality: " << (x == f1) << endl; return 0; }
Hi. Please find the answer above.. In case of any doubts, you may
ask in comments. You may upvote the answer if you feel i did a good
work!
C++ A rational number is of the form a/b, where a and b are integers, and...
C++ A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers.Pointers aren't needed and it should be able to handle all of these examples and more. Details: Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of...
C++ A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers.Pointers aren't needed and it should be able to handle all of these examples and more. Its supposed to accept a rational number dynamically in the form of "a/b" and tell you the result. Details: Your program should have 3 files: a driver file to test the operations, a header file...
General Description: A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers. Details: Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of the items in the header file. The class should read and display all rational...
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...
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...
(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...
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...
A complex number is a number of the form a + bi, where a and b are real numbers √ and i is −1. The numbers a and b are known as the real and the imaginary parts, respectively, of the complex number. The operations addition, subtraction, multiplication, and division for complex num- bers are defined as follows: (a+bi)+(c+di) = (a+c)+(b+d)i (a+bi)−(c+di) = (a−c)+(b−d)i (a + bi) ∗ (c + di) = (ac − bd) + (bc + ad)i (a...
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...
C++ only
I'm using code lite editor as well
Please add comments and pre and post conditions too
9:16 AM .oooo Verizon GI 98% Use C++ only Please include comments and pre post conditions am also using a new editor called code lite write a class for rational numbers. Each 15 object in the class should have two inte values that define the rational number: the numerator and the denominator. For example, the fraction 516 would have a denominator of...