| | |
Help Me!!!
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Some improvisation:
c Syntax (Toggle Plain Text)
/* It turned up up just at the right moment: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/gcd.html PROGRAM GreatestCommonDivisor IMPLICIT NONE INTEGER :: a, b, c WRITE(*,*) 'Two positive integers please --> ' READ(*,*) a, b IF (a < b) THEN ! since a >= b must be true, they c = a ! are swapped if a < b a = b b = c END IF DO ! now we have a <= b c = MOD(a, b) ! compute c, the reminder IF (c == 0) EXIT ! if c is zero, we are done. GCD = b a = b ! otherwise, b becomes a b = c ! and c becomes b END DO ! go back WRITE(*,*) 'The GCD is ', b END PROGRAM GreatestCommonDivisor */ int GreatestCommonDivisor(int a, int b) { int c; if (a < 0) a = -a; if (b < 0) b = -b; if (a < b) { c = a; a = b; b = c; } if (b == 0) return a; for (;;) { c = a % b; if (c == 0) break; a = b; b = c; } return b; } char *double2fraction(char* buf, int bufsize, double x, int maxdigits) { double intpart; double f; int negative = 0; int i, gcd, numerator, divisor; char temp[64]; int templen; if (!buf || bufsize < 2) return 0; else if (x == 0.0) { buf[0] = '0'; buf[1] = '\0'; } if (x < 0.0) { x = -x; negative = 1; } if (maxdigits <= 0) maxdigits = 5; else if (maxdigits > 9) maxdigits = 9; divisor = 1; for (i = 0; i < maxdigits; ++i) divisor *= 10; f = modf(x,&intpart); numerator = (int)(f*divisor+0.5); if (numerator != 0) { while ((gcd = GreatestCommonDivisor(divisor,numerator)) > 1) { divisor /= gcd; numerator /= gcd; } sprintf(temp,"%s%.0f/%d",(negative?"-":""),intpart*divisor+numerator,divisor); } else sprintf(temp,"%.0f",intpart); templen = strlen(temp); if (templen+1 < bufsize) strcpy(buf,temp); else { buf[0] = '*'; buf[1] = '\0'; } return buf; }
A little thorough version:
c Syntax (Toggle Plain Text)
int greatestCommonDivisor(int a, int b) { int t; if (a < 0) a = -a; if (b < 0) b = -b; if (a < b) { t = a; a = b; b = t; } while (b) { t = b; b = a % b; a = t; } return a; } char *double2fraction(char* buf, int bufsize, double x, int maxdigits) { double intpart; double f; int negative = 0; int i, gcd, numerator, divisor; char temp[64]; int templen; if (!buf || bufsize < 2) return 0; if (x == 0.0) { buf[0] = '0'; buf[1] = '\0'; return buf; } if (x < 0.0) { x = -x; negative = 1; } if (maxdigits <= 0) maxdigits = 5; else if (maxdigits > 9) maxdigits = 9; divisor = 1; for (i = 0; i < maxdigits; ++i) divisor *= 10; f = modf(x,&intpart); numerator = (int)(f*divisor+0.5); if (numerator != 0) { while ((gcd = greatestCommonDivisor(divisor,numerator)) > 1) { divisor /= gcd; numerator /= gcd; } sprintf(temp,"%s%.0f/%d",(negative?"-":""), intpart*divisor+numerator,divisor); } else sprintf(temp,"%.0f",intpart); templen = strlen(temp); if (templen+1 < bufsize) strcpy(buf,temp); else { buf[0] = '*'; buf[1] = '\0'; } return buf; }
The only thing which you need to work around is to find the no of digit after the decimal point. If you can get the no of digits you could pow function to get the denominator and the numerator would be just the original number with no decimal point in it.
ssharish
ssharish
Last edited by ssharish2005; Aug 1st, 2008 at 6:03 pm.
![]() |
Other Threads in the C Forum
- Previous Thread: Problem in trapping in TIC TAC TOE
- Next Thread: Need HELP!!!!!
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h







