Main.cpp

#include <iostream> #include "Rational.h" using namespace std; int main() { Rational half(1,2), two_thirds(2,3), four_fifths(4,5); Rational third(1,3), one(1,1), two(2,1); Rational answer; answer = third * half; answer.printRational(); answer = half + four_fifths / two * (third + one); //does bodmas answer.printRational(); (half + third).printRational(); third.add(two).printRational(); return 0; }

The header:

Rational.h

#ifndef RATIONAL_H #define RATIONAL_H class Rational{ public: Rational(); Rational(int, int); //Operator overloading. This four are the main methods Rational operator+(Rational); Rational operator-(Rational); Rational operator*(Rational); Rational operator/(Rational); Rational operator()(Rational); //This others just call the four above Rational add(Rational); Rational Subtract(Rational); Rational multiply(Rational); Rational divide(Rational); Rational add(int, int); Rational Subtract(int, int); Rational multiply(int, int); Rational divide(int, int); //This are used within the methods void simplify(); void printRational(); void improper(); int GCD(int, int); private: //a is the numerator and d the denominator. whole keeps the whole number part int a, b, whole; }; #endif // RATIONAL_H

and the meat of the code:

Rational.cpp

#include "Rational.h" #include <iostream> using namespace std; Rational::Rational() { a = 0; b = 1; whole =0; } int Rational::GCD(int a, int b){ if(a%b == 0) return b; else return GCD(b, a%b); } Rational::Rational(int x, int y){ a = x; if(y != 0){ b = y; }else{ //Validation cout<<"Denominator cannot be zero"; b =1; } whole=0; simplify(); } void Rational::simplify(){ //get the whole part of the fraction whole = a/b; //first get the greatest common divisor of the two int gcd = GCD(a , b); a = a%b; a = a / gcd; //numerator b = b / gcd; //denominator } void Rational::improper(){ if(whole != 0) a = whole * b + a; } void Rational::printRational(){ simplify(); // if no whole number no need to print 0, if b is zero just print the whole part if(whole ==0 && a != 0) cout<<a<<"/"<<b<<endl; else if(a== 0) cout<<whole<<endl; else cout<<whole<<" "<<a<<"/"<<b<<endl; } //overloading the addition operator. An addition sign between two objects //of Rational class will cause this method to be executed Rational Rational::operator+(Rational frac){ Rational obj; //Initialise an object from Rational class; improper(); //Change to improper fractions to get rid of the whole number part frac.improper(); obj.b = b * frac.b; // a/b + c/d = (ad + cb)/bd obj.a = a * frac.b + frac.a * b; return obj; //return the object with its a and b values altered } Rational Rational::operator-(Rational frac){ Rational obj; //Similar as above improper(); frac.improper(); obj.b = b * frac.b; // a/b - c/d = (ad - bc)/bd obj.a = a * frac.b - frac.a * b; return obj; } Rational Rational::operator*(Rational frac){ Rational obj; improper(); frac.improper(); obj.b = b * frac.b; // a/b * c/d = ac/bd obj.a = a * frac.a; return obj; } Rational Rational::operator/(Rational frac){ Rational obj; improper(); frac.improper(); obj.a = a * frac.b; //a/b / c/d = ad / bc obj.b = b * frac.a; return obj; } //Overloading the brackets will allow: // third * (half + third) Rational Rational::operator()(Rational frac){ //the object pointed to by 'this'(thats this object) multiplied by another //object Rational obj = *this * frac; //the * has been overloaded return obj; } Rational Rational::multiply(Rational frac){ Rational obj = *this /frac; //Using operator overloading use this object divided by frac and return the new object return obj; } Rational Rational::add(Rational frac){ Rational obj = *this + frac; return obj; } Rational Rational::Subtract(Rational frac){ Rational obj = *this - frac; return obj; } Rational Rational::divide(Rational frac){ Rational obj = *this / frac; return obj; } Rational Rational::add(int x, int y){ Rational obj,frac(x,y); obj = *this + frac; return obj; } Rational Rational::Subtract(int x, int y){ Rational obj,frac(x,y); obj = *this - frac; return obj; } Rational Rational::multiply(int x, int y){ Rational obj,frac(x,y); obj = *this * frac; return obj; } Rational Rational::divide(int x, int y){ Rational obj,frac(x,y); obj = *this / frac; return obj; }

Advertisements