Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 148,766 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,760 people online right now. Registration is fast and FREE... Join Now!





Class Implementing Fractions

This Class Implements Fractions using various Functions.

Submitted By: born2c0de
Actions:
Rating:
Views: 9,154

Language: C++

Last Modified: December 28, 2007

Snippet


  1. /*                                           THE FRACTION CLASS
  2.                                                                            -Sanchit Karve
  3.                                               born2c0de@hotmail.com
  4.      Use this class to carry out a few mathematical operations on Fractions.
  5.    -> Add,Subtract,Multiply,Divide and Compare Fractions.
  6.    -> Contains Various Methods of assigning values to objects.
  7.    -> Example Program ( in main() ) to show how each function in the class can
  8.         be used for various operations.
  9. */
  10.  
  11.  
  12. #include <iostream>
  13.  
  14. using namespace std;
  15.  
  16. class fraction
  17. {
  18.    private:
  19.                   int num;         // Numerator
  20.                   int den;         // Denominator
  21.  
  22.             void simplify()// Simplify Function
  23.             static int lcm(int,int); // lcm...but not really as u shall see...
  24.  
  25.    public:
  26.               fraction();   // Constructor
  27.             fraction(int,int); // 2 Argument Constructor
  28.             fraction(float);   // 1 Argument Constructor
  29.             fraction(fraction&); // Copy Constructor
  30.             ~fraction();       // Destructor
  31.             float fractofloat(); // Fraction to Float Function
  32.             void operator =(float)// Overloaded Assignment Operator 1
  33.             void operator =(fraction);// Overloaded Assignment Operator 2
  34.             fraction operator +(fraction); // Addition of Fractions
  35.             fraction operator -(fraction); // Subtraction of Fractions
  36.             fraction operator *(fraction); // Product of Fractions
  37.             fraction operator /(fraction); // Division of Fractions
  38.             bool operator ==(fraction);   // Equality Overloaded Function
  39.             bool operator !=(fraction)// InEquality Overloaded Function
  40.             bool operator <(fraction);   // Comparison
  41.             bool operator <=(fraction)// Overloaded
  42.             bool operator >(fraction);   // Functions
  43.             bool operator >=(fraction)//   ''
  44.             friend ostream& operator <<(ostream &s,fraction f); // for cout
  45. };
  46.  
  47.  
  48.    fraction::fraction()
  49.    {
  50.      num=0;
  51.      den=1;
  52.    }
  53.  
  54.    fraction::fraction(int n,int d)
  55.    {
  56.      num=n;
  57.      den=(d==0)? 1 :d;
  58.      simplify();
  59.    }
  60.  
  61.    fraction::fraction(float f)
  62.    {
  63.         num=f*100000;
  64.       den=100000;
  65.       simplify();
  66.    }
  67.  
  68.    fraction::fraction(fraction& f)
  69.    {
  70.      num=f.num;
  71.      den=f.den;
  72.      fraction::simplify();
  73.    }
  74.  
  75.    fraction::~fraction()
  76.    {
  77.    }
  78.  
  79.  
  80.    void fraction::simplify()
  81.    {
  82.          float n=(float)this->num;
  83.       float d=(float)this->den;
  84.       int i=2;
  85.       while((i<=n) && (n!=1))
  86.       {
  87.            while((n/i==(int)n/i) && (d/i ==(int)d/i))
  88.          {
  89.               n/=i;
  90.             d/=i;
  91.          }
  92.          i++;
  93.       }
  94.       num=n;
  95.       den=(n==0)?1:d;
  96.    }
  97.  
  98.    int fraction::lcm(int a,int b)
  99.    {
  100.         return a*b;
  101.       // Just the Multiplied product is returned since the
  102.       // Fraction will be simplified further anyway
  103.    }
  104.  
  105.    float fraction::fractofloat()
  106.    {
  107.         return (float)num /(float)den;
  108.    }
  109.    void fraction::operator =(fraction f)
  110.    {
  111.      num=f.num;
  112.      den=f.den;
  113.      fraction::simplify();
  114.    }
  115.  
  116.    void fraction::operator =(float f)
  117.    {
  118.         num=f*100000;
  119.       den=100000;
  120.       fraction::simplify();
  121.    }
  122.  
  123.    fraction fraction::operator +(fraction f)
  124.    {
  125.      fraction::simplify();
  126.      fraction tmp;
  127.      f.simplify();
  128.      if(den==f.den)
  129.      {
  130.                tmp.num=num+f.num;
  131.          tmp.den=den;
  132.      }
  133.      else
  134.      {
  135.                int lcm=fraction::lcm(den,f.den);
  136.          tmp.num=(num*(lcm/den)) + (f.num*(lcm/f.den));
  137.          tmp.den=lcm;
  138.      }
  139.      tmp.simplify();
  140.      return tmp;
  141.   }
  142.  
  143.  
  144.  
  145.    fraction fraction::operator -(fraction f)
  146.    {
  147.      fraction::simplify();
  148.      fraction tmp;
  149.      f.simplify();
  150.      if(den==f.den)
  151.      {
  152.                tmp.num=num - f.num;
  153.          tmp.den=den;
  154.      }
  155.      else
  156.      {
  157.                int lcm=fraction::lcm(den,f.den);
  158.          tmp.num=(num*(lcm/den)) - (f.num*(lcm/f.den));
  159.          tmp.den=lcm;
  160.      }
  161.      tmp.simplify();
  162.      return tmp;
  163.  
  164.    }
  165.  
  166.    fraction fraction::operator *(fraction f)
  167.    {
  168.      fraction::simplify();
  169.      fraction tmp;
  170.      tmp.num=this->num * f.num;
  171.      tmp.den=this->den * f.den;
  172.      tmp.simplify();
  173.      return tmp;
  174.    }
  175.  
  176.  
  177.    fraction fraction::operator /(fraction f)
  178.    {
  179.      fraction::simplify();
  180.      fraction tmp;
  181.      tmp.num=this->num / f.num;
  182.      tmp.den=this->den / f.den;
  183.      tmp.simplify();
  184.      return tmp;
  185.    }
  186.  
  187.    bool fraction::operator ==(fraction f)
  188.    {
  189.         fraction::simplify();
  190.       f.simplify();
  191.       return ((num==f.num) && (den==f.den)) ? 1 : 0;
  192.    }
  193.  
  194.    bool fraction::operator !=(fraction f)
  195.    {
  196.         fraction::simplify();
  197.       f.simplify();
  198.       return ((num==f.num) && (den==f.den)) ? 0 : 1;
  199.    }
  200.  
  201.    bool fraction::operator <(fraction f)
  202.    {
  203.         int d;
  204.       d=this->den;
  205.       den*=f.den;
  206.       num*=f.den;
  207.       f.den*=d;
  208.       f.num*=d;
  209.       return (num<f.num) ? 1 : 0;
  210.    }
  211.  
  212.    bool fraction::operator <=(fraction f)
  213.    {
  214.         int d;
  215.       d=this->den;
  216.       den*=f.den;
  217.       num*=f.den;
  218.       f.den*=d;
  219.       f.num*=d;
  220.       return (num<=f.num) ? 1 : 0;
  221.    }
  222.  
  223.    bool fraction::operator >(fraction f)
  224.    {
  225.         int d;
  226.       d=this->den;
  227.       den*=f.den;
  228.       num*=f.den;
  229.       f.den*=d;
  230.       f.num*=d;
  231.       return (num>f.num) ? 1 : 0;
  232.    }
  233.  
  234.    bool fraction::operator >=(fraction f)
  235.    {
  236.         int d;
  237.       d=this->den;
  238.       den*=f.den;
  239.       num*=f.den;
  240.       f.den*=d;
  241.       f.num*=d;
  242.       return (num>=f.num) ? 1 : 0;
  243.    }
  244.  
  245.  
  246. ostream& operator <<(ostream &s,fraction f)
  247. {
  248.   s<<f.num<<" / "<<f.den;
  249.   return s;
  250. }
  251.  
  252. int main()
  253. {
  254.      fraction a;  // Calls ZERO ARGUMENT Contructor
  255.    fraction b(3,2); // Calls 2 Argument Constructor
  256.    fraction c=1.2// Calls 1 Argument Constructor
  257.    cout<<"1.2 is equal to "<<c<<endl;
  258.    fraction d=b;    // Calls Copy Constructor
  259.    cout<<d<<" is equal to "<<d.fractofloat();//Calls fractofloat(),<< overloaded
  260.    a=6.25;         // Calls void operator =(float)
  261.    cout<<"\nValue of a = "<<a;
  262.    a=c;           // Calls void operator =(fraction)
  263.    cout<<"\nNew Value of a = "<<a<<endl;
  264.    cout<<b<<" + "<<c<<" = "<<(b+c); // Calls fraction operator +(fraction)
  265.    d=(b-a)*c/a;     // Calls -,*,/ Overloaded Functions
  266.    cout<<"\nNew Value of d = "<<d;
  267.    if(a==c) // Calls == overloaded function
  268.         cout<<endl<<a<<" is equal to "<<c;
  269.    if(b!=c) // Calls != Overloaded Function
  270.         cout<<endl<<b<<" is not equal to "<<c;
  271.    if(b>c) // Calls > Overloaded Function
  272.         cout<<endl<<b<<" is greater than "<<c;
  273.    if(b>=c)// Calls >= Overloaded Function
  274.         cout<<endl<<b<<" is greater than or equal to "<<c;
  275.    if(b<c)// Calls < Overloaded Function
  276.         cout<<endl<<b<<" is lesser than "<<c;
  277.    if(b<=c)// Calls <= Overloaded Function
  278.         cout<<endl<<b<<" is lesser than or equal to "<<c;
  279.  
  280.    return 0;
  281.  
  282. }
  283.  

Copy & Paste


Comments


idle_09 2008-04-13 00:25:25

This code is well-written and neat. Howlong did it take to write this program successfully?

venkatritch 2008-06-28 06:20:17

An excellant class for abstracting Rational numbers. I feel the class name would have been rationalnumber. Venkat www.ritchcenter.com/elearn

mythmystic 2008-07-29 17:56:27

I like the way u code. Comes with comment (//) easier to read and understand!


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month