Created
October 1, 2012 07:23
-
-
Save krofna/3810085 to your computer and use it in GitHub Desktop.
Razlomci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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