PAT1034. Rational number arithmetic (20)

Source: Internet
Author: User
Tags greatest common divisor

The subject asks to write a procedure, calculates 2 rational number's and, difference, product, quotient.

Input format:

The input gives a rational number in the form of "A1/b1 a2/b2" in a row in two fractions, where the numerator and denominator are all integers within the integer range, and the minus sign may appear only in front of the numerator, and the denominator is not 0.

Output format:

In 4 rows, according to the "rational number 1 operator rational number 2 = result" In the format order Output 2 rational number of the and, difference, product, quotient. Note that each rational number of the output must be the simplest form "K A/b" of the rational number, where k is the integer part, A/b is the simplest fraction, and if it is negative, parentheses are required, and if the division denominator is 0, the "Inf" is output. The title guarantees that the correct output does not exceed integers in the integer range.

Input Sample 1:

2/3 -4/2

Output Example 1:

2/3 + (-2) = (-1 1/3) 2/3-(-2) = 2 2/32/3 * (-2) = (-1 1/3) 2/3/(-2) = ( -1/3)

Input Sample 2:

5/3 0/6

Output Example 2:

1 2/3 + 0 = 1 2/31 2/3-0 = 1 2/31 2/3 * 0 = 2/3/0 = INF

Idea: Write the frame on the paper, the programming should be organized, and finally deal with the details
1#include <stdio.h>2 //the minus sign can only appear in front of the molecule3 //finding a good greatest common divisor algorithm can reduce the time4 Long Long intGongyue (Long Long intXLong Long inty)5 {6     if(x<0)7x=-x;8     if(y<0)9y=-y;Ten    Long Long intm = x%y;  One      while(m) { Ax =y;  -y =m;  -m = x%y;  the     }   -     returny;  - } -  + Charsig[4]={ -     '+','-','*','/' + }; A voidPrint (Long Long intA1,Long Long intB1) at { -       Long Long     intgongyue1=Gongyue (A1,B1); -       Long Long     inttemp=a1/B1; -       if(a1==0)//0 -printf"0"); -         Else if(temp==0)//true Score in       { -           if(a1<0) to             { +printf"(%LLD/%LLD)", a1/gongyue1,b1/gongyue1); -             } the           Else *printf"%lld/%lld", a1/gongyue1,b1/gongyue1); $         } Panax Notoginseng       Else  //false Score -       {   the          //int te=a1; +          if(a1<0) Aa1=-A1; thea1=a1/gongyue1; +b1=b1/gongyue1;//There may be a problem??? -           if(temp<0&&a1%b1!=0) $            { $printf"(%lld%lld/%lld)", temp,a1%b1,b1); -            }            -             Else if(temp<0&&a1%b1==0)//divisible the           { -printf"(%LLD)", temp); Wuyi              }  the              Else if(temp>=0&&a1%b1==0) -              { Wuprintf"%lld", temp); -           } About           Else    //not divisible $           { -printf"%lld%lld/%lld", temp,a1%b1,b1);  -           }                -     }        A } + intMainintargcChar*argv[]) the { -     //B1 B2 >0 $     Long Long     inta1,b1,a2,b2; the     Long Long     intc1,c2; thescanf"%lld/%lld%lld/%lld",&a1,&b1,&a2,&B2); the     //and the theC2=b1*B2; -c1=a1*b2+a2*B1; in Print (A1,B1); theprintf"%c", sig[0]); the Print (A2,B2); Aboutprintf" = "); the Print (C1,C2); thePutchar ('\ n'); the     //Poor +C2=b1*B2; -c1=a1*b2-a2*B1; the Print (A1,B1);Bayiprintf"%c", sig[1]); the Print (A2,B2); theprintf" = "); - Print (C1,C2); -Putchar ('\ n'); the     //Product theC1=a1*A2; theC2=b1*B2; the Print (A1,B1); -printf"%c", sig[2]); the Print (A2,B2); theprintf" = "); the Print (C1,C2);94Putchar ('\ n'); the     //Business the Print (A1,B1); theprintf"%c", sig[3]);98 Print (A2,B2); Aboutprintf" = "); -     if(a2!=0)101     {102         //need to guarantee c2>0103C1=a1*B2;104c2=a2*B1; the         if(c2<0)106         {107c2=-C2;108c1=-C1;109         }  the Print (C1,C2);111Putchar ('\ n'); the     } 113     Else the     { theprintf"inf\n"); the     }117     return 0;118}
View Code

PAT1034. Rational number arithmetic (20)

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.