SlideShare ist ein Scribd-Unternehmen logo
1 von 7
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

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

Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)Thai Pangsakulyanont
 
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
 
lecture8_Cuong.ppt
lecture8_Cuong.pptlecture8_Cuong.ppt
lecture8_Cuong.pptHongV34104
 
C Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptC Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptGAURAVNAUTIYAL19
 
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 NOTESLeahRachael
 
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.pptxssuser3cbb4c
 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docxGEETHAR59
 
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.docxssuser454af01
 
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...Codemotion
 
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 JobyGrejoJoby1
 
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.pdfrupeshmehta151
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
Overview Of Using Calculator
Overview Of Using CalculatorOverview Of Using Calculator
Overview Of Using CalculatorFrancescoPozolo1
 

Ähnlich wie 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
 

Mehr von allurafashions98

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 .pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 
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,.pdfallurafashions98
 
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 .pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 
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 .pdfallurafashions98
 
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.pdfallurafashions98
 
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 .pdfallurafashions98
 
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 .pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 
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.pdfallurafashions98
 

Mehr von 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
 

Kürzlich hochgeladen

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Kürzlich hochgeladen (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

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); } };