Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Fraction{
- public:
- Fraction();
- Fraction(long num, long den);
- void setNum(long new_num);
- void setDen(long new_den);
- long getNum();
- long getDen();
- void add(Fraction other);
- void sub(Fraction other);
- void mult(Fraction other);
- void div(Fraction other);
- void inc();
- void print();
- private:
- long num;
- long den;
- };
- long gcd(long x, long y);
- Fraction::Fraction():
- num(0),
- den(1)
- {
- }
- Fraction::Fraction(long num, long den):
- num(num),
- den(den)
- {
- }
- void Fraction::setNum(long new_num)
- {
- num = new_num;
- }
- void Fraction::setDen(long new_den)
- {
- den = new_den;
- }
- long Fraction::getNum()
- {
- return num;
- }
- long Fraction::getDen()
- {
- return den;
- }
- void Fraction::add(Fraction other)
- {
- num = num * other.den + other.num * den;
- den *= other.den;
- }
- void Fraction::sub(Fraction other)
- {
- num = num * other.den - other.num * den;
- den *= other.den;
- }
- void Fraction::mult(Fraction other)
- {
- num *= other.num;
- den *= other.den;
- }
- void Fraction::div(Fraction other)
- {
- num *= other.den;
- den *= other.num;
- }
- void Fraction::inc()
- {
- num += den;
- }
- void Fraction::print()
- {
- long divisor = gcd(num, den);
- std::cout << num / divisor << "/" << den / divisor << std::endl;
- }
- long gcd(long x, long y)
- {
- return (x == 0) ? y : gcd(y%x, x);
- }
- int main()
- {
- Fraction f1, f2, f3, f4, f5, f6, f7;
- f1.setDen(2L);
- f1.setNum(0L);
- f1.print();
- f2.setDen(4L);
- f2.setNum(3L);
- f2.print();
- f3 = f1;
- f3.add(f2);
- f3.print();
- f4 = f1;
- f4.sub(f2);
- f4.print();
- f5 = f1;
- f5.mult(f2);
- f5.print();
- f6;
- f6.div(f2);
- f6.print();
- f7 = f1;
- f7.inc();
- f7.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement