I am working on a project and I have an idea on how to start. Basically this is how the program runs:
$ ./rulerbuddy 2.25
2.25 is exactly 2 1/4
So, I kind of have the idea that I need first to rip off the whole number which in this case is ‘2’ then start manipulating the fraction to get the result. My question is how can I rip off that whole number from the decimal fraction? Any ideas, steps, guide is appreciated. Thank you.
Take out the whole number and consider only the decimal part (do a
sscanf(input, "%d.%d", &intPart, &fracPart)to take them apart).Count the digits after the decimal point; your starting fraction is digits/10^number of digits, i.e. in your case 25/100;
Now you can simplify it finding the greatest common divisor (e.g. with Euclid algorithm) and dividing both terms by it.
Quick example of how this can be implemented: