Pat B 1034. Rational number arithmetic (20)

Source: Internet
Author: User

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

1#include <stdio.h>2 intGongLong LongALong Longb)3 {4     Long LongR,min,max;5Min= (a>b)?b:a;6max= (a>b)?a:b;7r=max%min;8      while(R)9     {Tenmax=min; Onemin=R; Ar=max%min; -     } -     returnmin; the } - voidPrintLong LongALong Longb) - { -     Long Longflag=0, C,d,e; +     if(a<0){ -a=-A; +flag=1; A     } at     if(a==0) printf ("0"); -     Else{ -E=Gong (A, b); -A/=e; -B/=e; -c=a/b; ind=a%b; -         if(flag==0){ to             if(d==0) printf ("%lld", c); +             Else{ -                 if(c==0) printf ("%lld/%lld", A, b); the                 Elseprintf"%lld%lld/%lld", c,d,b); *             } $         }Panax Notoginseng         Else{ -             if(d==0) printf ("(-%LLD)", c); the             Else{ +                 if(c==0) printf ("(-%LLD/%LLD)", A, b); A                 Elseprintf"(-%lld%lld/%lld)", c,d,b); the             } +         } -     } $ } $ voidAddLong LongA1,Long LongB1,Long LongA2,Long LongB2) - { - print (A1,B1); theprintf" + "); - print (A2,B2);Wuyiprintf" = "); thePrint (a1*b2+a2*b1,b1*B2); -printf"\ n"); Wu } - voidMinus (Long LongA1,Long LongB1,Long LongA2,Long LongB2) About { $ print (A1,B1); -printf" - "); - print (A2,B2); -printf" = "); APrint (a1*b2-a2*b1,b1*B2); +printf"\ n"); the } - voidMuilt (Long LongA1,Long LongB1,Long LongA2,Long LongB2) $ { the print (A1,B1); theprintf" * "); the print (A2,B2); theprintf" = "); -Print (a1*a2,b1*B2); inprintf"\ n"); the } the voidDivideLong LongA1,Long LongB1,Long LongA2,Long LongB2) About { the print (A1,B1); theprintf" / "); the print (A2,B2); +printf" = "); -     if(a2==0) printf ("inf\n"); the     Else{Bayi         if(a2<0){ thea2=-A2; thea1=-A1; -         } -Print (a1*b2,b1*A2); theprintf"\ n"); the     } the } the intMain () - { the     Long Longa1,b1,a2,b2; the      while(~SCANF ("%lld/%lld%lld/%lld",&a1,&b1,&a2,&B2)) the     {94 Add (A1,B1,A2,B2); the minus (A1,B1,A2,B2); the muilt (A1,B1,A2,B2); the divide (A1,B1,A2,B2);98     } About     return 0; -}

Pat B 1034. 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.