Fraction Class with Addition Operator
Define a class Rational to represent fractoins using two private integer members: numerator and denominator. The denominator is always positive, witth the sign of the fraction determined solely by the numerator. Implement operator overloading for addition (+) to sum two fractions and return the result in its simplest form.
Input Format
- The first line contains two integers: the numerator and denominator of the first fraction (denominator ≠ 0).
- The second line contains two integers: the numerator and denominator of the second fraction.
Output Format
Print the resulting numerator and, if the denominator is not 1, the denominator. If the result is a whole number, only print the numerator.
Sample Input
1 5
2 5
Sample Output
3 5
Implementation
#include <iostream>
#include <numeric> // for std::gcd
class Rational {
private:
int numerator;
int denominator;
// Helper to reduce the fraction to lowest terms
void reduce() {
int divisor = std::gcd(std::abs(numerator), denominator);
numerator /= divisor;
denominator /= divisor;
}
public:
Rational(int num = 0, int den = 1) : numerator(num), denominator(den) {
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
reduce();
}
// Overload + operator
Rational operator+(const Rational& other) const {
int common_denom = denominator * other.denominator;
int new_numerator = numerator * other.denominator + other.numerator * denominator;
return Rational(new_numerator, common_denom);
}
// Display result
void display() const {
std::cout << numerator;
if (denominator != 1) {
std::cout << " " << denominator;
}
}
};
int main() {
int a, b, c, d;
std::cin >> a >> b >> c >> d;
Rational frac1(a, b);
Rational frac2(c, d);
Rational result = frac1 + frac2;
result.display();
return 0;
}
Complex Class with Modulus Comparison
Create a Complex class with private data members for real and imaginary parts. Overload the greater-than operator (>) to compare the magnitudes (moduli) of two complex numbers.
Input Format
Each test case consists of four floating-point numbers on a single line:
- First two: real and imaginary parts of the first complex number.
- Last two: real and imaginary parts of the second complex number.
Input ends when all four values are zero (0 0 0 0), which should not be processed.
Output Format
For each test case, output true if the modulus of the first complex number is strict greater than that of the second; otherwise, output false.
Sample Input
3 5 4 0
0 3 4 1
0 0 0 0
Sample Output
true
false
Implementation
#include <iostream>
#include <cmath>
class Complex {
private:
double realPart;
double imagPart;
public:
Complex(double r = 0.0, double i = 0.0) : realPart(r), imagPart(i) {}
// Compute modulus (magnitude)
double magnitude() const {
return std::sqrt(realPart * realPart + imagPart * imagPart);
}
// Overload > to compare magnitudes
friend bool operator>(const Complex& c1, const Complex& c2) {
return c1.magnitude() > c2.magnitude();
}
};
int main() {
double r1, i1, r2, i2;
while (std::cin >> r1 >> i1 >> r2 >> i2) {
if (r1 == 0 && i1 == 0 && r2 == 0 && i2 == 0) break;
Complex c1(r1, i1);
Complex c2(r2, i2);
if (c1 > c2) {
std::cout << "true" << std::endl;
} else {
std::cout << "false" << std::endl;
}
}
return 0;
}