Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int gcd(int a,int b)
- {
- if (b==0) return a;
- else return gcd(b,a%b);
- }
- class fraction
- {
- private:
- int divident;
- int divider;
- public:
- fraction() {}
- fraction(int up, int down){
- divident = up;
- divider = down;
- }
- void setDivident(int d){
- divident = d;
- }
- void setDivider(int d){
- divider = d;
- }
- int getDivident(){
- return divident;
- }
- int getDivider(){
- return divider;
- }
- fraction operator+ (fraction s){
- fraction result;
- if (this->getDivider() == s.getDivider()){
- result.setDivident(s.getDivident() + this->getDivident());
- result.setDivider(this->getDivider());
- return result;
- }
- int nod = gcd(this->getDivider(), s.getDivider());
- if (nod != 1) {
- int side_a = s.getDivider()/nod;
- int side_b = this->getDivider()/nod;
- result.setDivident(side_a*this->getDivident() + side_b*s.getDivident());
- result.setDivider(side_a*side_b*nod);
- }
- else{
- result.setDivident(s.getDivider()*this->getDivident() + this->getDivider()*s.getDivident());
- result.setDivider(s.getDivider()*this->getDivider());
- }
- return result;
- }
- void operator=(fraction r){
- this->setDivident(r.getDivident());
- this->setDivider(r.getDivider());
- }
- void print(){
- cout << divident << "/" << divider << endl;
- }
- };
- int main()
- {
- fraction a(1,2);
- fraction b(3,4);
- fraction c(1,3);
- fraction d = c+ a;
- d.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement