Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <iostream> //input output
  2. #include <string> // string
  3. #include <cstdlib> // die
  4. using namespace std;
  5.  
  6. class Rational {
  7. public:
  8. Rational(unsigned long long numerator = 0, unsigned long long denominator = 1); // ctor
  9. unsigned long long getNumerator() const; // getting numerator
  10. unsigned long long getDenominator() const; // getting denominator
  11. double getFloatingValue() const; // getting a floating point/decimal value
  12. const Rational & output() const; // output
  13. Rational add(const Rational & other) const; // add
  14. static unsigned long long gcf(unsigned long long a, unsigned long long b); // greatest common factor
  15. private:
  16. unsigned long long DeNoMiNaToR, NuMeRaToR;
  17. }; // class Rational
  18.  
  19. // constructor definition goes here
  20. Rational::Rational(unsigned long long numerator, unsigned long long denominator) {
  21. unsigned long long tempnum = numerator;
  22. unsigned long long tempden = denominator;
  23.  
  24. for (unsigned long long i = tempden * tempnum; i > 1; i--) {
  25. if ((tempden % i == 0) && (tempnum % i == 0)) {
  26. tempden /= i;
  27. tempnum /= i;
  28. } // simplifying fractions
  29. else
  30. break;
  31. }
  32.  
  33. NuMeRaToR = tempnum;
  34. DeNoMiNaToR = tempden;
  35. }
  36.  
  37. // output
  38. const Rational & Rational::output() {
  39. cout << NuMeRaToR "/" << DeNoMiNaToR << endl;
  40. }
  41.  
  42.  
  43. // add takes 2 rational functions and adds them together after they've been simplified
  44. // to add 2 rational functions, formula is ::::::: (A/B) + (C/D) = ((A*D) + (C*B)) / (B*D)
  45. // Second object in add should be the parameter (when called in main)
  46.  
  47. unsigned long long Rational::getNumerator() const { //after simplification
  48. return NuMeRaToR;
  49. }
  50.  
  51. unsigned long long Rational::getDenominator() const { //after simplification
  52. return DeNoMiNaToR;
  53. }
  54.  
  55. bool die(const string &msg); // die
  56.  
  57. int main() {
  58. Rational r1;// make a Rational object
  59. unsigned num, den;
  60. cout << "Enter numerator, denomintor: ";
  61. cin >> num >> den; //getting values to pass to get functions
  62.  
  63. if (den == 0) die("Division by 0 is illegal"); // c&d
  64.  
  65. r1.output();
  66.  
  67. // call output function
  68.  
  69. // call gcf function
  70.  
  71.  
  72.  
  73. } // main
  74.  
  75. bool die(const string &msg) {
  76. cout << "Fatal error: " << msg << endl;
  77. exit(EXIT_FAILURE);
  78. } // die
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement