944,141 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3919
  • C++ RSS
Nov 21st, 2006
0

converting a float to a fraction.

Expand Post »
Can anyone help me get started on converting a float to a fraction?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
dmmckelv is offline Offline
33 posts
since Nov 2006
Nov 21st, 2006
0

Re: converting a float to a fraction.

Be more specific, state your context and post your latest code attempt. Being vague wont help you at all.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 24th, 2006
0

Re: converting a float to a fraction.

Click to Expand / Collapse  Quote originally posted by dmmckelv ...
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.
Reputation Points: 14
Solved Threads: 4
Junior Poster
mathematician is offline Offline
149 posts
since Nov 2006
Nov 25th, 2006
0

Re: converting a float to a fraction.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. struct Fraction
  4. {
  5. int numerator,
  6. denominator;
  7. };
  8. int FindCommonFactor(int m, int n)
  9. {
  10. int x;
  11. while (m % n)
  12. {
  13. x = m % n;
  14. m = n;
  15. n = x;
  16. }
  17. return n;
  18. }
  19. void Process(char str[], Fraction fraction[], int &result)
  20. {
  21. int strLen = strlen(str);
  22. const int displace = strLen - 4;
  23. const int usefulLen = displace - 1;
  24. int constDenominator,
  25. constNumerator = 0,
  26. cycleDenominator,
  27. cycleNumerator = 0,
  28. cycleDenominatorAdd = 0,
  29. cycleDenominatorMain,
  30. i,
  31. k = 1;
  32. for (i=1; i<usefulLen; i++)
  33. {
  34. constNumerator += int(str[displace - i] - 48) * k;
  35. k *= 10;
  36. }
  37. cycleDenominatorMain = constDenominator = k;
  38. // cout << cycleDenominatorMain << endl << constNumerator << endl << usefulLen << endl;
  39. k = 1;
  40. int arrayDisplace = 0;
  41. for (i=0; i<usefulLen; i++)
  42. {
  43. cycleNumerator += ( int(str[displace - i] - 48) * k );
  44.  
  45. cycleDenominatorAdd += ( 9 * k );
  46. // cout << cycleNumerator << endl << cycleDenominatorAdd << endl << cycleDenominatorMain << endl;
  47. cycleDenominator = cycleDenominatorAdd * cycleDenominatorMain;
  48. // cout << cycleNumerator << endl << cycleDenominator << endl;
  49. fraction[arrayDisplace].numerator = cycleDenominatorAdd * constNumerator + cycleNumerator;
  50. fraction[arrayDisplace].denominator = cycleDenominator;
  51. // cout << fraction[arrayDisplace].numerator << endl << fraction[arrayDisplace].denominator << endl;
  52. int maxFactor = FindCommonFactor(fraction[arrayDisplace].denominator, fraction[arrayDisplace].numerator);
  53.  
  54. fraction[arrayDisplace].denominator /= maxFactor;
  55. fraction[arrayDisplace].numerator /= maxFactor;
  56. arrayDisplace++;
  57. k *= 10;
  58. constNumerator /= 10;
  59. constDenominator /= 10;
  60. cycleDenominatorMain = constDenominator;
  61. }
  62. int sum = fraction[0].denominator;
  63. result = 0;
  64. for (i=0; i<arrayDisplace; i++)
  65. {
  66. if (sum > fraction[i].denominator)
  67. {
  68. sum = fraction[i].denominator;
  69. result = i;
  70. }
  71. }
  72. // cout << i << endl << fraction[i].numerator << "/" << fraction[i].denominator << endl;
  73. return;
  74. }
  75. int main()
  76. {
  77. char str[14];
  78. Fraction fraction[9];
  79. while (cin >> str)
  80. {
  81. if (strlen(str) == 1 && str[0] == '0')
  82. {
  83. break;
  84. }
  85. int result;
  86. Process(str, fraction, result);
  87. cout << fraction[result].numerator << "/" << fraction[result].denominator << endl;
  88. }
  89. return 0;
  90. }

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
Last edited by zouyu1983; Nov 25th, 2006 at 11:35 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zouyu1983 is offline Offline
5 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Only pass-by-value in C!
Next Thread in C++ Forum Timeline: Re: Tic-Tac-Toe





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC