pat-basic-1034-Rational Number arithmetic

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

This problem WA for a long time, stuck in ll here ...
The general idea is to-pass after the offer and then turn the number after numerator into a true fraction form.
//ll WA#include <bits/stdc++.h>#defineLL Long Longusing namespacestd;voidCONVERT (ll A, ll b);intMain () {LL A1, B1, A2, B2; scanf ("%lld/%lld%lld/%lld", &AMP;A1, &AMP;B1, &AMP;A2, &B2); //+CONVERT (A1, B1); printf (" + ");    Convert (A2, B2); printf (" = "); CONVERT (A1*B2+A2*B1, b1*B2); printf ("\ n"); //-CONVERT (A1, B1); printf (" - ");    Convert (A2, B2); printf (" = "); CONVERT (A1*B2-A2*B1, b1*B2); printf ("\ n"); //*CONVERT (A1, B1); printf (" * ");    Convert (A2, B2); printf (" = "); CONVERT (A1*A2, b1*B2); printf ("\ n"); // /CONVERT (A1, B1); printf (" / ");    Convert (A2, B2); printf (" = "); if(A2 = =0) {printf ("inf\n"); }    Else{CONVERT (A1*B2, a2*B1); printf ("\ n"); }    return 0;}voidCONVERT (ll A, ll b) {if(A >0&& B <0) {a= -A; b= -b; }    Else if(A <0&& B <0) {a= -A; b= -b; }    //A = B*k    if(a% b = =0){        if(A <0) {printf ("(%LLD)", A/b); }        Else{printf ("%lld", A/b); }        return; }    //otherwise//Simplify     for(inti =2; I <= sqrt (max (b)); ++i) {         while(a% i = =0&& b% i = =0) {a/=i; b/=i; }    }    if(A <0){        //-        if(A/b! =0) {printf ("(%lld%lld/%lld)", A/b, (ABS (a))%B, b); }        Else{printf ("(%LLD/%LLD)", A, b); }    }    Else{        //+        if(A/b! =0) {printf ("%lld%lld/%lld", A/b, a%B, b); }        Else{printf ("%lld/%lld", A, b); }    }}
Capouis ' CODE

pat-basic-1034-Rational Number arithmetic

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.