[Beauty of programming] 2.6 precise expression of Floating Point Numbers

Source: Internet
Author: User

Question: Give a finite decimal or an infinite repeating decimal number, and use the smallest denominator to accurately express this number with a fraction. Parentheses indicate the cyclic part, for example:

0.3, 0.3 (3), 0.3 (0)

 

Thought: I saw it, dumb. After thinking twice and not thinking about it, I went online to find a round-robin decimal point to convert the score. The result was actually a primary school Olympics question! I'm depressed .....

Take 1.2 (34) as an example.

1.2 (34) * 10 = 12. 34343434...

1.2 (34) * 1000 = 1234. 3434...

1.2 (34) * 1000-1.2 (34) * 10 = 1234-12

990*1.2 (34) = 1222

1.2 (34) = 1222/990

 

Then, we can divide the denominator of the numerator by their maximum appointment.

The code input is a string. You need to determine whether there are loops and loops that are 0.

# Include <stdio. h> # include <string. h> typedef struct fraction {int numerator; int denominator;} fraction; // returns num * 10 ^ powint TENs (INT num, int POW) {for (INT I = POW; i! = 0; I --) {num * = 10;} return num;} // calculate the maximum common approx. Int Mod (INT num1, int num2) {int A, B; A = (num1> num2 )? Num1: num2; B = (num1 <num2 )? Num1: num2; int r = A % B; If (r = 0) return B; else {A = B; B = r; return Mod (, b) ;}} fraction getfractionexperssion (char * In) {int intefer = 0; // integer part int withoutloop = 0; // int withloop = 0; // int powwithoutloop = 0 in the loop part; // the nth decimal point int powofloop = 0 in the non-circular part; // the nth decimal point int Numerator = 0 in the loop part; // molecule int Denominator = 0; // denominator fraction ans; int TMP = 0; int tmppowwithoutloop = 0; int tmppowofloo P = 0; char * tmpin = In; bool bhasloop = false; while (* tmpin! = '\ 0') {If (* tmpin> = '0' & * tmpin <= '9') {TMP = TMP * 10 + (* tmpin) -'0'); tmppowwithoutloop ++; tmppowofloop ++;} else if (* tmpin = '. ') {tmppowwithoutloop = 0;} else if (* tmpin =' (') {powwithoutloop = tmppowwithoutloop; withoutloop = TMP; tmppowofloop = 0; TMP = 0; bhasloop = true;} else if (* tmpin = ') {powofloop = tmppowofloop; withloop = TMP; TMP = 0;} else {printf ("input error !! ");} Tmpin ++;} // If (! Bhasloop) {withoutloop = TMP; powwithoutloop = tmppowwithoutloop;} If (withloop = 0) // itself is not a circular decimal number {// obtain the numerator Numerator = withoutloop; denominator = TENs (1, powwithoutloop); // calculates the maximum common approx. Int GCD = Mod (numerator, denominator); // minimizes the molecular denominator ans of the answer. denominator = Denominator/GCD; ans. numerator = numerator/GCD; printf ("% s = % d/% d \ n", in, ans. numerator, ans. denominator); Return ans;} else // is a loop decimal point {Numerator = TENs (withoutloop, powofloop) + withloop-withoutloop; Denominator = TENs (1, powofloop + powwithoutloop) -Tens (1, powwithoutloop); int GCD = Mod (numerator, denominator); ans. denominator = Denominator/GCD; ans. numerator = numerator/GCD; printf ("% s = % d/% d \ n", in, ans. numerator, ans. denominator); Return ans ;}} int main () {char * in1 = "3.3 (3)"; char * in2 = "2.3 (12 )"; char * in3 = "0.300 (0)"; getfractionexperssion (in1); getfractionexperssion (in2); getfractionexperssion (in3); Return 0 ;}

 

[Beauty of programming] 2.6 precise expression of Floating Point Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.