Not Yet Answered # converting a float to a fraction.

~s.o.s~ 2,560 mathematician zouyu1983 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

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 article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:

```
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
Dim txtB1 As New TextBox
Dim i
For i = ...
```