added fractions and RNG::trial
This commit is contained in:
44
src/fraction.cc
Normal file
44
src/fraction.cc
Normal file
@ -0,0 +1,44 @@
|
||||
#include "fraction.h"
|
||||
|
||||
fraction fraction::operator+(const fraction &frac) {
|
||||
fraction tmp = *this;
|
||||
tmp.numerator = tmp.numerator * frac.denominator +
|
||||
tmp.denominator * frac.numerator;
|
||||
tmp.denominator = tmp.denominator * frac.denominator;
|
||||
return tmp.simplify();
|
||||
}
|
||||
|
||||
fraction fraction::operator*(const fraction &frac) {
|
||||
fraction tmp = *this;
|
||||
tmp.numerator = tmp.numerator * frac.numerator;
|
||||
tmp.denominator = tmp.denominator * frac.denominator;
|
||||
return tmp.simplify();
|
||||
}
|
||||
|
||||
bool fraction::operator==(const fraction &frac) {
|
||||
this->simplify();
|
||||
return gcd(frac.numerator, this->numerator) == this->numerator &&
|
||||
gcd(frac.denominator, this->denominator) == this->denominator;
|
||||
}
|
||||
|
||||
bool fraction::operator!=(const fraction &frac) {
|
||||
return !(*this == frac);
|
||||
}
|
||||
|
||||
fraction &fraction::simplify() {
|
||||
int g = gcd(numerator, denominator);
|
||||
numerator /= g;
|
||||
denominator /= g;
|
||||
return *this;
|
||||
}
|
||||
|
||||
float fraction::real() {
|
||||
return (float)numerator / denominator;
|
||||
}
|
||||
|
||||
int fraction::gcd(int a, int b) {
|
||||
if (a % b == 0)
|
||||
return b;
|
||||
|
||||
return gcd(b, a % b);
|
||||
}
|
18
src/fraction.h
Normal file
18
src/fraction.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef __FRACTION_H__
|
||||
#define __FRACTION_H__
|
||||
|
||||
struct fraction final {
|
||||
int numerator;
|
||||
int denominator;
|
||||
|
||||
fraction operator+(const fraction &frac);
|
||||
fraction operator*(const fraction &frac);
|
||||
bool operator==(const fraction &frac);
|
||||
bool operator!=(const fraction &frac);
|
||||
fraction &simplify();
|
||||
float real();
|
||||
private:
|
||||
int gcd(int a, int b);
|
||||
};
|
||||
|
||||
#endif
|
10
src/log.cc
Normal file
10
src/log.cc
Normal file
@ -0,0 +1,10 @@
|
||||
#include "log.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
logger::logger(std::ofstream &&new_out):
|
||||
out{std::move(new_out)} {}
|
||||
|
||||
void logger::render() {
|
||||
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
#define __LOG_H__
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "constants.h"
|
||||
#include "characters.h"
|
||||
@ -10,6 +11,7 @@
|
||||
class logger final {
|
||||
private:
|
||||
std::ofstream out;
|
||||
std::vector<char> display;
|
||||
public:
|
||||
logger(std::ofstream &&new_out);
|
||||
|
||||
|
@ -32,6 +32,10 @@ int RNG::get_curr_rand_num() const {
|
||||
return curr_rand_num;
|
||||
}
|
||||
|
||||
bool RNG::trial(fraction &psuccess) {
|
||||
return (rand() % psuccess.denominator) < psuccess.numerator;
|
||||
}
|
||||
|
||||
template<class T> T &RNG::get_rand_in_vector(const std::vector<T> &vec) {
|
||||
curr_rand_num = rand();
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
|
||||
#include "fraction.h"
|
||||
|
||||
// Technically, you should ONLY define one rng for the entire program
|
||||
// IMPORTANT: pass all RNG objects as references
|
||||
class RNG final {
|
||||
@ -24,6 +26,8 @@ public:
|
||||
unsigned int get_init_seed() const;
|
||||
int get_curr_rand_num() const;
|
||||
|
||||
bool trial(fraction &psuccess);
|
||||
|
||||
template<class T> T &get_rand_in_vector(const std::vector<T> &vec);
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user