In a computer, using a float or double to store decimals is not an exact value. If you want to get accurate results, it's best to use fractional notation to represent decimals. Finite decimals or infinite loop decimals can be converted to fractions. Like what:
0.9 = 9/10
0.333 (3) = 1/3 (the number in parentheses indicates a circular section)
Of course, a decimal number can be expressed in several different fractional forms. Such as:
0.333 (3) = 1/3 = 3/9
Given a finite decimal or infinite loop decimal, can you return this fraction in the fraction of the lowest denominator? If the input is repeating decimal, the loop bracket is marked. Here are some possible input data, such as 0.3, 0.30, 0.3 (000), 0.3333 (3333) 、......
Analysis and Solution
(1) Limited decimal conditions
such as: x=0.a1a2...an
Can get 10n*x=a1a2...an
then x = a1a2...an /10n
(2) Starting from the first position to cycle the situation
such as y=0. (B1B2...BM)
then 10M*Y=B1B2...BM. (B1B2...BM) =B1B2...BM +y
then Y=B1B2...BM /(10m-1)
(3) Repeating decimal z=0.a1a2...an(B1B2...BM) under normal circumstances
10n*z=a1a2...an +Y=A1A2...AN+B1B2...BM /(10m-1)
z=[(10m-1) A1A2...AN+B1B2...BM/[(10m-1) 10n]
Finally, the results can be obtained by numerator.
A/b = (A/(GCD (A, B))/(/(GCD (A, b)))
The reference code is as follows:
1#include <iostream>2#include <string>3#include <algorithm>4 using namespacestd;5 6 intgcdintAintb)7 {8 return(b = =0) ? A:GCD (b, a%b); 9 }Ten One intTenpow (intN) A { - intRET =1; - for(inti =0; I < n; i++) the { -RET *=Ten; - } - returnret; + } - + stringInttostring (intnum) A { at stringret; - if(num = =0) num + ='0'; - while(num) - { -RET + = num%Ten+'0'; -Num/=Ten; in } - Reverse (Ret.begin (), Ret.end ()); to returnret; + } - the classFraction * { $ Private:Panax Notoginseng intIntpart;//Integer Part - intDecpart;//Pre-circulation section of the decimal part the intCntdec;//number of bits in the first fractional part of the cycle section + BOOLISCYC;//whether there is a cyclic section A intCycpart;//Cycle Section section the intCNTCYC;//number of bits in the loop section + - Public: $Fraction ()//default constructor $ { -Intpart =0; -Decpart =0; theCntdec =0; -ISCYC =false; WuyiCycpart =0; theCNTCYC =0; - } Wu -Fraction (stringstr) About { $Intpart =0; -Decpart =0; -Cntdec =0; -ISCYC =false; ACycpart =0; +CNTCYC =0; the inti =0, Len =str.size (); - while(I < len && Str[i]! ='.')//Calculate integer Part $ { theIntpart = Intpart *Ten+ Str[i]-'0'; thei++; the } the if(I < Len)//small number of parts exist - { ini++; the while(I < len && Str[i]! ='(')//calculate the front part of the Loop body the { AboutDecpart = Decpart *Ten+ Str[i]-'0'; thei++; theCntdec++; the } + if(I < Len)//Circulating body exists - { thei++; BayiISCYC =true; the while(I < len && Str[i]! =')') the { -Cycpart = Cycpart *Ten+ Str[i]-'0'; -i++; theCntcyc++; the } the } the } - } the the stringtoString () the {94 stringret; the intNumerator =0, denominator =0; the if(ISCYC) the {98Denominator = Tenpow (cntdec) * (Tenpow (CNTCYC)-1); AboutNumerator = Decpart * (Tenpow (CNTCYC)-1) +Cycpart; - }101 Else 102 {103Denominator =Tenpow (CNTDEC);104Numerator =Decpart; the }106 intg = gcd (numerator, denominator);//Numerator107Numerator/=G;108Denominator/=G;109Numerator + = denominator *Intpart; theret = inttostring (numerator) +" / "+inttostring (denominator);111 returnret; the }113 }; the the intMainintargcChar*argv[]) the {117 stringstr;118 while(Cin >>str)119 { - fraction fr (str);121cout << fr.tostring () <<Endl; 122 }123 return 0; 124}View Code
"The beauty of programming" accurately expresses floating-point numbers