SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <iostream> | |
| 2 | ||
| 3 | class Fraction{
| |
| 4 | public: | |
| 5 | Fraction(); | |
| 6 | Fraction(long num, long den); | |
| 7 | - | void setNum(int); |
| 7 | + | |
| 8 | - | void setDen(int); |
| 8 | + | void setNum(long new_num); |
| 9 | - | int getNum(); |
| 9 | + | void setDen(long new_den); |
| 10 | - | int getDen(); |
| 10 | + | long getNum(); |
| 11 | - | long gcd(long,long); |
| 11 | + | long getDen(); |
| 12 | - | Fraction add(Fraction,Fraction); |
| 12 | + | |
| 13 | - | Fraction sub(Fraction,Fraction); |
| 13 | + | void add(Fraction other); |
| 14 | - | Fraction mult(Fraction,Fraction); |
| 14 | + | void sub(Fraction other); |
| 15 | - | Fraction div(Fraction,Fraction); |
| 15 | + | void mult(Fraction other); |
| 16 | - | Fraction inc(Fraction); |
| 16 | + | void div(Fraction other); |
| 17 | void inc(); | |
| 18 | void print(); | |
| 19 | - | void Fraction::setNum(int num) |
| 19 | + | private: |
| 20 | long num; | |
| 21 | - | Fraction::num = num; |
| 21 | + | |
| 22 | }; | |
| 23 | - | void Fraction::setDen(int den) |
| 23 | + | |
| 24 | long gcd(long x, long y); | |
| 25 | - | Fraction::den = den; |
| 25 | + | |
| 26 | Fraction::Fraction(): | |
| 27 | - | int Fraction::getNum() |
| 27 | + | num(0), |
| 28 | den(1) | |
| 29 | - | return Fraction::num; |
| 29 | + | |
| 30 | } | |
| 31 | - | int Fraction::getDen() |
| 31 | + | |
| 32 | Fraction::Fraction(long num, long den): | |
| 33 | - | return Fraction::den; |
| 33 | + | num(num), |
| 34 | den(den) | |
| 35 | - | long Fraction::gcd(long x, long y) |
| 35 | + | |
| 36 | } | |
| 37 | - | return (x==0)?y:gcd(y%x,x); |
| 37 | + | |
| 38 | void Fraction::setNum(long new_num) | |
| 39 | - | Fraction Fraction::add(Fraction first, Fraction second) |
| 39 | + | |
| 40 | num = new_num; | |
| 41 | - | Fraction sum; |
| 41 | + | |
| 42 | - | sum.setNum(first.num*second.den+second.num*first.den); |
| 42 | + | |
| 43 | - | sum.setDen(first.den*second.den); |
| 43 | + | void Fraction::setDen(long new_den) |
| 44 | - | return sum; |
| 44 | + | |
| 45 | den = new_den; | |
| 46 | - | Fraction Fraction::sub(Fraction first, Fraction second) |
| 46 | + | |
| 47 | ||
| 48 | - | Fraction dif; |
| 48 | + | long Fraction::getNum() |
| 49 | - | dif.setNum(first.num*second.den-second.num*first.den); |
| 49 | + | |
| 50 | - | dif.setDen(first.den*second.den); |
| 50 | + | return num; |
| 51 | - | return dif; |
| 51 | + | |
| 52 | ||
| 53 | - | Fraction Fraction::mult(Fraction first, Fraction second) |
| 53 | + | long Fraction::getDen() |
| 54 | {
| |
| 55 | - | Fraction prod; |
| 55 | + | return den; |
| 56 | - | prod.setNum(first.num*second.num); |
| 56 | + | |
| 57 | - | prod.setDen(first.den*second.den); |
| 57 | + | |
| 58 | - | return prod; |
| 58 | + | void Fraction::add(Fraction other) |
| 59 | {
| |
| 60 | - | Fraction Fraction::div(Fraction first, Fraction second) |
| 60 | + | num = num * other.den + other.num * den; |
| 61 | den *= other.den; | |
| 62 | - | Fraction div; |
| 62 | + | |
| 63 | - | div.setNum(first.num*second.den); |
| 63 | + | |
| 64 | - | div.setDen(first.den*second.num); |
| 64 | + | void Fraction::sub(Fraction other) |
| 65 | - | return div; |
| 65 | + | |
| 66 | num = num * other.den - other.num * den; | |
| 67 | - | /*Fraction Fraction::inc(Fraction f) |
| 67 | + | den *= other.den; |
| 68 | } | |
| 69 | - | Fraction temp; |
| 69 | + | |
| 70 | - | temp.setNum(num+=den); |
| 70 | + | void Fraction::mult(Fraction other) |
| 71 | - | return temp; |
| 71 | + | |
| 72 | - | }*/ |
| 72 | + | num *= other.num; |
| 73 | den *= other.den; | |
| 74 | } | |
| 75 | - | Fraction f; |
| 75 | + | |
| 76 | - | f.setNum(Fraction::getNum()/gcd(Fraction::getNum(),Fraction::getDen())); |
| 76 | + | void Fraction::div(Fraction other) |
| 77 | - | f.setDen(Fraction::getDen()/gcd(Fraction::getNum(),Fraction::getDen())); |
| 77 | + | |
| 78 | - | std::cout<<f.getNum()<<"/"<<f.getDen()<<"\n"; |
| 78 | + | num *= other.den; |
| 79 | den *= other.num; | |
| 80 | } | |
| 81 | ||
| 82 | void Fraction::inc() | |
| 83 | {
| |
| 84 | num += den; | |
| 85 | } | |
| 86 | ||
| 87 | void Fraction::print() | |
| 88 | {
| |
| 89 | - | f3.add(f1,f2); |
| 89 | + | long divisor = gcd(num, den); |
| 90 | std::cout << num / divisor << "/" << den / divisor << std::endl; | |
| 91 | - | f4.sub(f1, f2); |
| 91 | + | |
| 92 | ||
| 93 | - | f5.mult(f1, f2); |
| 93 | + | long gcd(long x, long y) |
| 94 | {
| |
| 95 | - | f6.div(f1, f2); |
| 95 | + | return (x == 0) ? y : gcd(y%x, x); |
| 96 | } | |
| 97 | - | //f7.inc(f1); |
| 97 | + | |
| 98 | - | //f7.print(); |
| 98 | + | |
| 99 | {
| |
| 100 | Fraction f1, f2, f3, f4, f5, f6, f7; | |
| 101 | f1.setDen(2L); | |
| 102 | f1.setNum(0L); | |
| 103 | f1.print(); | |
| 104 | f2.setDen(4L); | |
| 105 | f2.setNum(3L); | |
| 106 | f2.print(); | |
| 107 | f3 = f1; | |
| 108 | f3.add(f2); | |
| 109 | f3.print(); | |
| 110 | f4 = f1; | |
| 111 | f4.sub(f2); | |
| 112 | f4.print(); | |
| 113 | f5 = f1; | |
| 114 | f5.mult(f2); | |
| 115 | f5.print(); | |
| 116 | f6; | |
| 117 | f6.div(f2); | |
| 118 | f6.print(); | |
| 119 | f7 = f1; | |
| 120 | f7.inc(); | |
| 121 | f7.print(); | |
| 122 | } |