I really need your help..well we have a project and it is called Rational Class.

Create a class called Rational for performing arithmetic with fractions. write a program to test your class.

Use integer variables to represent the private data of the class -- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should the fraction in reduced form. Provide public member functions that perform each of the following tasks:

a) Adding two Rational numbers. The result should be stored in reduced form.
b) Subtracting two Rational numbers. The result should be stored in reduced form.
c) Multiplying two Rational numbers. The result should be stored in reduced form.
d) Dividing two Rational numbers. The result should be stored in reduced form.
e) Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator.
f) Printing Rational numbers in floating-point format.


please help me... i'm still not good in making codes... Thank you..

Dani AI

Generated

For (and noting 's advice to try an implementation first): a compact, safe approach is to store numerator and denominator as integers, enforce a nonzero denominator, keep the sign in the numerator, and always reduce to lowest terms. The implementation below shows one clear way to do that, plus simple operator overloads for +, -, *, /, a conversion to floating point, and an ostream printer. The constructor normalizes immediately (handles zero numerator and negative denominators) and division checks for divide-by-zero.

Key details before the code: normalize (reduce) via a gcd routine; if numerator is zero set denominator to 1; keep denominator positive; throw an exception on an invalid denominator; be aware of integer overflow in intermediate multiplications — switch to wider integer types or reduce crosswise if large inputs are expected.

#include <iostream>
#include <stdexcept>
#include <cstdlib>

class Rational {
private:
    int num, den;
    static int gcd(int a, int b) {
        a = std::abs(a); b = std::abs(b);
        if (a == 0) return b;
        while (b) { int t = a % b; a = b; b = t; }
        return a;
    }
    void normalize() {
        if (den == 0) throw std::invalid_argument("Denominator cannot be zero");
        if (den < 0) { num = -num; den = -den; }
        if (num == 0) { den = 1; return; }
        int g = gcd(num, den);
        num /= g; den /= g;
    }
public:
    Rational(int n = 0, int d = 1) : num(n), den(d) { normalize(); }
    double toDouble() const { return static_cast<double>(num)/den; }
    Rational operator+(const Rational& o) const { return Rational(num*o.den + o.num*den, den*o.den); }
    Rational operator-(const Rational& o) const { return Rational(num*o.den - o.num*den, den*o.den); }
    Rational operator*(const Rational& o) const { return Rational(num*o.num, den*o.den); }
    Rational operator/(const Rational& o) const {
        if (o.num == 0) throw std::invalid_argument("Division by zero");
        return Rational(num*o.den, den*o.num);
    }
    friend std::ostream& operator<<(std::ostream& os, const Rational& r) { return os << r.num << '/' << r.den; }
};

int main() {
    Rational a(2,3), b(4,5);
    std::cout << "a="<<a<<" b="<<b<<"\n";
    std::cout << "a+b="<<(a+b)<<" ("<<(a+b).toDouble()<<")\n";
    return 0;
}

Troubleshooting and tests: verify behavior with negative inputs, zero numerator, and attempts to create a 0 denominator. Add unit tests for chained operations (e.g., (a/b)*c) and large numerators to reveal overflow; if overflow arises, switch fields and intermediate arithmetic to long long or use reduction-before-multiplication. If a simpler interface is needed, add operator==, operator!=, and getters for numerator/denominator.

Recommended Answers

All 2 Replies

Try, at least. Then come back with an attempt and we'll help from there. We'll explain your mistakes, suggest how to fix and then you'll have a working solution.

ok i'll try my very best.. thanks

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.