#include <iostream.h>
class Rational
{
private:
int zaehler, nenner;
public:
/* GR für die Berechnung des größten
gemeinsamen Teilers des Nenners;
kann mit dem ggt kürzen
*/
int ggt(int, int);
/* GR berechne das größte gemeinsame
Vielfache zweier Rationals
*/
void ggv(Rational& r);
Rational();
Rational(int, int n=1);
friend Rational operator + (Rational, Rational);
friend ostream& operator << (ostream&, Rational&);
};
Rational::Rational()
{
zaehler = 0;
nenner = 1;
}
Rational::Rational(int Zaehler, int Nenner)
{
zaehler = Zaehler;
nenner = Nenner;
}
int Rational::ggt(int a, int b)
{
if (b == 0)
return a;
else
return ggt(b, a%b);
}
void Rational::ggv(Rational& r)
{
int nenner_alt = nenner;
nenner *= r.nenner;
zaehler *= r.nenner;
r.nenner *= nenner_alt;
r.zaehler *= nenner_alt;
}
Rational operator + (Rational r1, Rational r2)
{
int g = r1.ggt(r1.nenner, r2.nenner);
r1.ggv(r2);
Rational temp(r1.zaehler + r2.zaehler, r1.nenner);
temp.zaehler /= g;
temp.nenner /= g;
return temp;
}
ostream& operator << (ostream& s, Rational& r)
{
return s << r.zaehler << '/' << r.nenner;
}
main()
{
Rational a(1,2), b(1,1), c;
Rational x(5,8), y(1,6), z;
c = a + b;
cout << a << "+" << b << "=" << c << endl;
z = x + y;
cout << x << "+" << y << "=" << z << endl;
}