// rational.cpp - Script 9.5

#include <iostream>

#include <string>

// Declare the class.
class Rational {
      
public:
             
// Constructor:
Rational(int num, int denom);

// The overloaded methods that implement
// arithmetic functions:
Rational operator+(Rational rhs);
Rational operator-(Rational rhs);
Rational operator*(Rational rhs);
Rational operator/(Rational rhs);
              
void print();
              
private:
// normalize() will take care of transforming
// numerator and denominator into a well defined
// format.
void normalize();

int numerator;
int denominator;
};

// Define the constructor.
Rational::Rational(int num, int denom) {
                       
// Assign the values.
numerator = num;
denominator = denom;
                       
// Call normalize in case the user
// passed "garbage" to the object.
normalize();
}
                       
// Overload the operators.
Rational Rational::operator+(Rational rhs) {
         
// a c a*d c*b a*d + c*b
// - + - = --- + --- = ---------
// b d b*d b*d  b*d
         
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
         
// Calculate the new numerator and denominator.
int e = a*d + c*b;
int f = b*d;
         
// Return the resulting Rational
return Rational(e,f);
}
         
Rational Rational::operator-(Rational rhs) {
                  
// a c a -c
// - - - = - + --
// b d b d
                  
// Change the sign of the right-hand side
rhs.numerator = -rhs.numerator;
                  
// Now just add the two, using the operator+ method call.
return operator+(rhs);
}
                  
Rational Rational::operator*(Rational rhs) {
         
// a c a*c
// - * = ---
// b d b*d
         
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
         
// Calculate the new numerator and denominator.
int e = a*c;
int f = b*d;
         
// Return the resulting Rational.
return Rational(e,f);
}
         
Rational Rational::operator/(Rational rhs) {
                  
// a c a d
// - / - = - * -
// b d b c
                  
// Invert the right-hand side
int t = rhs.numerator;
rhs.numerator = rhs.denominator;
rhs.denominator = t;
                  
// Now just multiply the Rationals.
return operator*(rhs);
}
                  
// Define the methods.
void Rational::print() {
std::cout << numerator << "/" << denominator;
}
                       
void Rational::normalize() {
                            
//Check the signs.
if (denominator < 0) {
// "Move" the sign to the nominator.
numerator = -numerator;
denominator = -denominator;
}
                
// Calculate the greatest common denominator of a/b
// using Euclid's algorithm.
int a = abs(numerator);
int b = abs(denominator);
while (b > 0) {
int t = a % b;
a = b;
b = t;
}

// Divide both numbers by a.
numerator /= a;
denominator /= b;
}


int main() {
    
// Create two Rationals: 2/16 and 7/8.
Rational f1(2,16);
Rational f2(7,8);
    
// Test addition.
Rational res = f1 + f2;
f1.print();
std::cout << " + ";
f2.print();
std::cout << " == ";
res.print();
std::cout << "\n";
    
// Test subtraction.
res = f1 - f2;
f1.print();
std::cout << " - ";
f2.print();
std::cout << " == ";
res.print();
std::cout << "\n";
    
// Test multiplication.
res = f1 * f2;
f1.print();
std::cout << " * ";
f2.print();
std::cout << " == ";
res.print();
std::cout << "\n";
    
// Test division.
res = f1 / f2;
f1.print();
std::cout << " \ ";
f2.print();
std::cout << " == ";
res.print();
std::cout << "\n";
    
std::cout << "Press Enter or Return to continue.";
std::cin.get();
return 0;
}

When I compile the program I get this warning or error message:
179:14 C:\Dev-Cpp\rational.cpp [Warning] '\040' Anybody know why I'm getting it or a way around it. Many thanks in advance!!

Because the \ is the character used to escape things like quotes, it's not appropriate to use it on its own.

Use

std::cout << " \\ ";

instead.

Thanks. I'm not getting that warning now and it's compileing. But whenever I run it it says rational.exe has encountered a problem and needs to close. We are sorry for the inconvenience. Any ideas why it's doing this? Thanks again!!

Line 139.

denominator /= b;

should of course be

denominator /= a;

Thanks!! It's working. Just wish I understood the code now lol. I'm not the best at math.. which probably isn't a good thing when you want to learn how to program!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.