Skip to content

A fraction class is overloaded with some operators

Notifications You must be signed in to change notification settings

GeMiNiOranGe/Fraction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 be calculated with other Fractions/Integers

How to Install

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

mingw32-make

Full example code

You can read more examples here.

mingw32-make build-examples
.\bin\<example-file-name>_example.exe

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

About

A fraction class is overloaded with some operators

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published