So, I am Writing this little program in c++, it's made to compute various values with lines (sorry i am french, i don't know how to say it in English, but they are the lines with equation types Y = kx + t). And I want my program to output fractions instead of decimals (2/3 instead of 0.666666666...).
Can anyone tell me how ?
I read online that there are some libraries for that purpose, can anyone help me on how to use them and/or how to implement them in my code ? Thanks :)
#include "pch.h"
#include <iostream>
#include <string>
std::string mainAnswer;
bool endVar = false;
void lineEquationFromTwoPoints() {
mainAnswer.clear();
double Xa = 0;
double Ya = 0;
double Xb = 0;
double Yb = 0;
double Y = 0;
double X = 0;
double k = 0;
double t = 0;
std::cout << ("Enter the Coordinates of your first point in this format x y : ");
std::cin >> Xa >> Ya;
std::cout << ("Enter the Coordinates of your second point in this format x y : ");
std::cin >> Xb >> Yb;
if (Xb != Xa && Yb != Ya) {
k = (Yb - Ya) / (Xb - Xa);
t = -(Xa)*k + Ya;
if (k != 1 && t != 0) {
std::cout << ("Y = ") << k << ("x + ") << t << std::endl;
}
else if (k == 1) {
std::cout << ("Y = ") << ("x") << ("+") << t << std::endl;
}
else if (t == 0) {
std::cout << ("Y = ") << k << ("x") << std::endl;
}
}
else if (Xb == Xa) {
std::cout << ("Coordinates of the first point are Equal");
}
else if (Yb == Ya) {
std::cout << ("Coordinates of the second point are Equal");
}
else if (Xb == Xa && Yb == Ya) {
std::cout << ("Coordinates of both points are Equal");
}
}
void triangle() {
double Xa = 0;
double Ya = 0;
double Xb = 0;
double Yb = 0;
double Xc = 0;
double Yc = 0;
double Ym1 = 0;
double Xm1 = 0;
double km1 = 0;
double tm1 = 0;
double Ym2 = 0;
double Xm2 = 0;
double km2 = 0;
double tm2 = 0;
double Ym3 = 0;
double Xm3 = 0;
double km3 = 0;
double tm3 = 0;
std::cout << ("Work in progress. . . :-)") << std::endl;
}
void Choose() {
while (endVar != true) {
std::cout << ("Lines:") << std::endl;
std::cout << ("------") << std::endl << std::endl;
std::cout << ("Choose What Line Operations do You Want Me To Perform:") << std::endl;
std::cout << ("1.Formulas") << std::endl;
std::cout << ("2.Calculation of a Line's equation from 2 points") << std::endl;
std::cout << ("3.Calculation of all data in a triangle") << std::endl;
std::cout << ("Type Exit to Exit") << std::endl << std::endl;
std::getline(std::cin, mainAnswer);
if (mainAnswer == "exit" || mainAnswer == "Exit") {
std::exit;
endVar = true;
}
else if (mainAnswer == "1") {
std::cout << ("Formulas will be added Here once main program with main calculation functions will be finished") << std::endl;
}
else if (mainAnswer == "2") {
lineEquationFromTwoPoints();
}
else if (mainAnswer == "3") {
triangle();
}
else {
std::cout << ("Unexpected error occured. Please relaunch program.");
std::exit;
}
}
}
int main()
{
Choose();
return 0;
}
A nice way to approximate a float with a fraction is to used continued fractions. In the following code,
eps
is the desired precision.x
is assumed to be strictly positive.Detailed information on continued fractions is available on Wikipedia for example.
If you don't need a high precision or if you assume that the denominators will be small, you can use a brute force approach instead, simply incrementing the denominator
b
.