🎁BACK-TO-SCHOOL DEAL. Subscribe Now to get 40% OFF at only 8.49 USD/month, only valid until Sep 30th, 2024

Question

Question
Сpp C++ Structures 18 19 string to_string() { return "(" + std::to_string(a) + "/" + std::to_string(b) + ""; } 20 21 22 23 24 //This function subtracts the input fraction from this fraction 1/Do not simplify the result in any way fraction subtract(fraction f) 25 26 27 28 // Your code starts here 77 Your code ends here 29 3 30 31 // This function divides this fraction by the input fraction //Do not simplify the result in any way fraction divide(fraction f) 32 33 34 35 86 37 38 39 Your code starts here 27 Your code ends here SURMET 10:08 PM Cpp C++ Structures 30 31 32 B3 //This function divides this fraction by the input fraction 7/Do not simplify the result in any way fraction divide(fraction f) { // Your code starts here 34 35 36 37 38 39 40 // Your code ends here } I //This function simplifies the fraction //Definition of simplification: https://www.mathsisfun.com/simplifying-frac fraction simplify [ // Your code starts here 41 42 43 44 45 46 47 // Your code ends here 1 48 49 After 50 SUBMIT 1 #include #include using namespace std; I 000 NOWN struct fraction { public: int a; int b; 10 11 12 13 fraction(int _a, int _b) { a = _a; b = _b; } 14 15 16 17 18 19 20 string to_string() { return "C" + std::to_string() + "%" + std::to_string() + ": 21 1 22

Asked By TwilightSerenade45 at

Answered By Expert

Phillip

Expert · 2.6k answers · 2k people helped

Note: 
If you are a beginner I will help you in executing the code.
Could you plz go through this code and let me know if u need any changes in this.Thank You
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

#include <string>
#include <iostream>
#include <sstream>
using namespace std;

struct fraction {
public:
    int a;
    int b;
    fraction(int _a, int _b)
    {
        a = _a;
        b = _b;
    }
    string to_string()
    {
        return "{"+std::to_string(a)+"/"+std::to_string(b)+")";
    }
    fraction subtract(fraction f)
    {
        int numer = (a * f.b) + (f.a * b);
        int denom = this->b * f.b;
        fraction fraction(numer, denom);
        return fraction.simplify();
    }
    fraction divide(fraction f)
    {
        int a = a;
        int e = b;
        int c = f.a;
        int d = f.b;
        int num = (a * d);
        int den = (c * e);
        fraction frac(num, den);
        return frac.simplify();
    }
    fraction simplify()
    {
        int loop;
        int n = a < 0 ? -a : a;
        int d = b;
        int largest = n > d ? n : d;
        int gcd = 0; // greatest common divisor
        for (loop = largest; loop >= 2; loop--)
            if (a % loop == 0 && b % loop == 0) {
                gcd = loop;
                break;
            }
        if (gcd != 0) {
            a /= gcd;
            b /= gcd;
        }

        return *this;
    }
};
int main()
{
    fraction f1(5, 10), f2(3, 9);
    cout << "Fraction#1:" << f1.to_string() << endl;
    cout << "Fraction#2:" << f2.to_string() << endl;
    fraction fsub = f1.subtract(f2);
    fraction fdiv = f1.divide(f2);
    cout << f1.to_string() << " - " << f2.to_string() << " = " << fsub.to_string() << endl;
    cout << f1.to_string() << " - " << f2.to_string() << " = " << fdiv.to_string() << endl;

    return 0;
}

 

+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

Output:

C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\StructfractionAddSubtractDivide.exe\nFraction#1:(5/10)\nFraction#2:(3/9)\n(5/10) (3/9

+=+=+=+=+=+=+=+=+=+ Could you plz rate me well.Thank You

🧑‍🏫 More Questions