Double to Fraction.

Hey, I am writing a Fraction class for an assignment. I think I am pretty close to done, however, I am having trouble going about converting a double number into fraction form. Its a requirement for it to be able to be cast from type double to type Fraction. Also if you see any errors or better ways to do some of the code I would appreciate some advice.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228`` `````` #pragma once #if !defined (__FRACTION_H__) #define __FRACTION_H__ #include using namespace std; class Fraction { public: friend ostream& operator<< ( ostream& stream, const Fraction& f); friend static int GCD(int, int); public: Fraction(int numerator = 0, int denominator = 1); Fraction(double numerator, double denominator); ~Fraction ( void ); Fraction(const Fraction& other); Fraction& operator =(const Fraction& rhs); Fraction operator= (int rhs){numerator = rhs; denominator = 1; return *this;}; Fraction operator= (double rhs){numerator = rhs; denominator = 1.0; return *this;}; Fraction& operator+=(const Fraction& rhs); Fraction& operator-=(const Fraction& rhs); Fraction& operator/=(const Fraction& rhs); Fraction& operator*=(const Fraction& rhs); friend Fraction operator+ (const Fraction& lhs, const Fraction& rhs); friend Fraction operator* (const Fraction& lhs, const Fraction& rhs); friend Fraction operator- (const Fraction& lhs, const Fraction& rhs); friend Fraction operator/ (const Fraction& lhs, const Fraction& rhs); void setPrintLeadingInt(bool enable = true); void ifZeroDen(bool zero = true); void setNegative(); void decimalToFraction(double decimalValue, const Fraction& rhs); private: int numerator; int denominator; void reduce(void); void printFraction(); static bool printLeadingInt; const int defaultFracDigits; }; #endif #include "fraction.h" bool Fraction::printLeadingInt = false; Fraction::Fraction(int num, int den) : defaultFracDigits(10) { numerator = num; denominator = den; reduce(); } Fraction::Fraction(double num, double den) : defaultFracDigits(10) { numerator = num; denominator = den; reduce(); } Fraction::Fraction(const Fraction& other) : defaultFracDigits(10) { this->numerator = other.numerator; this->denominator = other.denominator; } Fraction::~Fraction ( void ) { } Fraction& Fraction::operator+= (const Fraction& rhs) { numerator = rhs.numerator + numerator; denominator = rhs.denominator; reduce(); return *this; } Fraction& Fraction::operator-=(const Fraction& rhs) { numerator = rhs.numerator - numerator; denominator = rhs.denominator; reduce(); return *this; } Fraction& Fraction::operator/=(const Fraction& rhs) { numerator = rhs.denominator * numerator; denominator = rhs.numerator * denominator; reduce(); return *this; } Fraction& Fraction::operator*=(const Fraction& rhs) { denominator = rhs.denominator * denominator; numerator = rhs.numerator * numerator; reduce(); return *this; } void Fraction::setPrintLeadingInt(bool enable) { this->printLeadingInt = enable; } Fraction& Fraction::operator= (const Fraction& rhs) { this->numerator = rhs.numerator; this->denominator = rhs.denominator; return *this; } ostream& operator<< ( ostream& stream, const Fraction& f) { int intValue, numValue; if(f.printLeadingInt) { intValue = (abs(f.numerator/f.denominator)); if(intValue) { numValue =abs(f.numerator)%f.denominator; } } else stream << "(" << f.numerator << "/" << f.denominator << ")"; return stream; } Fraction operator+ (const Fraction& lhs, const Fraction& rhs) { Fraction temp; temp += rhs; return temp; } Fraction operator* (const Fraction& lhs, const Fraction& rhs) { Fraction temp; temp *= rhs; return temp; } Fraction operator- (const Fraction& lhs, const Fraction& rhs) { Fraction temp; temp -= rhs; return temp; } Fraction operator/ (const Fraction& lhs, const Fraction& rhs) { Fraction temp(lhs); temp /= rhs; return temp; } int GCD(int n, int d) { n = abs(n); d = abs(d); int t; if ( d == 0 ) return n; else while ( d != 0 ) { t = d; d = n % d; n = t; } return n; } void Fraction::reduce(void) { int gcd = GCD(numerator, denominator); numerator /= gcd; denominator /= gcd; } void Fraction::setNegative() { if ((numerator < 0 && denominator < 0) || (numerator >= 0 && denominator < 0)) { numerator = -numerator; } void decimalToFraction(double decimalValue, const Fraction& rhs) { } }``````
I'm not sure if this is the best approach but one method would be to figure out how many digits are after the decimal (.) then , set the numerator equal to something like this`static_cast<int>( decimal_value * 10^number_of_digits_after_decimal );` then the numerator denominator would be `10^number_of_digits_after_decimal;`

*fixed small typo

**when I put ^ I didn't mean the XOR operator I mean to actually put it to the power of that value.
Last edited on
Topic archived. No new replies allowed.