Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. Fraction& Fraction::simplify()
  2. {
  3. if (isDecimal(this->whole_Number_)) //checking if the whole number is decimal value
  4. {
  5. float decimal_value = this->whole_Number_ - (int)this->whole_Number_; //getting the decimal value
  6. this->whole_Number_ = this->whole_Number_; //making the whole number a int
  7. this->numerator_ += decimal_value * this->denominator_; //adding the access decimal value to numerator
  8. }
  9. if (isDecimal(this->numerator_)) //checking if the numerator is a decimal value
  10. {
  11. float new_numerator_ = Decimal_To_Int(this->numerator_); //getting the multiple that is a int
  12. this->denominator_ *= (new_numerator_ / this->numerator_); //update denominator
  13. this->numerator_ = (float)new_numerator_;
  14. }
  15. if (isDecimal(this->denominator_)) //checking if the denominator is a decimal value
  16. {
  17. float new_denominator_ = Decimal_To_Int(this->denominator_); //converts denominator to a int
  18. this->numerator_ *= (new_denominator_ / this->denominator_); //updates the numerator
  19. this->denominator_ = new_denominator_; //updates the denominator
  20. }
  21. if (this->numerator_ > this->denominator_) //converting to proper fractions
  22. {
  23. int i = 0;
  24. while (true)
  25. {
  26. if (this->numerator_ < this->denominator_) //if numerator smaller than denominator, break
  27. {
  28. break;
  29. }
  30. this->numerator_ -= this->denominator_; //carry iver whole number
  31. i++;
  32. }
  33. this->whole_Number_ += (float)i; //update whole number
  34. }
  35. if ((int)this->denominator_ % (int)this->numerator_ == 0) //if the fraction can be simplified
  36. {
  37. float i = this->denominator_ / this->numerator_;
  38. this->denominator_ /= i; //simplify the fraction
  39. this->numerator_ /= i;
  40. }
  41. return *this;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement