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 #include using std::cout; using std::endl; using std::string; class Assert { public: static void isTrue(bool actual) { if (actual) cout << "OK" << endl; else cout << "FAIL" << endl; } static void isFalse(bool actual) { isTrue(!actual); } static void equals(int expected, int actual) { isTrue (expected == actual); } static void equals(string expected, string actual) { isTrue (expected.compare(actual) == 0); } template static void equals(const T& expected, const T& actual) { isTrue(expected == actual); } }; #endif

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 #include #include "Assert.h" #include "Rational.h" using namespace std; int main() { Rational a(1, 3); Rational b(100, 1); Rational x(0, 0); cout << "Handles zero over anything as 0 over 1 "; Assert::equals(x, a * x); Rational y(99, 33); cout << "Normalizes Rational(99,33) in the constructor. "; Assert::equals(Rational(3,1), y); cout << a << " * " << b << " = " << a * b << endl; Assert::equals(Rational(100, 3), a * b); a = Rational(3, 7); b = Rational(1, 7); cout << a << " / " << b << " = " << a / b << endl; Assert::equals(Rational(3, 1), a / b); a = Rational(1, 3); b = Rational(2, 5); Assert::isTrue(a != b); Assert::isFalse(a == b); Rational c = a + b; cout << a << " + " << b << " = " << c << endl; Assert::equals(Rational(11, 15), c); a = Rational(2, 3); b = Rational(1, 6); cout << a << " - " << b << " = " << a - b << endl; Assert::equals(Rational(1, 2), a - b); Assert::equals(Rational(0, 1), a - a); a = b = c; cout << a << " = " << b << " = " << c << endl; Assert::equals(Rational(11, 15), a); Assert::isTrue(b == c); double dValue = a; cout << fixed << setprecision(11) << dValue << endl; return 0; }

Rational.cpp

// Rational.cpp

#include #include #include "Rational.h"

// 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(num) / static_cast(den); }

// 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!