Question: In C++ for Mobaxterm, I need help fixing the code to remove the errors and reduce the Rational.cpp down, please. Don't edit either Assert.h and
In C++ for Mobaxterm, I need help fixing the code to remove the errors and reduce the Rational.cpp down, please. Don't edit either Assert.h and hw3.cpp. I can only edit the Rational.h and Rational.cpp. Please tell me what was changed and was fixed, and please provide your output in Mobaxterm.
Assert.h
#ifndef ASSERT_H #define ASSERT_H #include
Rational.h
// Rational.h
#ifndef RATIONAL_H #define RATIONAL_H
#include
class Rational { public: // Constructors Rational(int n, int d); Rational(int n);
// Arithmetic operators Rational operator+(const Rational& other) const;
hw3.cpp
#include
Rational.cpp
// Rational.cpp
#include
// Returns the greatest common divisor of two integers using Euclid's algorithm int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
// Rational constructor that normalizes numerator and denominator and enforces safety rules Rational::Rational(int n, int d) { // Handle zero over anything as 0 over 1 if (d == 0) { num = 0; den = 1; return; } // Ensure denominator is positive if (d < 0) { n = -n; d = -d; } // Reduce numerator and denominator by greatest common divisor int divisor = gcd(abs(n), d); num = n / divisor; den = d / divisor; // Ensure numerator and denominator have no common factors other than 1 if (gcd(num, den) != 1) { throw std::invalid_argument("numerator and denominator must have no common factors other than 1"); } }
// Rational constructor from integer Rational::Rational(int n) : num(n), den(1) {}
// Returns a Rational object that is the sum of this and another Rational object Rational Rational::operator+(const Rational& other) const { return Rational(num * other.den + den * other.num, den * other.den); }
// Returns a Rational object that is the difference of this and another Rational object Rational Rational::operator-(const Rational& other) const { return Rational(num * other.den - den * other.num, den * other.den); }
// Returns a Rational object that is the product of this and another Rational object Rational Rational::operator*(const Rational& other) const { return Rational(num * other.num, den * other.den); }
// Returns a Rational object that is the quotient of this and another Rational object Rational Rational::operator/(const Rational& other) const { return Rational(num * other.den, den * other.num); }
// Returns true if this Rational object is equal to another Rational object bool Rational::operator==(const Rational& other) const { return num == other.num && den == other.den; }
// Returns true if this Rational object is not equal to another Rational object bool Rational::operator!=(const Rational& other) const { return !(*this == other); }
// Returns true if this Rational object is less than another Rational object bool Rational::operator<(const Rational& other) const { return num * other.den < den * other.num; }
// Returns a double value that approximates this Rational object Rational::operator double() const { return static_cast
// Overloads the insertion operator to output a Rational object in the form (num/den) std::ostream& operator<<(std::ostream& os, const Rational& r) { os << '(' << r.num << '/' << r.den << ')'; return os; }
The expected output that we should be recieving is:
Handles zero over anything as 0 over 1 OK Normalizes Rational(99,33) in the constructor. OK (1/3) * (100/1) = (100/3) OK (3/7) / (1/7) = (3/1) OK OK OK (1/3) + (2/5) = (11/15) OK (2/3) - (1/6) = (1/2) OK OK (11/15) = (11/15) = (11/15) OK OK 0.73333333333
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
