Advertisement
Finigini

Lab05

Sep 23rd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. //Main.cpp----------------------------------------------------------------------------------------------------------
  2.  
  3. #include "Fraction.h"
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     Fraction one(1,5);
  11.     Fraction two(3,4);
  12.     cout << "\nThe fraction is: ";
  13.     one.print();
  14.     cout << "\nThe fraction is: ";
  15.     two.print();
  16.    
  17.     if (one.equal(two))
  18.     {
  19.     cout << "\nThe fractions are equal";   
  20.     }
  21.     else
  22.     {
  23.     cout << "\nThe fractions are NOT equal";
  24.     }
  25.    
  26.     cout << "\n\nThe sum of the fractions is: ";
  27.     Fraction three = one.add(two);
  28.     three.print();
  29.    
  30.     cout << "\nThe sum of the fractions is: ";
  31.     Fraction four = one+two;
  32.     four.print();
  33.    
  34.     cout << "\nThe difference of the fractions is: ";
  35.     Fraction five = one-two;
  36.     five.print();
  37.    
  38.     cout << "\nThe quotient of the fractions is: ";
  39.     Fraction six = one/two;
  40.     six.print();
  41.    
  42.     cout << "\nThe product of the fractions is: ";
  43.     Fraction seven = one*two;
  44.     seven.print();
  45.  
  46.     cin >> three;
  47.     cout << "The fractin is now: " << three;
  48.     return 0;
  49. }
  50.  
  51. //Fraction.h----------------------------------------------------------------------------------------------------------
  52.  
  53. #ifndef Fraction_h
  54. #define Fraction_h
  55. #include<iostream>
  56.  
  57. using namespace std;
  58.  
  59. class Fraction
  60. {
  61. public:
  62.     Fraction();
  63.     Fraction(int, int);
  64.     void setFraction(int, int);
  65.     void getFraction();
  66.     void print();
  67.     bool equal(const Fraction &);
  68.     Fraction add(const Fraction &);
  69.     Fraction operator+(const Fraction &);
  70.     Fraction operator*(const Fraction &);
  71.     friend ostream& operator<<(ostream&, const Fraction&);
  72.     friend istream& operator>>(istream&, Fraction&);
  73.     Fraction operator-(const Fraction &);
  74.     Fraction operator/(const Fraction &);
  75. private:
  76.     int num;
  77.     int den;
  78. };
  79.  
  80. #endif
  81.  
  82. //Fraction.cpp----------------------------------------------------------------------------------------------------------
  83.  
  84. #include "Fraction.h"
  85. #include <iostream>
  86.  
  87. using namespace std;
  88.  
  89. Fraction::Fraction(int n, int d)
  90. {
  91.     num = n;
  92.     den = d;
  93. }
  94.  
  95. Fraction::Fraction()
  96. {
  97.     num = 1;
  98.     den = 1;
  99. }
  100.  
  101.  
  102. void Fraction::setFraction(int n, int d)
  103. {
  104.     num = n;
  105.     den = d;
  106. }
  107.  
  108. void Fraction::getFraction()
  109. {
  110.     int n;
  111.     int d;
  112.     cout << "\nEnter a number for the numerator: ";
  113.     cin >> n;
  114.     cout << endl;
  115.     cout << "\nEnter a number for the denominator: ";
  116.     cin >> d;
  117.     cout << endl;
  118.     num = n;
  119.     den = d;
  120. }
  121.  
  122. void Fraction::print()
  123. {
  124.     cout << endl << num << "/" << den << endl;
  125. }
  126.  
  127. bool Fraction::equal(const Fraction& a)
  128. {
  129.     return ((num == a.num) and (den == a.den));
  130. }
  131.  
  132. Fraction Fraction::add(const Fraction& a)
  133. {
  134.     int denMultOne = den;
  135.     int denMultTwo = a.den;
  136.     int newDen, newNum, addNumOne, addNumTwo;
  137.     addNumOne = num*denMultTwo;
  138.     addNumTwo = a.num*denMultOne;
  139.     newDen = den*denMultTwo;
  140.     newNum = addNumOne+addNumTwo;
  141.    
  142.     return Fraction(newNum,newDen);
  143. }
  144.  
  145. Fraction Fraction::operator+(const Fraction& a)
  146. {
  147.     int denMultOne = den;
  148.     int denMultTwo = a.den;
  149.     int newDen, newNum, addNumOne, addNumTwo;
  150.     addNumOne = num*denMultTwo;
  151.     addNumTwo = a.num*denMultOne;
  152.     newDen = den*denMultTwo;
  153.     newNum = addNumOne+addNumTwo;
  154.    
  155.     return Fraction(newNum,newDen);
  156. }
  157.  
  158. Fraction Fraction::operator*(const Fraction& a)
  159. {
  160.     int newDen, newNum;
  161.     newDen = den*a.den;
  162.     newNum = num*a.num;
  163.    
  164.     return Fraction(newNum,newDen);
  165. }
  166.  
  167. istream& operator>>(istream& is, Fraction& a)
  168. {
  169.     cout << "\n\nEnter a number for the numerator then the denominator" << endl;
  170.     is >> a.num >> a.den;
  171.     return is;
  172. }
  173.  
  174. ostream& operator<<(ostream& os, const Fraction& a)  
  175. {  
  176.     os << a.num <<"/" << a.den;  
  177.     return os;
  178. }
  179.  
  180. Fraction Fraction::operator/(const Fraction& a)
  181. {
  182.     int newDen, newNum;
  183.     newDen = den*a.num;
  184.     newNum = num*a.den;
  185.    
  186.     return Fraction(newNum,newDen);
  187. }
  188.  
  189. Fraction Fraction::operator-(const Fraction& a)
  190. {
  191.     int denMultOne = den;
  192.     int denMultTwo = a.den;
  193.     int newDen, newNum, addNumOne, addNumTwo;
  194.     addNumOne = num*denMultTwo;
  195.     addNumTwo = a.num*denMultOne;
  196.     newDen = den*denMultTwo;
  197.     newNum = addNumOne-addNumTwo;
  198.    
  199.     return Fraction(newNum,newDen);
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement