Advanced Data Type

So, ofcourse not asking you to do my assignment for me, So I have
the work I tried attached.

Could someone simplify this to me in c++ language?

Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class Rational. Implement the following functions for this class:

1. Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values.
2. Also include a constructor that has only a single parameter of type int; define the constructor such that an object is initialized with denominator always equal to 1.
3. Also include a default constructor that initializes an object to 0 (that is, to 0/1).
4. Overload all of the following operators so that they correctly apply to the type Rational:
==, <, >, +, -, *. Make all the overloaded operators "friends" of the class.
5. Implement Print() function that prints any rational number in the form of numerator/denominator.

Remember to make all member variables "private".
When implementing ==, note that the rational number 2 is equal to 2/1 and 4/2 both. Similarly, implement all other operators properly. You should also check for wrong inputs, say 1/0.

and this is my work....where did I go wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
using namespace std;

class rational
{

 friend bool operator==(rational,rational);
 friend bool operator<(rational,rational);
 friend bool operator>(rational,rational);
 friend rational operator+(rational,rational);
 friend rational operator-(rational,rational);
 friend rational operator*(rational,rational);

private:
  int numerator;
  int denominator;

public:
  rational();
  rational(int, int);
  void setnumerator(int);
  void setdinominator(int);
  int getnumerator();
  int getdenominator();
  void print();
}


rational::rational()
{
       numerator=0;
       denominator=1;
}

rational::rational(int n, int d)
{
       numerator=n;
       denominator=d;

void rational::Print()
{
	cout<<numerator<<"/"<<denominator<<endl;
}

void rational::setnumerator(int n)
{
        numerator=n;
}

void rational::setdenominator(int d)
{
        denominator=d;
}

int rational::getnumerator()
{
        return numerator;
}

int rational::getdenominator()
{
        return denominator;
}


bool operator==rational x, rational y)
{
	if(x.numerator==y.numerator && x.denominator==y.denominator )
		return true;
	else return false;
}

rational operator+(rational x, rational y)
{
	rational c(x.numerator+y.numerator, x.denominator+y.denominator);
	return c;
}

rational operator-(rational x, rational y)
{
	rational c(x.numerator-y.numerator, x.denominator-y.denominator);
	return c;
}

bool operator<rational x, rational y)
{
	if(x.numerator<y.numerator && x.denominator<y.denominator )
		return true;
	else return false;
}

bool operator>rational x, rational y)
{
	if(x.numerator>y.numerator && x.denominator>y.denominator )
		return true;
	else return false;
}

rational operator*(rational x, rational y)
{
	rational c(x.numerator*y.numerator, x.denominator*y.denominator);
	return c;
}
Last edited on
This is just the start. On line 39 you should have }
In the constructor starting on line 35 you should check if the denominator is 0.
For operator ==, you were asked to make sure that 4/2==2/1. The way to do it is to check x.numerator*y.denominator==y.numerator*x.denominator. This will also catch 0/1==0/3
For operator + (and -), you got the formula wrong a/b+c/d is not (a+c)/(b+d)
Formulas for > and < are wrong as well
> and this is my work....where did I go wrong?

Recall lessons on basic arithmetic with fractions. If you cant, refer to your old mathematics text book.

How would you compare the two fractions 4/6 and 12/15? Are they equal?

How would you add 4/6 and 12/15?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <stdexcept>
#include <cstdlib>

int gcd( int a, int b )
{
    if( b == 0 ) return a ;
    else return gcd( b, a%b ) ;
}

struct rational
{
    rational() : num(0), denom(1) {}
    rational( int numerator ) : num(numerator), denom(1) {}
    rational( int numerator, int denominator ) : num(numerator), denom(denominator) { normalise() ; }

    int numerator() const { return num ; }
    int denominator() const { return denom ; }

    rational& operator+= ( rational that )
    {
        num = this->num * that.denom + that.num * this->denom ;
        denom = this->denom * that.denom ;
        normalise() ;
        return *this ;
    }

    rational& operator-= ( rational that )
    {
        that.num = -that.num ;
        return operator+= (that) ;
    }
    // and so on for the other compound assignment operators


    private:
        int num ;
        int denom ;

        void normalise()
        {
            if( denom == 0 ) throw std::range_error( "denominator can't be zero" ) ;

            bool negative = ( ( num < 0 ) + ( denom < 0 ) ) % 2 ;
            const int abs_num = std::abs(num) ;
            const int abs_denom = std::abs(denom) ;
            const int common_divisor = gcd( abs_num, abs_denom ) ;

            num = abs_num / common_divisor ;
            denom = abs_denom / common_divisor ;
            if(negative) num = -num ;
        }
};

std::ostream& print( std::ostream& stm, rational r )
{ return stm << '(' << r.numerator() << ',' << r.denominator() << ')' ; }

std::ostream& operator<< ( std::ostream& stm, rational r ) { return print( stm, r ) ; }


bool operator== ( rational a, rational b )
{ return a.numerator() == b.numerator() && a.denominator() == b.denominator() ; }

bool operator!= ( rational a, rational b ) { return !( a == b ) ; }

bool operator< ( rational a, rational b )
{ return a.numerator() * b.denominator() < b.numerator() * a.denominator() ; }

bool operator>= ( rational a, rational b ) { return !( b < a ) ; }
// and so on for the other comparison operators


rational operator+ ( rational a, rational b ) { return a += b ; }
rational operator- ( rational a, rational b ) { return a -= b ; }
// and so on for the other arithmetic operators 
Thank you JLBorges and ats15

However my math abilities are really bad, and I couldn't withstand the pressure.
Could you guys provide me with the Operator+ operator- operator< operator> relative to fractions.

Appreciated,
Topic archived. No new replies allowed.