Hi

Im completely new to programming and was wondering if anyone would be willing to help me with this exercise in C++. I have absolutely no idea how to do this exercise:
-----------------------------------------------

Rational.h is a C++ header file which declares a class (Rational) which handles Rational numbers (i.e. fractions).

You must create a C++ file which contains the definitions for all the methods in that class. Rational.cc is the start of just such a file, which contains one method to get you started. 
Once you have the class methods defined, you can use it with the TestRat.cc test driver program. Using g++ you would perform the following steps: 

1. g++ -c Rational.cc //this creates Rational.o with the class definitions in it 
2. g++ -c TestRat.cc //this creates TestRat.o with the main function in it 
3. g++ Rational.o TestRat.o -o TestRat //this combines the class definitions with the class usage in the main function and creates an executable file called TestRat 

Rational.h is given here:

const int MAXB=20;

class Rational {

private:
int numerator, denominator;
char buffer[MAXB];

static int gcd (int a, int b); //a helpful function that returns the
//Greatest Common 
Denominator of
//two numbers

void normalize(); //Make sure that this fraction is in
//its lowest terms (ie 3/12 -> 1/4)
//Hint: use gcd()

public:
Rational(); //default constructor of a Rational
//number whose numerator and
//denominator are 0/1
Rational(int n, int d); //constructor of a Rational number
//whose numerator and denominator
//are n/d

char *toString(); //turns this number into a string
//using this object's handy buffer

Rational plus(Rational r); // add two rational numbers
Rational minus(Rational r); // subtract two rational numbers
Rational times(Rational r); // multiply two rational numbers
Rational divide(Rational r); // divide two rational numbers

// don't forget that
// a/b + c/d == (ad + bc) / bd
// a/b - c/d == (ad - bc) / bd
// a/b * c/d == ac / bd
// a/b / c/d == ad / bc
};

Rational.cc is given here:

#include <stdio.h>
#include "Rational.h"

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

TestRat.cc is given here:

#include <iostream.h>
#include "Rational.h"

int main(int argc, char *argv[]){
Rational a(2,3);
Rational b(1,2);

Rational res;

res=a.plus(b);
cout << a.toString() << " + " << b.toString() << " = " <<
res.toString() << endl ;

res=a.minus(b);
cout << a.toString() << " - " << b.toString() << " = " <<
res.toString() << endl ;

res=a.times(b);
cout << a.toString() << " * " << b.toString() << " = " <<
res.toString() << endl ;

res=a.divide(b);
cout << a.toString() << " / " << b.toString() << " = " <<
res.toString() << endl ;

}

--------------------------------------------------------------------------------
If someone could possibly do the exercise and run my through it, i would be really grateful.

Thanks alot

Dani AI

Generated

A compact, practical implementation and a few notes to fill the missing pieces in Rational.cc so the test driver behaves as expected. supplied the headers and the test values (a=2/3, b=1/2); 's post is off-topic, so the useful path is to implement the remaining methods and handle a few common edge cases.

Key design choices made here:

  • normalize() reduces to lowest terms via the existing gcd() and forces a positive denominator.
  • A zero denominator in input is treated as an invalid fraction and is converted to 0/1 (safer than crashing).
  • toString() writes into the object's buffer[MAXB] via snprintf to avoid overruns; note that each Rational object has its own buffer, so printing different objects is safe.
  • Arithmetic uses 64-bit temporaries to reduce overflow risk; with very large values the user should change the header types (int -> long long) or add overflow checks.
// append these definitions to Rational.cc (gcd already exists in the file)
#include <stdio.h>
#include <stdlib.h>

Rational::Rational() {
    numerator = 0;
    denominator = 1;
    buffer[0] = '\0';
}

Rational::Rational(int n, int d) {
    numerator = n;
    denominator = d;
    normalize();
    buffer[0] = '\0';
}

void Rational::normalize() {
    if (denominator == 0) {            // invalid input: choose safe representation
        numerator = 0;
        denominator = 1;
        return;
    }
    if (denominator < 0) {             // keep denominator positive
        numerator = -numerator;
        denominator = -denominator;
    }
    int g = gcd(abs(numerator), abs(denominator));
    if (g != 0) {
        numerator /= g;
        denominator /= g;
    }
}

char *Rational::toString() {
    snprintf(buffer, MAXB, "%d/%d", numerator, denominator);
    return buffer;
}

Rational Rational::plus(Rational r) {
    Rational res;
    long long n = (long long)numerator * r.denominator + (long long)r.numerator * denominator;
    long long d = (long long)denominator * r.denominator;
    res.numerator = (int)n;
    res.denominator = (int)d;
    res.normalize();
    return res;
}

Rational Rational::minus(Rational r) {
    Rational res;
    long long n = (long long)numerator * r.denominator - (long long)r.numerator * denominator;
    long long d = (long long)denominator * r.denominator;
    res.numerator = (int)n;
    res.denominator = (int)d;
    res.normalize();
    return res;
}

Rational Rational::times(Rational r) {
    Rational res;
    long long n = (long long)numerator * r.numerator;
    long long d = (long long)denominator * r.denominator;
    res.numerator = (int)n;
    res.denominator = (int)d;
    res.normalize();
    return res;
}

Rational Rational::divide(Rational r) {
    Rational res;
    if (r.numerator == 0) {
        fprintf(stderr, "Rational::divide: division by zero -- returning 0/1\n");
        res.numerator = 0;
        res.denominator = 1;
        return res;
    }
    long long n = (long long)numerator * r.denominator;
    long long d = (long long)denominator * r.numerator;
    res.numerator = (int)n;
    res.denominator = (int)d;
    res.normalize();
    return res;
}

Quick troubleshooting and expectations:

  • With a=2/3 and b=1/2 the test output should be:
    2/3 + 1/2 = 7/6
    2/3 - 1/2 = 1/6
    2/3 * 1/2 = 1/3
    2/3 / 1/2 = 4/3
  • If numbers appear truncated or strings garbled, check MAXB (20 may be small for very large ints) and ensure each file is compiled and linked with the implementation.
  • For production use, replace int with a wider integer type or add overflow detection for the intermediate products.

>>If someone could possibly do the exercise and run my through it, i would be really grateful.

I'm sure you would be gradeful. I'll be grateful too if you deposite $10,000.00 USD in my PayPal account. After you do that then I'll gladly email you the 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.