Files
some-cpp-files/Miscellaneous/decimal_to_fraction.cpp
T
2024-03-17 22:30:49 +08:00

32 lines
491 B
C++

#include <bits/stdc++.h>
using namespace std;
int main() {
double decimal;
double temp;
int integer, numerator, denominator;
int d = -1;
int g;
cin >> decimal;
integer = (int)decimal;
temp = decimal - integer;
while ((int)temp % 10 != 0 || (int)temp == 0) {
temp *= 10;
d++;
}
temp /= 10;
g = (int)gcd((int)temp, (int)pow(10, d));
numerator = (int)(temp / g);
denominator = (int)(pow(10, d) / g);
cout << integer << numerator << denominator << endl;
return 0;
}