Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Fraction {
- public:
- int numer;
- int denom;
- public:
- Fraction();
- Fraction( int numer , int denom );
- int getGCD();
- Fraction operator + ( Fraction & f );
- bool operator < ( Fraction & f );
- operator double() { return double(numer) / double(denom); }
- };
- ostream& operator << ( ostream & out, Fraction & f );
- Fraction::Fraction(int numer , int denom)
- {
- if (denom == 0)
- throw 0;
- int gcd = getGCD();
- if (gcd > 1) {
- numer /= gcd;
- denom /= gcd;
- }
- }
- int Fraction::getGCD()
- {
- int n = ( numer < denom ) ? numer : denom ;
- int n1, n2;
- for ( ; n>=1 ; n-- ) {
- n1 = numer % n ;
- n2 = denom % n ;
- if ( n1==0 && n2==0 ) {
- break;
- }
- }
- return n;
- }
- int main()
- {
- try {
- Fraction f( 3, 0 );
- double d = double(f);
- }
- catch ( int err ) {
- cout << "divide by " << err << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement