| | |
converting a float to a fraction.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 134
Reputation:
Solved Threads: 3
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.
•
•
Join Date: Nov 2006
Posts: 5
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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
Last edited by zouyu1983; Nov 25th, 2006 at 11:35 pm.
![]() |
Similar Threads
- C++, issues with converting a decimal to fraction (C++)
- converting from float 2 integer (C++)
- Need help with a function (C++)
- Parse error, syntax error, Forbids declaration (C++)
Other Threads in the C++ Forum
- Previous Thread: Only pass-by-value in C!
- Next Thread: Re: Tic-Tac-Toe
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






