converting a float to a fraction.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 32
Reputation: dmmckelv is an unknown quantity at this point 
Solved Threads: 0
dmmckelv dmmckelv is offline Offline
Light Poster

converting a float to a fraction.

 
0
  #1
Nov 21st, 2006
Can anyone help me get started on converting a float to a fraction?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: converting a float to a fraction.

 
0
  #2
Nov 21st, 2006
Be more specific, state your context and post your latest code attempt. Being vague wont help you at all.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 134
Reputation: mathematician is an unknown quantity at this point 
Solved Threads: 3
mathematician mathematician is offline Offline
Junior Poster

Re: converting a float to a fraction.

 
0
  #3
Nov 24th, 2006
Originally Posted by dmmckelv View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 5
Reputation: zouyu1983 is an unknown quantity at this point 
Solved Threads: 0
zouyu1983 zouyu1983 is offline Offline
Newbie Poster

Re: converting a float to a fraction.

 
0
  #4
Nov 25th, 2006
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC