Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Fraction& Fraction::simplify()
- {
- if (isDecimal(this->whole_Number_)) //checking if the whole number is decimal value
- {
- float decimal_value = this->whole_Number_ - (int)this->whole_Number_; //getting the decimal value
- this->whole_Number_ = this->whole_Number_; //making the whole number a int
- this->numerator_ += decimal_value * this->denominator_; //adding the access decimal value to numerator
- }
- if (isDecimal(this->numerator_)) //checking if the numerator is a decimal value
- {
- float new_numerator_ = Decimal_To_Int(this->numerator_); //getting the multiple that is a int
- this->denominator_ *= (new_numerator_ / this->numerator_); //update denominator
- this->numerator_ = (float)new_numerator_;
- }
- if (isDecimal(this->denominator_)) //checking if the denominator is a decimal value
- {
- float new_denominator_ = Decimal_To_Int(this->denominator_); //converts denominator to a int
- this->numerator_ *= (new_denominator_ / this->denominator_); //updates the numerator
- this->denominator_ = new_denominator_; //updates the denominator
- }
- if (this->numerator_ > this->denominator_) //converting to proper fractions
- {
- int i = 0;
- while (true)
- {
- if (this->numerator_ < this->denominator_) //if numerator smaller than denominator, break
- {
- break;
- }
- this->numerator_ -= this->denominator_; //carry iver whole number
- i++;
- }
- this->whole_Number_ += (float)i; //update whole number
- }
- if ((int)this->denominator_ % (int)this->numerator_ == 0) //if the fraction can be simplified
- {
- float i = this->denominator_ / this->numerator_;
- this->denominator_ /= i; //simplify the fraction
- this->numerator_ /= i;
- }
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement