SlideShare a Scribd company logo
I need help getting my code to work in Mobaxterm in C++ with only two files being edited, both
under the name of Rational and I am trying to fix my one error. Please have a snapshot of your
output in Mobaxterm. I want the output to be exactly the same:
Output:
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
Code: That needs to be edited and fixed.
Rational.cpp
#include "Rational.h"
#include
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;
}
int Rational::getGCD(int a, int b) {
// operands must be positive
if (a < 0)
a = -a;
// in case you are wondering, writing a = (a < 0) ? -a : a;
// uses 1 less line of code, but is more than 2x slower.
// using a = abs(a); has similar performance to (a < ) ? -a : a
if (b < 0)
b = -b;
while (a != b) {
if (a > b)
a -= b; // a = a - b;
else
b -= a; // b = b - a;
}
return a;
}
void Rational::normalize() {
// zero is a special case
if (num == 0) {
den = 1;
} else {
// divide out the greatest common divisor
int gcd = getGCD(num, den);
if (gcd > 1) {
num /= gcd;
den /= gcd;
}
// make the denominator positive
if (den < 0) {
den = -den;
num = -num;
}
}
}
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);
}
Rational Rational::operator=(const Rational& other) {
num = other.num;
den = other.den;
}
bool Rational::operator==(const Rational& other) const {
return num == other.num && den == other.den;
}
bool Rational::operator!=(const Rational& other) const {
return !(*this == other);
}
std::ostream& operator<<(std::ostream& os, const Rational& rhs) {
os << "(" << rhs.num << "," << rhs.den << ")";
}
//Rational::operator double() {
// return num/den;
//}
Rational.h
// Rational.h
#ifndef RATIONAL_H
#define RATIONAL_H
#include
#include
class Rational {
public:
// Constructors
Rational(int n= 0, int d = 1);
int getNum() const {
return num;
}
int getden() const {
return den;
}
void normalize();
// 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;
Rational operator=(const Rational& other);
bool operator==(const Rational& other) const;
bool operator!=(const Rational& other) const;
friend std::ostream& operator<<(std::ostream& os, const Rational& rhs);
operator double () const {
return static_cast (num/den);
}
int getGCD(int a, int b);
private:
int num;
int den;
};
#endif
The other two code files can't be edited.
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 1n";
Assert::equals(x, a * x);
Rational y(99, 33);
cout << "Normalizes Rational(99,33) in the constructor.n";
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;
}
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

More Related Content

Similar to I need help getting my code to work in Mobaxterm in C++ with only tw.pdf

Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
LeahRachael
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
ssuser454af01
 
Hey, looking to do the following with this programFill in the multip.pdf
Hey, looking to do the following with this programFill in the multip.pdfHey, looking to do the following with this programFill in the multip.pdf
Hey, looking to do the following with this programFill in the multip.pdf
rupeshmehta151
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 

Similar to I need help getting my code to work in Mobaxterm in C++ with only tw.pdf (20)

Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
 
Cpl
CplCpl
Cpl
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
lecture8_Cuong.ppt
lecture8_Cuong.pptlecture8_Cuong.ppt
lecture8_Cuong.ppt
 
C Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptC Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.ppt
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
Lecture17
Lecture17Lecture17
Lecture17
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docx
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Hey, looking to do the following with this programFill in the multip.pdf
Hey, looking to do the following with this programFill in the multip.pdfHey, looking to do the following with this programFill in the multip.pdf
Hey, looking to do the following with this programFill in the multip.pdf
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
R code for data manipulation
R code for data manipulationR code for data manipulation
R code for data manipulation
 
Overview Of Using Calculator
Overview Of Using CalculatorOverview Of Using Calculator
Overview Of Using Calculator
 

More from allurafashions98

Data table Requirements 1. Compute the product cost per meal produced.pdf
 Data table Requirements 1. Compute the product cost per meal produced.pdf Data table Requirements 1. Compute the product cost per meal produced.pdf
Data table Requirements 1. Compute the product cost per meal produced.pdf
allurafashions98
 
Danny Anderson admired his wifes success at selling scarves at local.pdf
 Danny Anderson admired his wifes success at selling scarves at local.pdf Danny Anderson admired his wifes success at selling scarves at local.pdf
Danny Anderson admired his wifes success at selling scarves at local.pdf
allurafashions98
 
Daring the financial crisis an the end of the firs decade of the 2000.pdf
 Daring the financial crisis an the end of the firs decade of the 2000.pdf Daring the financial crisis an the end of the firs decade of the 2000.pdf
Daring the financial crisis an the end of the firs decade of the 2000.pdf
allurafashions98
 

More from allurafashions98 (20)

Current Attempt in Progress. At December 31, 2024, Culiumber Imports .pdf
 Current Attempt in Progress. At December 31, 2024, Culiumber Imports .pdf Current Attempt in Progress. At December 31, 2024, Culiumber Imports .pdf
Current Attempt in Progress. At December 31, 2024, Culiumber Imports .pdf
 
Current Attempt in Progress Waterway Company uses a periodic inventor.pdf
 Current Attempt in Progress Waterway Company uses a periodic inventor.pdf Current Attempt in Progress Waterway Company uses a periodic inventor.pdf
Current Attempt in Progress Waterway Company uses a periodic inventor.pdf
 
Current Attempt in Progress The comparative balance.pdf
 Current Attempt in Progress The comparative balance.pdf Current Attempt in Progress The comparative balance.pdf
Current Attempt in Progress The comparative balance.pdf
 
Current Attempt in Progress Novaks Market recorded the following eve.pdf
 Current Attempt in Progress Novaks Market recorded the following eve.pdf Current Attempt in Progress Novaks Market recorded the following eve.pdf
Current Attempt in Progress Novaks Market recorded the following eve.pdf
 
Current Attempt in Progress Ivanhoe Corporation reported the followin.pdf
 Current Attempt in Progress Ivanhoe Corporation reported the followin.pdf Current Attempt in Progress Ivanhoe Corporation reported the followin.pdf
Current Attempt in Progress Ivanhoe Corporation reported the followin.pdf
 
Define a class called Disk with two member variables, an int called.pdf
 Define a class called Disk with two member variables, an int called.pdf Define a class called Disk with two member variables, an int called.pdf
Define a class called Disk with two member variables, an int called.pdf
 
Define to be the median of the Exponential () distribution. That is,.pdf
 Define  to be the median of the Exponential () distribution. That is,.pdf Define  to be the median of the Exponential () distribution. That is,.pdf
Define to be the median of the Exponential () distribution. That is,.pdf
 
Dedewine the bridthenes tar this test Choose the conist answet below .pdf
 Dedewine the bridthenes tar this test Choose the conist answet below .pdf Dedewine the bridthenes tar this test Choose the conist answet below .pdf
Dedewine the bridthenes tar this test Choose the conist answet below .pdf
 
Davenport Incorporated has two divisions, Howard and Jones. The f.pdf
 Davenport Incorporated has two divisions, Howard and Jones. The f.pdf Davenport Incorporated has two divisions, Howard and Jones. The f.pdf
Davenport Incorporated has two divisions, Howard and Jones. The f.pdf
 
Data tableRequirements 1. Prepare the income statement for the mon.pdf
 Data tableRequirements 1. Prepare the income statement for the mon.pdf Data tableRequirements 1. Prepare the income statement for the mon.pdf
Data tableRequirements 1. Prepare the income statement for the mon.pdf
 
Current Attempt in Progress Items from Oriole Companys budget for Ma.pdf
 Current Attempt in Progress Items from Oriole Companys budget for Ma.pdf Current Attempt in Progress Items from Oriole Companys budget for Ma.pdf
Current Attempt in Progress Items from Oriole Companys budget for Ma.pdf
 
Data tableAfter researching the competitors of EJH Enterprises, yo.pdf
 Data tableAfter researching the competitors of EJH Enterprises, yo.pdf Data tableAfter researching the competitors of EJH Enterprises, yo.pdf
Data tableAfter researching the competitors of EJH Enterprises, yo.pdf
 
Current Attempt in Progress If a qualitative variable has c categ.pdf
 Current Attempt in Progress If a qualitative variable has  c  categ.pdf Current Attempt in Progress If a qualitative variable has  c  categ.pdf
Current Attempt in Progress If a qualitative variable has c categ.pdf
 
Data tableData tableThe figures to the right show the BOMs for .pdf
 Data tableData tableThe figures to the right show the BOMs for .pdf Data tableData tableThe figures to the right show the BOMs for .pdf
Data tableData tableThe figures to the right show the BOMs for .pdf
 
Data table Requirements 1. Compute the product cost per meal produced.pdf
 Data table Requirements 1. Compute the product cost per meal produced.pdf Data table Requirements 1. Compute the product cost per meal produced.pdf
Data table Requirements 1. Compute the product cost per meal produced.pdf
 
Darla, Ellen, and Frank have capital balances of $30,000,$40,000 and .pdf
 Darla, Ellen, and Frank have capital balances of $30,000,$40,000 and .pdf Darla, Ellen, and Frank have capital balances of $30,000,$40,000 and .pdf
Darla, Ellen, and Frank have capital balances of $30,000,$40,000 and .pdf
 
Daniel, age 38 , is single and has the following income and exnencac .pdf
 Daniel, age 38 , is single and has the following income and exnencac .pdf Daniel, age 38 , is single and has the following income and exnencac .pdf
Daniel, age 38 , is single and has the following income and exnencac .pdf
 
Danny Anderson admired his wifes success at selling scarves at local.pdf
 Danny Anderson admired his wifes success at selling scarves at local.pdf Danny Anderson admired his wifes success at selling scarves at local.pdf
Danny Anderson admired his wifes success at selling scarves at local.pdf
 
CX Enterprises has the following expected dividends $1.05 in one yea.pdf
 CX Enterprises has the following expected dividends $1.05 in one yea.pdf CX Enterprises has the following expected dividends $1.05 in one yea.pdf
CX Enterprises has the following expected dividends $1.05 in one yea.pdf
 
Daring the financial crisis an the end of the firs decade of the 2000.pdf
 Daring the financial crisis an the end of the firs decade of the 2000.pdf Daring the financial crisis an the end of the firs decade of the 2000.pdf
Daring the financial crisis an the end of the firs decade of the 2000.pdf
 

Recently uploaded

IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
17thcssbs2
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
ashishpaul799
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 

I need help getting my code to work in Mobaxterm in C++ with only tw.pdf

  • 1. I need help getting my code to work in Mobaxterm in C++ with only two files being edited, both under the name of Rational and I am trying to fix my one error. Please have a snapshot of your output in Mobaxterm. I want the output to be exactly the same: Output: 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 Code: That needs to be edited and fixed. Rational.cpp #include "Rational.h" #include 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;
  • 2. } if (d < 0) { n = -n; d = -d; } int divisor = gcd(abs(n), d); num = n / divisor; den = d / divisor; } int Rational::getGCD(int a, int b) { // operands must be positive if (a < 0) a = -a; // in case you are wondering, writing a = (a < 0) ? -a : a; // uses 1 less line of code, but is more than 2x slower. // using a = abs(a); has similar performance to (a < ) ? -a : a if (b < 0) b = -b; while (a != b) { if (a > b) a -= b; // a = a - b; else b -= a; // b = b - a; } return a; } void Rational::normalize() { // zero is a special case if (num == 0) { den = 1; } else { // divide out the greatest common divisor int gcd = getGCD(num, den); if (gcd > 1) { num /= gcd;
  • 3. den /= gcd; } // make the denominator positive if (den < 0) { den = -den; num = -num; } } } 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); } Rational Rational::operator=(const Rational& other) { num = other.num; den = other.den; } bool Rational::operator==(const Rational& other) const { return num == other.num && den == other.den; } bool Rational::operator!=(const Rational& other) const { return !(*this == other); } std::ostream& operator<<(std::ostream& os, const Rational& rhs) { os << "(" << rhs.num << "," << rhs.den << ")"; } //Rational::operator double() { // return num/den;
  • 4. //} Rational.h // Rational.h #ifndef RATIONAL_H #define RATIONAL_H #include #include class Rational { public: // Constructors Rational(int n= 0, int d = 1); int getNum() const { return num; } int getden() const { return den; } void normalize(); // 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; Rational operator=(const Rational& other); bool operator==(const Rational& other) const; bool operator!=(const Rational& other) const; friend std::ostream& operator<<(std::ostream& os, const Rational& rhs); operator double () const { return static_cast (num/den); } int getGCD(int a, int b); private: int num; int den; }; #endif
  • 5. The other two code files can't be edited. 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 1n"; Assert::equals(x, a * x); Rational y(99, 33); cout << "Normalizes Rational(99,33) in the constructor.n"; 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;
  • 6. Assert::equals(Rational(11, 15), a); Assert::isTrue(b == c); double dValue = a; cout << fixed << setprecision(11) << dValue << endl; return 0; } 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); } };