SHARE
TWEET

Untitled

a guest Jan 19th, 2020 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include<istream>
  4. using namespace std;
  5. class Fraction {
  6. public:
  7. Fraction(int numerator, int denominator);
  8. string toString();
  9.  
  10. double toDouble();
  11.  
  12.  
  13.  
  14. Fraction plus(Fraction that);
  15. Fraction minus(Fraction that);
  16. Fraction times(Fraction that);
  17. Fraction by(Fraction that);
  18.  
  19. private:  int numerator;  int denominator;
  20.        void reduce(int num, int den) {
  21.            int x = num + den;
  22.            for (int i = x; i > 0; i--) {
  23.                if (num % i == 0 && den % i == 0){
  24.                    num = num / i;
  25.                    den = den / i;
  26.                }
  27.            }
  28.        };
  29. };
  30. Fraction::Fraction(int numerator, int denominator) {
  31.     this->numerator = numerator;
  32.     this->denominator = denominator;
  33. }
  34.  
  35. Fraction Fraction::plus(Fraction that) {
  36.     int num = this->numerator + that.denominator;
  37.     int den = this->denominator + that.denominator;
  38.     Fraction result(num, den);
  39.     result.reduce(num, den);
  40.     cout << "add" << result<< endl;
  41.     return result;
  42. }
  43. Fraction Fraction::minus(Fraction that) {
  44.     int num = this->numerator - that.denominator;
  45.     int den = this->denominator - that.denominator;
  46.     Fraction result(num, den);
  47.     result.reduce(num, den);
  48.     cout << "minus" << result << endl;
  49.     return result;
  50. }
  51. Fraction Fraction::times(Fraction that) {
  52.     int num = this->numerator * that.denominator;
  53.     int den = this->denominator * that.denominator;
  54.     Fraction result(num, den);
  55.     result.reduce(num, den);
  56.     cout << "times" << result << endl;
  57.     return result;
  58. }
  59. Fraction Fraction::by(Fraction that) {
  60.     int num = this->numerator / that.denominator;
  61.     int den = this->denominator / that.denominator;
  62.     Fraction result(num, den);
  63.     result.reduce(num, den);
  64.     cout << "by" << result << endl;
  65.     return result;
  66. }
  67. string Fraction::toString() {
  68.     int x = 0;
  69.     if (numerator > denominator) {
  70.         int a = numerator / denominator;
  71.         for (int i = 0; i < a; i++) {
  72.             numerator -= denominator;
  73.             x += 1;
  74.         }
  75.     }
  76.     string y = "";
  77.     if (x > 0) {
  78.         y += to_string(x);
  79.     }
  80.     y += " ";
  81.     y += to_string(numerator);
  82.     y += "/";
  83.     y += to_string(denominator);
  84.     return y;
  85.  
  86. }
  87. double Fraction::toDouble() {
  88.     if (denominator < 0) {
  89.         numerator = numerator * -1;
  90.         denominator = denominator * -1;
  91.     }
  92.     return numerator, denominator;
  93. }
  94.  
  95. int main(void){
  96.     int x, y;
  97.     string input = "";
  98.     getline(cin, input);
  99.     sscanf_s(input.c_str(), "%d / %d", &x, &y);
  100.     Fraction fraction(x, y);
  101.     int x1, y1;
  102.     getline(cin, input);
  103.     sscanf_s(input.c_str(), "%d / %d", &x1, &y1);
  104.     Fraction fraaction(x1, y1);
  105.  
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top