maiacore 1.10.1
Music analisys library
fraction.h
1#pragma once
2
3#include <iostream>
4#include <numeric> // Para std::gcd
5#include <sstream> // Para std::ostringstream e std::istringstream
6#include <string>
7
9
15class Fraction {
16 private:
17 int _numerador;
18 int _denominador;
19
23 void simplify() {
24 int gcd = std::gcd(_numerador, _denominador);
25 _numerador /= gcd;
26 _denominador /= gcd;
27 if (_denominador < 0) { // Manter o denominador positivo
28 _numerador = -_numerador;
29 _denominador = -_denominador;
30 }
31 }
32
33 public:
39 Fraction(int num = 1, int denom = 1) : _numerador(num), _denominador(denom) { simplify(); }
40
45 std::string toString() const {
46 std::ostringstream oss;
47 oss << _numerador << "/" << _denominador;
48 return oss.str();
49 }
50
55 float getFloatValue() const {
56 return static_cast<float>(_numerador) / static_cast<float>(_denominador);
57 }
58
64 void setValues(const int numerador, const int denominador) {
65 _numerador = numerador;
66 _denominador = denominador;
67 simplify();
68 }
69
75 Fraction operator+(const Fraction& other) const {
76 int num = _numerador * other._denominador + other._numerador * _denominador;
77 int denom = _denominador * other._denominador;
78 return Fraction(num, denom);
79 }
80
86 Fraction operator-(const Fraction& other) const {
87 int num = _numerador * other._denominador - other._numerador * _denominador;
88 int denom = _denominador * other._denominador;
89 return Fraction(num, denom);
90 }
91
97 Fraction operator*(const Fraction& other) const {
98 int num = _numerador * other._numerador;
99 int denom = _denominador * other._denominador;
100 return Fraction(num, denom);
101 }
102
108 Fraction operator/(const Fraction& other) const {
109 int num = _numerador * other._denominador;
110 int denom = _denominador * other._numerador;
111 return Fraction(num, denom);
112 }
113
119 Fraction& operator+=(const Fraction& other) {
120 _numerador = _numerador * other._denominador + other._numerador * _denominador;
121 _denominador = _denominador * other._denominador;
122 simplify();
123 return *this;
124 }
125
131 Fraction& operator-=(const Fraction& other) {
132 _numerador = _numerador * other._denominador - other._numerador * _denominador;
133 _denominador = _denominador * other._denominador;
134 simplify();
135 return *this;
136 }
137
143 Fraction& operator*=(const Fraction& other) {
144 _numerador *= other._numerador;
145 _denominador *= other._denominador;
146 simplify();
147 return *this;
148 }
149
155 Fraction& operator/=(const Fraction& other) {
156 _numerador *= other._denominador;
157 _denominador *= other._numerador;
158 simplify();
159 return *this;
160 }
161
167 bool operator==(const Fraction& other) const {
168 return _numerador == other._numerador && _denominador == other._denominador;
169 }
170
176 bool operator>(const Fraction& other) const {
177 return _numerador * other._denominador > other._numerador * _denominador;
178 }
179
185 bool operator>=(const Fraction& other) const { return *this > other || *this == other; }
186
192 bool operator<=(const Fraction& other) const { return !(*this > other); }
193
197 void simplifyFraction() { simplify(); }
198
204 static Fraction stringToFraction(const std::string& fractionAsString) {
205 int num, denom;
206 char slash;
207 std::istringstream iss(fractionAsString);
208 iss >> num >> slash >> denom;
209 return Fraction(num, denom);
210 }
211
218 friend std::ostream& operator<<(std::ostream& os, const Fraction& fraction) {
219 os << fraction.toString();
220 return os;
221 }
222
229 friend std::istream& operator>>(std::istream& is, Fraction& fraction) {
230 char slash;
231 is >> fraction._numerador >> slash >> fraction._denominador;
232 fraction.simplify();
233 return is;
234 }
235};
236
237