Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main.cpp----------------------------------------------------------------------------------------------------------
- #include "Fraction.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- Fraction one(1,5);
- Fraction two(3,4);
- cout << "\nThe fraction is: ";
- one.print();
- cout << "\nThe fraction is: ";
- two.print();
- if (one.equal(two))
- {
- cout << "\nThe fractions are equal";
- }
- else
- {
- cout << "\nThe fractions are NOT equal";
- }
- cout << "\n\nThe sum of the fractions is: ";
- Fraction three = one.add(two);
- three.print();
- cout << "\nThe sum of the fractions is: ";
- Fraction four = one+two;
- four.print();
- cout << "\nThe difference of the fractions is: ";
- Fraction five = one-two;
- five.print();
- cout << "\nThe quotient of the fractions is: ";
- Fraction six = one/two;
- six.print();
- cout << "\nThe product of the fractions is: ";
- Fraction seven = one*two;
- seven.print();
- cin >> three;
- cout << "The fractin is now: " << three;
- return 0;
- }
- //Fraction.h----------------------------------------------------------------------------------------------------------
- #ifndef Fraction_h
- #define Fraction_h
- #include<iostream>
- using namespace std;
- class Fraction
- {
- public:
- Fraction();
- Fraction(int, int);
- void setFraction(int, int);
- void getFraction();
- void print();
- bool equal(const Fraction &);
- Fraction add(const Fraction &);
- Fraction operator+(const Fraction &);
- Fraction operator*(const Fraction &);
- friend ostream& operator<<(ostream&, const Fraction&);
- friend istream& operator>>(istream&, Fraction&);
- Fraction operator-(const Fraction &);
- Fraction operator/(const Fraction &);
- private:
- int num;
- int den;
- };
- #endif
- //Fraction.cpp----------------------------------------------------------------------------------------------------------
- #include "Fraction.h"
- #include <iostream>
- using namespace std;
- Fraction::Fraction(int n, int d)
- {
- num = n;
- den = d;
- }
- Fraction::Fraction()
- {
- num = 1;
- den = 1;
- }
- void Fraction::setFraction(int n, int d)
- {
- num = n;
- den = d;
- }
- void Fraction::getFraction()
- {
- int n;
- int d;
- cout << "\nEnter a number for the numerator: ";
- cin >> n;
- cout << endl;
- cout << "\nEnter a number for the denominator: ";
- cin >> d;
- cout << endl;
- num = n;
- den = d;
- }
- void Fraction::print()
- {
- cout << endl << num << "/" << den << endl;
- }
- bool Fraction::equal(const Fraction& a)
- {
- return ((num == a.num) and (den == a.den));
- }
- Fraction Fraction::add(const Fraction& a)
- {
- int denMultOne = den;
- int denMultTwo = a.den;
- int newDen, newNum, addNumOne, addNumTwo;
- addNumOne = num*denMultTwo;
- addNumTwo = a.num*denMultOne;
- newDen = den*denMultTwo;
- newNum = addNumOne+addNumTwo;
- return Fraction(newNum,newDen);
- }
- Fraction Fraction::operator+(const Fraction& a)
- {
- int denMultOne = den;
- int denMultTwo = a.den;
- int newDen, newNum, addNumOne, addNumTwo;
- addNumOne = num*denMultTwo;
- addNumTwo = a.num*denMultOne;
- newDen = den*denMultTwo;
- newNum = addNumOne+addNumTwo;
- return Fraction(newNum,newDen);
- }
- Fraction Fraction::operator*(const Fraction& a)
- {
- int newDen, newNum;
- newDen = den*a.den;
- newNum = num*a.num;
- return Fraction(newNum,newDen);
- }
- istream& operator>>(istream& is, Fraction& a)
- {
- cout << "\n\nEnter a number for the numerator then the denominator" << endl;
- is >> a.num >> a.den;
- return is;
- }
- ostream& operator<<(ostream& os, const Fraction& a)
- {
- os << a.num <<"/" << a.den;
- return os;
- }
- Fraction Fraction::operator/(const Fraction& a)
- {
- int newDen, newNum;
- newDen = den*a.num;
- newNum = num*a.den;
- return Fraction(newNum,newDen);
- }
- Fraction Fraction::operator-(const Fraction& a)
- {
- int denMultOne = den;
- int denMultTwo = a.den;
- int newDen, newNum, addNumOne, addNumTwo;
- addNumOne = num*denMultTwo;
- addNumTwo = a.num*denMultOne;
- newDen = den*denMultTwo;
- newNum = addNumOne-addNumTwo;
- return Fraction(newNum,newDen);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement