Advertisement
MasterGun

Fraction Operations

Feb 14th, 2021 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. class Fraction {
  2.     int uppernum;
  3.     int lowernum;
  4. public:
  5.     Fraction(int m_uppernum, int m_lowernum) {
  6.         this->uppernum = uppernum;
  7.         this->lowernum = lowernum;
  8.     }
  9.     void showdata() {
  10.         cout << uppernum << "/" << lowernum << endl;
  11.     }
  12.     friend Fraction operator*(Fraction& f1, Fraction& f2);
  13.     friend Fraction operator/(Fraction& f1, Fraction& f2);
  14.     friend Fraction operator*(Fraction& f1, int a);
  15.     friend Fraction operator*(int a,Fraction& f1);
  16.     friend Fraction operator/(Fraction& f1, int a);
  17.     friend Fraction operator/(int a, Fraction& f1);
  18. };
  19. Fraction operator*(Fraction& f1, Fraction& f2)
  20. {
  21.     return Fraction(f1.uppernum * f2.uppernum, f1.lowernum * f2.lowernum);
  22. }
  23. Fraction operator/(Fraction& f1, Fraction& f2) {
  24.     return Fraction(f1.uppernum * f2.lowernum, f1.lowernum * f2.uppernum);
  25. }
  26. Fraction operator*(Fraction& f1, int a)
  27. {
  28.     return Fraction(f1.uppernum * a, f1.lowernum * 1);
  29. }
  30. Fraction operator*(int a, Fraction& f1) {
  31.     return Fraction(f1.uppernum * a, f1.lowernum * 1);
  32. }
  33. Fraction operator/(Fraction& f1, int a) {
  34.     return Fraction(f1.uppernum * 1, f1.lowernum * a);
  35. }
  36. Fraction operator/(int a, Fraction& f1) {
  37.     return Fraction(f1.lowernum * 1, f1.uppernum * a);
  38. }
  39. int main()
  40. {
  41.     setlocale(LC_ALL, "rus");
  42.  
  43.     Fraction f1(1, 2);
  44.     Fraction f2(1, 4);
  45.     Fraction f3 = f1 * 4;
  46.     Fraction f4 = 4 * f1;
  47.     Fraction f5 = f1 / f2;
  48.     Fraction f6 = f1 / 2;
  49.     Fraction f7 = 2 / f1;
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement