Can anyone help me get started on converting a float to a fraction?

0

Be more specific, state your context and post your latest code attempt. Being vague wont help you at all.

0

Can anyone help me get started on converting a float to a fraction?

In theory all you've got to do is use the decimal as the numerator, and then count up the number of digits in that numerator before putting a 1, followed by the same number of zeros on the bottom. So 0.25 becomes 25/100, for example. If you wanted to reduce it further than that, you would have to go round a loop looking for the largest common factor; starting, in this case with 25. Also in this case, you would only have to go round it once.

0

```
#include <iostream>
using namespace std;
struct Fraction
{
int numerator,
denominator;
};
int FindCommonFactor(int m, int n)
{
int x;
while (m % n)
{
x = m % n;
m = n;
n = x;
}
return n;
}
void Process(char str[], Fraction fraction[], int &result)
{
int strLen = strlen(str);
const int displace = strLen - 4;
const int usefulLen = displace - 1;
int constDenominator,
constNumerator = 0,
cycleDenominator,
cycleNumerator = 0,
cycleDenominatorAdd = 0,
cycleDenominatorMain,
i,
k = 1;
for (i=1; i<usefulLen; i++)
{
constNumerator += int(str[displace - i] - 48) * k;
k *= 10;
}
cycleDenominatorMain = constDenominator = k;
// cout << cycleDenominatorMain << endl << constNumerator << endl << usefulLen << endl;
k = 1;
int arrayDisplace = 0;
for (i=0; i<usefulLen; i++)
{
cycleNumerator += ( int(str[displace - i] - 48) * k );
cycleDenominatorAdd += ( 9 * k );
// cout << cycleNumerator << endl << cycleDenominatorAdd << endl << cycleDenominatorMain << endl;
cycleDenominator = cycleDenominatorAdd * cycleDenominatorMain;
// cout << cycleNumerator << endl << cycleDenominator << endl;
fraction[arrayDisplace].numerator = cycleDenominatorAdd * constNumerator + cycleNumerator;
fraction[arrayDisplace].denominator = cycleDenominator;
// cout << fraction[arrayDisplace].numerator << endl << fraction[arrayDisplace].denominator << endl;
int maxFactor = FindCommonFactor(fraction[arrayDisplace].denominator, fraction[arrayDisplace].numerator);
fraction[arrayDisplace].denominator /= maxFactor;
fraction[arrayDisplace].numerator /= maxFactor;
arrayDisplace++;
k *= 10;
constNumerator /= 10;
constDenominator /= 10;
cycleDenominatorMain = constDenominator;
}
int sum = fraction[0].denominator;
result = 0;
for (i=0; i<arrayDisplace; i++)
{
if (sum > fraction[i].denominator)
{
sum = fraction[i].denominator;
result = i;
}
}
// cout << i << endl << fraction[i].numerator << "/" << fraction[i].denominator << endl;
return;
}
int main()
{
char str[14];
Fraction fraction[9];
while (cin >> str)
{
if (strlen(str) == 1 && str[0] == '0')
{
break;
}
int result;
Process(str, fraction, result);
cout << fraction[result].numerator << "/" << fraction[result].denominator << endl;
}
return 0;
}
```

i wrote this code when i was a freshman,

it coded for one problem in online judge of fuzhou university

i hope it will help to you

This topic has been dead for over six months. Start a new discussion instead.

Recommended Topics

For some reason the make_great function is not affecting the output of the show_magicians function. I'm trying to alter the list but it's not working.

`# the make_great function is ...`

Please help me about MenuStrip Control used.

I create a project there one parents form and two child form

MenuStripControl-1 is on parents form (main_form)

Menu Item name a) ...