Skip to content

Instantly share code, notes, and snippets.

@krofna
Created October 1, 2012 07:23
Show Gist options
  • Save krofna/3810085 to your computer and use it in GitHub Desktop.
Save krofna/3810085 to your computer and use it in GitHub Desktop.
Razlomci
#include <iostream>
// Najveci zajednicki djelitelj
// Euklidov algoritam
int gcd(int a, int b)
{
if(b == 0)
{
return a;
}
return gcd(b, a % b);
}
class Razlomak
{
public:
friend std::istream& operator>> (std::istream& i, Razlomak& r)
{
return i >> r.a >> r.b;
}
friend std::ostream& operator<< (std::ostream& o, Razlomak& r)
{
return o << r.a << " " << r.b;
}
Razlomak Skrati(Razlomak& Raz)
{
int _gcd = gcd(Raz.a, Raz.b);
Raz.a /= _gcd;
Raz.b /= _gcd;
return Raz;
}
Razlomak operator+(const Razlomak& Drugi)
{
Razlomak Rez;
Rez.b = this->b * Drugi.b;
Rez.a = this->a * Drugi.b + this->b * Drugi.a;
return Skrati(Rez);
}
private:
int a, b;
};
int main()
{
Razlomak Prvi, Drugi, Rez;
std::cin >> Prvi >> Drugi;
Rez = Prvi + Drugi;
std::cout << Rez;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment