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. I want to have the to have class, the seven member overloaded operators, of *, / , + , - , = ,==, != and two constructors

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

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

class Rational { public: // Constructors Rational(int n, int d); Rational(int n);

// Arithmetic operators Rational operator+(const Rational& other) const; Rational operator-(const Rational& other) const; Rational operator*(const Rational& other) const; Rational operator/(const Rational& other) const; bool operator==(const Rational& other) const; bool operator!=(const Rational& other) const;

private: int num; int den; }; Rational.cpp: The operator!=() function was missing its implementation. Updated the function as follows: // Returns true if this Rational object is not equal to another Rational object bool Rational::operator!=(const Rational& other) const { return !(*this == other); }

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

#include "Rational.h" int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } Rational::Rational(int n, int d) { if (d == 0) { num = 0; den = 1; return; } if (d < 0) { n = -n; d = -d; } int divisor = gcd(abs(n), d); num = n / divisor; den = d / divisor; if (gcd(num, den) != 1) { throw std::invalid_argument("numerator and denominator must have no common factors other than 1"); } } Rational Rational::operator+(const Rational& other) const { return Rational(num * other.den + den * other.num, den * other.den); } Rational Rational::operator-(const Rational& other) const { return Rational(num * other.den - den * other.num, den * other.den); } Rational Rational::operator*(const Rational& other) const { return Rational(num * other.num, den * other.den); } Rational Rational::operator/(const Rational& other) const { return Rational(num * other.den, den * other.num); } bool Rational::operator==(const Rational& other) const { return num == other.num && den == other.den; } bool Rational::operator!=(const Rational& other) const { return !(*this == other); }

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!