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!=.
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;
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;