Question

13.21 Lab: Rational class This question has been asked here before, but every answer I have...

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 accurately. Certainly double, which is usually stored as a 64-bit value, is far better than the old float, which is only 32 bits, but problems do arise. For example:

float n = 2.0;
float d = 3.0;
cout << precision(17);
cout << n / d << endl;

produces 0.6666668653488159, which is accurate to only 8 decimal places - a bit dirty for a discipline that prides itself on precision!

A solution that is often used when precision is of greatest importance and all of the numbers involved are going to be "rational" (that is, expressible as a 'ratio' of two integers - i.e. a fraction) is to use a custom data type - i.e. a class - that implements fractions, or "rational numbers". You will define the Rational class in this lab assignment.

Class Specification

Write all of your code in the Rational.cpp file.

Write a C++ program that performs the rational number operations addition, subtraction, multiplication and division on two fractions. The program should be written in a single file. You will need to design a "rational number" class named Rational whose value will be a fraction (e.g., 1/128, or 22/7), with appropriate constructors and member functions. A fraction will be specified as a numerator and a denominator - e.g. the pair (8, 109) represents the fraction 8/109. The member variables should be private and accessed using the accessor and mutator functions.

Constructors

Create 3 constructors:

  • a constructor with two parameters (numerator and denominator)
  • a constructor with one parameter (denominator set to 1)
  • a constructor with no parameters (0/1)

Accessor Functions

  • add
  • subtract
  • multiply
  • divide
  • display

Mutator Function

  • simplify

The following are a list of the rules of arithmetic for fractions:

  • (a/b) + (c/d) = (ad + bc) / (b*d)
  • (a/b) - (c/d) = (ad - bc) / (b*d)
  • (a/b) * (c/d) = (ac) / (bd)
  • (a/b) / (c/d) = (ad) / (cb)

Note that for this lab, when you perform an operation, you do not need to simplify the resulting fraction, i.e., 4/5 * 5/10 = 20/50. You should not simplify this to 2/5 at this point.

The display function should output the Rational object in the format:

n / d

The simplify function should divide the numerator and denominator by the greatest common divisor. This function should call the private helper function gcd to get the greatest common divisor.

Feel free to use one of the gcd algorithms here: Euclidean algorithm

Required Class Interface

You must use the Rational class declaration exactly as it is provided. The Rational.h file cannot be changed.

Required main function

You must use the main function and global functions getRational and displayResult exactly as they are provided. The main.cpp file cannot be changed.

main.cpp

// Main

#include
#include "Rational.h"
using namespace std;


Rational getRational();
void displayResult(const string &, const Rational &, const Rational&, const Rational&);

int main() {
Rational A, B, result;
int choice;

cout << "Enter Rational A:" << endl;
A = getRational();
cout << endl;

cout << "Enter Rational B:" << endl;
B = getRational();
cout << endl;

cout << "Enter Operation (1 - 4):" << endl
<< "1 - Addition (A + B)" << endl
<< "2 - Subtraction (A - B)" << endl
<< "3 - Multiplication (A * B)" << endl
<< "4 - Division (A / B)" << endl
<< "5 - Simplify A" << endl;
cin >> choice;
cout << endl;

if (choice == 1) {
result = A.add(B);
displayResult("+", A, B, result);
} else if (choice == 2) {
result = A.subtract(B);
displayResult("-", A, B, result);
} else if (choice == 3) {
result = A.multiply(B);
displayResult("*", A, B, result);
} else if (choice == 4) {
result = A.divide(B);
displayResult("/", A, B, result);
} else if (choice == 5) {
A.simplify();
A.display();
} else {
cout << "Unknown Operation";
}
cout << endl;

return 0;
}

Rational getRational() {
int choice;
int numer, denom;

cout << "Which Rational constructor? (Enter 1, 2, or 3)" << endl
<< "1 - 2 parameters (numerator & denominator)" << endl
<< "2 - 1 parameter (numerator)" << endl
<< "3 - 0 parameters (default)" << endl;
cin >> choice;
cout << endl;

if (choice == 1) {
cout << "numerator? ";
cin >> numer;
cout << endl;
cout << "denominator? ";
cin >> denom;
cout << endl;
return Rational(numer, denom);
} else if (choice == 2) {
cout << "numerator? ";
cin >> numer;
cout << endl;
return Rational(numer);
} else {
return Rational();
}
}

void displayResult(const string &op, const Rational &lhs, const Rational&rhs, const Rational &result) {
cout << "(";
lhs.display();
cout << ") " << op << " (";
rhs.display();
cout << ") = (";
result.display();
cout << ")";
}

Rational.h

// Rational class declaration

#ifndef RATIONAL_H
#define RATIONAL_H

#include
using namespace std;

class Rational
{
private:
int numerator;
int denominator;
public:
Rational();
explicit Rational(int);
Rational(int, int);
const Rational add(const Rational &) const;
const Rational subtract(const Rational &) const;
const Rational multiply(const Rational &) const;
const Rational divide(const Rational &) const;
void simplify();
void display() const;
private:
int gcd(int, int) const;
};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code For the Rational class along with the implementation of each function.

#include <iostream>

using namespace std;

class Rational{
private:
int numerator;
int denominator;
  
public:
Rational(){
this->numerator=0;
this->denominator=1;
}
  
Rational(int x){
this->numerator=x;
this->denominator=1;
}
  
Rational(int x, int y){
this->numerator=x;
this->denominator=y;
}
  
const Rational add(Rational & );
const Rational subtract(Rational & );
const Rational multiply(Rational & );
const Rational divide(Rational & );
void simplify();
void display() const{
cout<<numerator<<"/"<<denominator<<endl;
};
private:
int gcd(int, int) ;
};

const Rational Rational::add(Rational &r){
int lcm = (this->denominator*r.denominator)/gcd(this->denominator,r.denominator);
  
int result = ((lcm/this->denominator)*this->numerator)+((lcm/r.denominator)*r.numerator);
Rational r1=Rational(result,lcm);
return r1;
}

const Rational Rational::subtract(Rational &r){
int lcm = (this->denominator*r.denominator)/gcd(this->denominator,r.denominator);
  
int result = ((lcm/this->denominator)*this->numerator)-((lcm/r.denominator)*r.numerator);
Rational r1=Rational(result,lcm);
return r1;   
}

const Rational Rational::multiply(Rational &r){
int num = this->numerator*r.numerator;
int den = this->denominator*r.denominator;
Rational r1=Rational(num,den);
return r1;
}

const Rational Rational::divide(Rational &r){
int num = this->numerator*r.denominator;
int den = this->denominator*r.numerator;
Rational r1=Rational(num,den);
return r1;
}

void Rational::simplify(){
int gcm=gcd(this->numerator,this->denominator);
int num=this->numerator/gcm;
int den=this->denominator/gcm;
  
}

int Rational::gcd(int x, int y){
int result = 1;
for(int i =2;i<=x && i<=y ; ++i){
if(x%i==0 && y%i==0){
result = i;
}
}
  
return(result);
}

Add a comment
Know the answer?
Add Answer to:
13.21 Lab: Rational class This question has been asked here before, but every answer I have...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

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

  • Having to repost this as the last answer wasn't functional. Please help In the following program...

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

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

  • If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...

    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++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...

    Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions: Create constructors default two argument three argument copy constructor Create a destructor. The destructor should set whole and numerator to zero and denominator to one. Add cout statements to the constructors and the destructor so you know when it's getting executed. Add system ("pause") to the destructor so you know when it executes. Only positive values allowed If denominator is 0 set...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.]...

    C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.] Write a rational number class. Recall that a rational number is a ratio-nal number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects...

  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

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

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT