Advertisement
edensheiko

c++Rational class (just for fun) targil 5 beta

Feb 27th, 2021
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. // still not done.
  2. //used for operators like * - / +
  3. //Rational.h
  4. #pragma once
  5. class Rational
  6. {
  7. public:
  8.     Rational(int numerator=0,int denominator =1);
  9.     ~Rational();
  10.     int Getnumerator() const;
  11.     void Setnumerator(int);
  12.     int Getdenominator() const;
  13.     void Setdenominator(int);
  14.     void Setden_num(int, int);
  15.     void print()const;
  16.     Rational operator+ (const Rational&)const;
  17.     Rational operator- (const Rational&)const;
  18.     Rational operator* (const Rational&)const;
  19.     Rational operator/ (const Rational&)const;
  20.  
  21. private:
  22.     int numerator;
  23.     int denominator;
  24. };
  25. //Rational.cpp
  26. #include "Rational.h"
  27. #include <iostream>
  28. #include <math.h>
  29.  
  30. using namespace std;
  31.  
  32.  
  33. Rational::Rational(int a,int b)
  34. {
  35.     this->numerator = a;
  36.     this->denominator = b;
  37. }
  38.  
  39. Rational::~Rational()
  40. {
  41. }
  42.  
  43. int Rational::Getnumerator() const
  44. {
  45.     return this->numerator;
  46. }
  47.  
  48. void Rational::Setnumerator(int x)
  49. {
  50.     numerator = x;
  51. }
  52.  
  53. int Rational::Getdenominator() const
  54. {
  55.     return this->denominator;
  56. }
  57.  
  58. void Rational::Setdenominator(int y)
  59. {
  60.     denominator = y;
  61. }
  62.  
  63. void Rational::Setden_num(int num, int deno)
  64. {
  65.     this->numerator = num;
  66.     this->denominator = deno;
  67. }
  68.  
  69. void Rational::print() const
  70. {
  71.     cout << "(" << numerator << "," << denominator << ")"<< endl;
  72. }
  73.  
  74. int gcd(int a, int b)
  75. {
  76.     // Everything divides 0
  77.     if (a == 0)
  78.         return b;
  79.     if (b == 0)
  80.         return a;
  81.  
  82.     // base case
  83.     if (a == b)
  84.         return a;
  85.  
  86.     // a is greater
  87.     if (a > b)
  88.         return gcd(a - b, b);
  89.     return gcd(a, b - a);
  90. }
  91.  
  92. long long lcm(int a, int b)
  93. {
  94.     return (a / gcd(a, b) * b);
  95. }
  96.  
  97.  
  98.  
  99. Rational Rational::operator+ (const Rational& n)const
  100. {
  101.     Rational result; // first bug fixed -> 2 same denominator
  102.     if (this->denominator==n.denominator)
  103.     {
  104.         result.numerator = this->numerator + n.numerator;
  105.         result.denominator = denominator;
  106.         return result;
  107.     }
  108.     else
  109.     {
  110.         //**************************************************************************
  111.         //*
  112.         //*
  113.         //*
  114.         //*              algorithm DONT WORK!  
  115.         //*
  116.         //*
  117.         //***************************************************************************
  118.         //int lcm_temp = lcm(this->denominator, n.denominator); // 1/4 & 1/5 == holds 20
  119.         //int temp_numerator_first = (this->numerator * n.denominator);  //1/4 ->1*5 holds 5
  120.         //int temp_denominator_first = (this->denominator * n.denominator);// 1/4 -> 4*5 holds 20
  121.         //int temp_numerator_secand = (n.numerator * this->denominator); // 1/5 -> 1*4 holds 4
  122.         //int temp_denominator_secand = (n.denominator * this->denominator); // 1/5 -> 5*4 holds 20
  123.         //result.numerator = temp_numerator_first + temp_numerator_secand; // just 5+4 -> 9
  124.         //result.denominator = lcm_temp; // lcm
  125.         //return result; // we return the result
  126.         int temp = gcd(denominator, n.denominator); // Greatest common divisor of two numbers
  127.         int f1 = n.denominator / temp; //
  128.         int f2 = denominator / temp; //
  129.        
  130.         result.numerator = numerator * f1 + n.numerator * f2;
  131.         result.denominator = f1 * denominator;
  132.         return result;
  133.     }
  134.    
  135. }
  136.  
  137. Rational Rational::operator- (const Rational& n)const
  138. {
  139.     Rational temp(-n.numerator,n.denominator);
  140.     return (*this) + temp;
  141. }
  142.  
  143. Rational Rational::operator* (const Rational& n)const
  144. {
  145.     // 1/4 * 1/5; = 1*1=1 + 5*4=20 == 1/20
  146.     Rational temp;
  147.     int numerator_holder = numerator * n.numerator;
  148.     int denominator_holder = denominator * n.denominator;
  149.     temp.numerator = numerator_holder;
  150.     temp.denominator = denominator_holder;
  151.     return temp;
  152. }
  153.  
  154. Rational Rational::operator/ (const Rational& n)const
  155. {
  156.     Rational temp;
  157. }
  158. //Main.cpp
  159. #include <iostream>
  160. #include "Rational.h"
  161. using namespace std;
  162.  
  163. void main()
  164. {
  165.     Rational v1(4,8);
  166.     Rational v2; // default
  167.     Rational v3; // default with set func
  168.     v1.print();
  169.     v2.print();
  170.     v3.Setden_num(55, 22);
  171.     v3.print();
  172.     v2.Getdenominator();
  173.     //etc....
  174.     //test of 2 oprators "+"
  175.     cout << "----------------------" << endl;
  176.     Rational v4(1,4);
  177.     Rational v5(1,5);
  178.     Rational v6;
  179.     v6 = v4 + v5;
  180.     v6.print();
  181.     cout << "----------------------" << endl;
  182.     Rational v7(4, 99);
  183.     Rational v8(3, 99);
  184.     Rational v9;
  185.     v9 = v7 + v8;
  186.     v9.print(); // bug fixed
  187.     cout << "test to print 2 sided" << endl;
  188.     Rational test;
  189.     test = v8 + v7;
  190.     test.print();//bug fixed
  191.     cout << "----------------------" << endl;
  192.     Rational v10(1 , 20);
  193.     Rational v11(2 , 40);
  194.     Rational v12;
  195.     v12 = v11 + v10;
  196.     v12.print(); // bug fixed
  197.     cout << "----------------------" << endl;
  198.     Rational v13(55, 72);
  199.     Rational v14(66, 120);
  200.     Rational v15;
  201.     v15 = v14 + v13;
  202.     v15.print();// call for normlize need to be done + bug |update| delt with new algorithm
  203.     cout << "----------------------" << endl;
  204.     //test of 2 oprators "-"
  205.     Rational v16(33, 75);
  206.     Rational v17(63, 41);
  207.     Rational v18;
  208.     v18 = v17 - v16;
  209.     v18.print();
  210.     cout << "----------------------" << endl;
  211.     //test of 2 oprators "*"
  212.     Rational v20(1, 5);
  213.     Rational v21(1, 4);
  214.     Rational v22;
  215.     v22 = v20 * v21;
  216.     v22.print();
  217.     // more test by imri rand
  218.     Rational v23(1, 7);
  219.     Rational v24(3, 8);
  220.     Rational v25(1,1);
  221.     v25 = v24 * v23; // bug fixed
  222.     v25.print();
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement