Question
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:
+=+=+=+=+=+=+=+=+=+ Could you plz rate me well.Thank You
🧑🏫 More Questions
👉 Interested in exploring further?
Chrome Extension
1. Search answers from our 90+ million questions database.
2. Get instantly AI Solutions powered by most advanced models like GPT-4, Bard, Math GPT, etc.
3. Enjoy one-stop access to millions of textbook solutions.
4. Chat with 50+ AI study mates to get personalized course studies.
5. Ask your questions simply with texts or screenshots everywhere.