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
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
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
Get step-by-step solutions from verified subject matter experts
