Steppable 0.0.1
A CAS project written from scratch in C++
Loading...
Searching...
No Matches
fraction.hpp
1/**************************************************************************************************
2 * Copyright (c) 2023-2025 NWSOFT *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy *
5 * of this software and associated documentation files (the "Software"), to deal *
6 * in the Software without restriction, including without limitation the rights *
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8 * copies of the Software, and to permit persons to whom the Software is *
9 * furnished to do so, subject to the following conditions: *
10 * *
11 * The above copyright notice and this permission notice shall be included in all *
12 * copies or substantial portions of the Software. *
13 * *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20 * SOFTWARE. *
21 **************************************************************************************************/
22
30
31#pragma once
32
33#include "number.hpp"
34
35#include <array>
36#include <string>
37
38namespace steppable
39{
45 {
46 private:
48 std::string top;
49
51 std::string bottom;
52
53 public:
59 explicit Fraction(const std::string& top = "1", const std::string& bottom = "1");
60
66 explicit Fraction(const Number& number);
67
72 Fraction();
73
81 std::string present(bool inLine = true);
82
87 [[nodiscard]] std::array<std::string, 2> asArray() const;
88
96 Fraction operator+(const Fraction& rhs) const;
97
105 Fraction operator-(const Fraction& rhs) const;
106
114 Fraction operator*(const Fraction& rhs) const;
115
124 Fraction operator/(const Fraction& rhs) const;
125
132 Fraction operator^(const Number& rhs);
133
141 Fraction& operator+=(const Fraction& rhs);
142
150 Fraction& operator-=(const Fraction& rhs);
151
159 Fraction& operator*=(const Fraction& rhs);
160
169 Fraction& operator/=(const Fraction& rhs);
170
177 Fraction& operator^=(const Number& rhs);
178
185 bool operator==(const Fraction& rhs) const;
186
193 bool operator!=(const Fraction& rhs) const;
194
201 bool operator<(const Fraction& rhs) const;
202
209 bool operator>(const Fraction& rhs) const;
210
217 bool operator<=(const Fraction& rhs) const;
218
225 bool operator>=(const Fraction& rhs) const;
226
231 void reciprocal();
232
239 void simplify();
240 };
241} // namespace steppable
Fraction operator-(const Fraction &rhs) const
Subtracts a fraction from another fraction. This function does it by doing a simple fraction subtract...
Definition fraction.cpp:104
Fraction operator+(const Fraction &rhs) const
Adds two fractions together. This function does it by doing a simple fraction addition and returns th...
Definition fraction.cpp:94
bool operator<=(const Fraction &rhs) const
Compare two fractions for less or equal than.
Definition fraction.cpp:215
Fraction(const std::string &top="1", const std::string &bottom="1")
Initializes a fraction with a specified top component and bottom component.
Definition fraction.cpp:71
Fraction & operator*=(const Fraction &rhs)
Multiplies two fractions together and stores the result in the current one. This function does it by ...
Definition fraction.cpp:158
std::string bottom
The denominator. (Bottom component)
Definition fraction.hpp:51
void reciprocal()
Converts a fraction to its reciprocal. This function converts the fraction to its reciprocal.
Definition fraction.cpp:233
bool operator>=(const Fraction &rhs) const
Compare two fractions for greater or equal than.
Definition fraction.cpp:224
bool operator<(const Fraction &rhs) const
Compares two numbers for greater than.
Definition fraction.cpp:206
Fraction & operator/=(const Fraction &rhs)
Divides a fraction by abother fraction and stores the result in the current one. This function does i...
Definition fraction.cpp:165
std::array< std::string, 2 > asArray() const
Returns the fraction as an array of its top and bottom components.
Definition fraction.cpp:92
std::string present(bool inLine=true)
Returns the fraction as a string. The string is formatted as "top/bottom", and it will automatically ...
Definition fraction.cpp:86
void simplify()
Simplifies a fraction. This function simplifies the fraction by dividing the top and bottom component...
Definition fraction.cpp:239
Fraction operator/(const Fraction &rhs) const
Divides a fraction by abother fraction. This function does it by doing a simple fraction division (mu...
Definition fraction.cpp:124
Fraction & operator-=(const Fraction &rhs)
Subtracts a fraction from another fraction and stores the result in the current one....
Definition fraction.cpp:151
Fraction & operator+=(const Fraction &rhs)
Adds two fractions together and stores the result in the current one. This function does it by doing ...
Definition fraction.cpp:144
Fraction()
Initializes a fraction with no top component and bottom component specified. By default,...
Definition fraction.cpp:69
Fraction operator*(const Fraction &rhs) const
Multiplies two fractions together. This function does it by doing a simple fraction multiplication an...
Definition fraction.cpp:114
Fraction operator^(const Number &rhs)
Raises the fraction to a power.
Definition fraction.cpp:134
bool operator!=(const Fraction &rhs) const
Definition fraction.cpp:188
Fraction & operator^=(const Number &rhs)
Raises the fraction to a power and stores the result in the current one.
Definition fraction.cpp:172
std::string top
The numerator. (Top component)
Definition fraction.hpp:48
bool operator>(const Fraction &rhs) const
Compare two fractions for less than.
Definition fraction.cpp:197
bool operator==(const Fraction &rhs) const
Compares two numbers for equality.
Definition fraction.cpp:179
Represents a number with arbitrary precision. It basically stores the value as a string.
Definition number.hpp:68
The public namespace for the Steppable library.
Definition argParse.cpp:40
Untitled