I'm working on this problem: Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use Integer values to represent the private data of the class-the numerator and the denominator. Provide a constructor should contain default values in case no initializes are provided and should store the fraction in reduced form.
provide public member functions that perform each of the following tasks:
adding, subtracting, multiplying dividing, two rational numbers
// header file
#ifndef RATIONAL_H_
#define RATIONAL_H_
//Rational class definition
class Rational {
public:
Rational();
Rational ( int n = 0, int d = 1); //default case
void adding( const Rational &); // a) adding two rational numbers
void subtracting(const Rational &); // b) subtracting two rational numbers
void multiplying(const Rational &); // c) multiplying two rational numbers
void dividing(const Rational &); // d) dividing two rational numbers
void printRational(); // e) printing in form of a/b, where a is numerator and b is denominator
void printFloat(); // f) printing in floating point format
private:
int numerator;
int denominator;
void reduce();
};
#endif /* RATIONAL_H_ */
//source file
#include <iostream>
#include <iomanip>
#include "Rational.h"
usingnamespace std;
Rational::Rational(int n, int d) {
numerator = d<0 ? -n:n; // to make sure that if a negative number is entered, it will be in the numerator
denominator = d<0 ? -d:n;
reduce();
}
Rational Rational::adding (const Rational& a) {
Rational r;
r.numerator = a.numerator * denominator + a.denominator * numerator;
r.numerator = a.denominator * denominator;
r.reduce();
return r;
}
Rational Rational::subtracting (const Rational& s) {
Rational r;
r.numerator = s.numerator * denominator - s.denominator * numerator;
r.numerator = s.denominator * denominator;
r.reduce();
return r;
}
Rational Rational::multiplying (const Rational& m){
Rational r;
r.numerator = m.numerator * numerator;
r.numerator = m.denominator * denominator;
if (denominator != 0)
r.reduce();
return r;
}
Rational Rational::dividing (const Rational& b){
Rational r;
r.numerator = b.denominator * numerator;
r.numerator = b.numerator * denominator;
if (denominator != 0)
r.reduce();
return r;
}
void Rational::printRational(){
if (denominator = 0)
cout<< "Error" <<endl;
else{
cout << numerator << "/" << denominator << endl;
}
}
void Rational::printFloat(){
if (denominator = 0)
cout << "Error" <<endl;
else
cout << numerator / denominator << "." << numerator%denominator <<endl; //float(numerator) float(denominator) ????????
}
void Rational::reduce(){
int n = numerator < 0 ? -numerator : numerator;
int d = denominator;
int largest = n > d ? n : d;
int gcd = 0;
for (int i = largest; i>= 2; i--)
if(numerator % i == 0 && denominator % i == 0){
gcd = i;
break;
}
if (gcd != 0) {
numerator /= gcd;
denominator /= gcd;
}
}
// program to test class
#include <iostream>
#include "Rational.h"
usingnamespace std;
int main(){
Rational r;
cout<< "Please enter a numerator: " << endl;
cin >> r.numerator;
cout<< "Please enter a denominator: " << endl;
cin >> r.denominator;
cout << "The rational number you entered is: " << r.printRational() << endl;
cout << "The floating point format of the rational number you entered is: " << r.printFloat() << endl;
//adding
cout<< "Please enter another rational number to add to your original number. Enter the numerator first, then the denominator: "<<endl;
cin >> r.numerator >>r.denominator;
// How would i call the functions under adding that i declared in Rational.cpp??
}
I am lost on how I should integrate everything in the testing class. How do I call the functions under the adding, subtracting etc. that I declared in Rational.cpp? Any help would be appreciated since I'm still very new to C++ and I'm having a hard time grasping this topic
/*
* Rational.h
*
* Created on: Nov 16, 2016
*
* header file for Rational class
*/
#ifndef RATIONAL_H_
#define RATIONAL_H_
//Rational class definition
class Rational {
public:
Rational();
Rational ( int n = 0, int d = 1); //default case
int adding( const Rational& a); // a) adding two rational numbers
int subtracting(const Rational& s); // b) subtracting two rational numbers
int multiplying(const Rational& m); // c) multiplying two rational numbers
int dividing(const Rational& b); // d) dividing two rational numbers
void printRational(); // e) printing in form of a/b, where a is numerator and b is denominator
void printFloat(); // f) printing in floating point format
private:
int numerator;
int denominator;
void reduce();
};
#endif /* RATIONAL_H_ */
/*
* Rational.cpp
*
* Created on: Nov 16, 2016
*
*/
#include <iostream>
#include <iomanip>
#include "Rational.h"
usingnamespace std;
Rational::Rational(int n, int d) {
numerator = d<0 ? -n:n; // to make sure that if a negative number is entered, it will be in the numerator
denominator = d<0 ? -d:n;
reduce();
}
Rational Rational::adding ( const Rational& a) {
Rational r;
r.numerator = a.numerator * denominator + a.denominator * numerator;
r.numerator = a.denominator * denominator;
r.reduce();
return r;
}
Rational Rational::subtracting (const Rational& s) {
Rational r;
r.numerator = s.numerator * denominator - s.denominator * numerator;
r.numerator = s.denominator * denominator;
r.reduce();
return r;
}
Rational Rational::multiplying (const Rational& m){
Rational r;
r.numerator = m.numerator * numerator;
r.numerator = m.denominator * denominator;
if (denominator != 0)
r.reduce();
return r;
}
Rational Rational::dividing (const Rational& b){
Rational r;
r.numerator = b.denominator * numerator;
r.numerator = b.numerator * denominator;
if (denominator != 0)
r.reduce();
return r;
}
void Rational::printRational(){
if (denominator = 0)
cout<< "Error" <<endl;
else{
cout << numerator << "/" << denominator << endl;
}
}
void Rational::printFloat(){
if (denominator = 0)
cout << "Error" <<endl;
else
cout << numerator / denominator << "." << numerator%denominator <<endl; //float(numerator) float(denominator) ????????
}
void Rational::reduce(){
int n = numerator < 0 ? -numerator : numerator;
int d = denominator;
int largest = n > d ? n : d;
int gcd = 0;
for (int i = largest; i>= 2; i--)
if(numerator % i == 0 && denominator % i == 0){
gcd = i;
break;
}
if (gcd != 0) {
numerator /= gcd;
denominator /= gcd;
}
}
/*
* assign8.cpp
*
* Created on: Nov 16, 2016
*
*
* program to test Rational class
*
* ex. 9.15
*/
#include <iostream>
#include "Rational.h"
#include "Rational.cpp"
usingnamespace std;
int main(){
Rational x(-3/6), y(-25/40), z, w(9);
//adding
x.printRational();
cout<< " + ";
y.printRational();
z = x.adding(y);
cout<< " = ";
z.printRational();
cout << endl;
z.printRational();
cout<< " = ";
z.printFloat();
cout << endl;
// subtracting
x.printRational();
cout<< " - ";
y.printRational();
z = x.subtracting(y);
cout<< " = ";
z.printRational();
cout << endl;
z.printRational();
cout<< " = ";
z.printFloat();
cout << endl;
// multiplying
x.printRational();
cout<< " * ";
y.printRational();
z = x.multiplying(y);
cout<< " = ";
z.printRational();
cout << endl;
z.printRational();
cout<< " = ";
z.printFloat();
cout << endl;
//dividing
x.printRational();
cout<< " / ";
y.printRational();
z = x.adding(y);
cout<< " = ";
z.printRational();
cout << endl;
z.printRational();
cout<< " = ";
z.printFloat();
cout << endl;
}
Does anyone see any errors in this? I am using eclipse and it won't compile. it is showing me some errors on lines 19-22 ( candidate is: ( whatever the line has written for ex int Rational::adding (const Rational& )) and on lines 52, 62, 72, 83 it's saying it doesn't match but i thought it did in this new code???