Skip to content

Instantly share code, notes, and snippets.

@dvtate
Created May 28, 2016 19:52
Show Gist options
  • Save dvtate/39029134168798b0b43190fbc1469a46 to your computer and use it in GitHub Desktop.
Save dvtate/39029134168798b0b43190fbc1469a46 to your computer and use it in GitHub Desktop.
#include<iostream> // std::cin, std::cout, std::string()
///finds GCF using Euclid's Algorithm ( https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations (converted to C++))
template <class T>
T gcf(T a, T b){
//throw error if numbers aren't whole ( % = remainder: aka 'modulus')
if (a % 1 != 0 || b % 1 != 0) // could be simplified to `if (a % 1 || b % 1)` (as 0 == false)
throw std::string("GCF(): Numbers given weren\'t whole"); //error stops execution
if (b == 0) return a; // could be simplified to `if (!b) return a;` (as 0 == false)
//else
return gcf(b, a % b);
}
///simplifies the given numerator and denominator (reference parameters)
template <class T>
void fracSimplify(T& numerator, T& denominator){
//find the GCF/GCD
T fracGCF = gcf(numerator, denominator);
//divide numerator and denominator by their GCF
numerator /= fracGCF;
denominator /= fracGCF;
}
int main(){
//initialize numerator and denominator variables as 32bit integers
int32_t num, den;
//comand input
std::cout <<"\nEnter Numberator: ";
std::cin >>num;
std::cout <<"\nEnter Denominator: ";
std::cin >>den;
// show input being processed (try putting a string in)
std::cout <<'\n' <<num <<" / " <<den;
//simplify the fraction
fracSimplify(num, den); //the function changes the values of num and den
// anything over 1 is a whole number
if (den == 1)
std::cout <<" = \n" <<num <<'\n';
else
std::cout <<" = \n"<<num <<" / " <<den <<'\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment