I'm writing a program for a class that does operations with rational numbers. I'm not finished with it, but right now i'm working on the is_negative() function which is required to be in the program by the teacher. He has set up a make file to test each part of the program and i have gotten the through the tests to make the constructors work properly. Now i'm on the is_negative test but for some reason is will only work for objects that are initialized to something without a 1 in the denominator. So something like rational rat(3,2), rat1(5,4) will work. The problem comes when an uninitialized obj such as rational rat, rational rat(2) and rat2(2,1) is used. For some reason the the is_negative function is returning 204 for the neg variable when it is a boolean variable and should return 0 or 1. If someone could help i would really appreciate it. Keep in mind i'm not finished so i just need help with the is_negative function. Thank you.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Rational{
public:
int num;
int den;
bool neg;
Rational(int n, int d); // contructor for rationals
Rational(int n); // contructor for reals
Rational();//default constructor
int get_num();
int get_denom();
int gcd();
void simplify();
bool is_negative();
Rational operator +(Rational);
Rational operator -(Rational);
Rational operator *(Rational);
Rational operator /(Rational);
};
int main(){
Rational result, a, rat(2,1), rat2(0), rat3(6,-3), rat4(-3,-8);
result.den = rat2.get_denom();
result.num = rat2.get_num();
result.neg = rat2.is_negative();
cout << result.num << "/" << result.den << endl;
cout << result.neg << endl;
}
Rational::Rational(int n, int d)
{
if(n == 0)
{
num = n;
den = 1;
}else
{
num = n;
den = d;
}
}
Rational::Rational(int n)
{
num = n;
den = 1;
}
Rational::Rational()
{
num = 0;
den = 1;
}
int Rational::get_num()
{
simplify();
return abs(num);
}
int Rational::get_denom()
{
simplify();
return abs(den);
}
int Rational::gcd()
{ int a = num;
int b = den;
int temp;
while(b != 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}
void Rational::simplify()
{
int gcdNum = gcd();
if(gcdNum != 0)
{
num = num / gcdNum;
den = den / gcdNum;
}
}
bool Rational::is_negative()
{
if(den == 1)
{
if(num < 0)
{
neg = true;
}
}
else{
if(num||den < 0)
neg = true;
else
neg = false;
}
return neg;
}
Rational Rational:: operator +(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.den + den * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
Rational Rational:: operator -(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.den - den * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
Rational Rational:: operator *(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
Rational Rational:: operator /(Rational ratPassed)
{
Rational ratResult;
ratResult.num = num * ratPassed.den;
ratResult.den = den * ratPassed.num;
return ratResult;
}