/Fraction

A fraction class is overloaded with some operators

Primary LanguageC++

Editor Compiler Static Badge

Fraction

This library supports the calculation of a fraction

NOTE: Fraction supports from C++20 to latest

What can the fraction class do

The fraction class is overloaded with the following operators

  • Move/Copy-assignment operator
  • Input/Output operators
  • Comparison operators (==, !=, >, <, >=, <=)
  • Binary arithmetic operators (+, -, *, /, +=, -=, *=, /=)

It can perform calculations with other Fractions/Integers

How to Install and Run the Project

Open terminal with Visual Studio Code and run the command line below

mingw32-make run

Sample code

Binary arithmetic operators

#include "fraction.hpp"

int main() {
    Fraction result, first(12, 9), second(2, 7), third(3, 4);
    result = first + second + 2;
    result *= 3;
    result -= 4 / third;
    std::cout << result;
    return 0;
}

Comparison operators

#include "fraction.hpp"

int main() {
    Fraction first(42, 9), second(2, 23);
    std::cout << (first == second ? "equal" : "not equal") << std::endl;
    std::cout << (second < 1 ? "less than" : "not less than") << std::endl;
    std::cout << (5 > first ? "greater than" : "not greater than") << std::endl;
    return 0;
}

Typecast operator

#include "fraction.hpp"

int main() {
    Fraction first(5, 2);
    double number = 3.6;
    number += static_cast<double>(first);
    // Or: number += first.to_double();
    std::cout << number;
    return 0;
}

Full sample code in main