Pat-b 1034. Rational number arithmetic

Source: Internet
Author: User
Tags greatest common divisor

Topic content:

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 = 01 2/3 / 0 = Inf
Thinking Analysis:

This is the B in the personal feel the most wordy problem, the general idea is not difficult.
Use the structure to record fractions, including numerator, denominator, and symbols. Write four functions corresponding to the calculation plus, minus, multiply, divide, a function to output a single fraction.
In order to simplify the calculation, increase the function of fractional processing, guarantee the score after processing, the numerator denominator is positive, the sign bit is 1 or-1.
I am here to call four compute functions using a function pointer, so I write an output function for the entire calculation.

Code:
#include <stdio.h>typedef structfrac_{LongN, D, s;} FC;//The function of seeking greatest common divisorLonggcdLongALongb) {returnb = =0? A:GCD (b, a% b);}//Fractional processing functionFC Hand (FC a) {if(A.N <0) {A.S *=-1; A.N *=-1;}//If the numerator is negative, assign the symbol to the symbolic variable and the numerator takes a positive value    LongTMP = GCD (A.N, A.D);    A.N/= tmp; A.D/= tmp;//Numerator    returnA;}//single fractional output functionvoidPrt_fc (FC a) {if(A.D = =0)printf("INF");//If the denominator is 0, the output INF    Else{Longi = A.N/A.D;//Extract integer partA.N = a.n% A.D;//score to TRUE scoreA = Hand (a);//Numerator        if(A.N = =0&& i = =0)printf("0");//If the integer fractional part is 0, the output 0        Else{if(A.S = =-1)printf("(-");//If the symbol is negative, add parentheses and a minus sign            if(I! =0)printf("%ld", i);if(I! =0&& A.N! =0)printf(" ");if(A.N! =0)printf("%ld/%ld", A.N, A.D);if(A.S = =-1)printf(")"); }    }}//Calculation output functionvoidPrt_eq (FC A, FC B,CharC, FC (*fig) (FC, FC)) {Prt_fc (a);printf("%c", c); Prt_fc (b);printf(" = "); Prt_fc (Hand ((*fig) (A, b));printf("\ n");}//Calculation function, subtractionFC Plus (FC A, FC B) {return(FC) {A.S*A.N*B.D + B.S*B.N*A.D, A.D*B.D,1};} FC Sub (FC A, FC B) {return(FC) {A.S*A.N*B.D-B.S*B.N*A.D, A.D*B.D,1};} FC multi (FC A, FC B) {return(FC) {A.N*B.N, A.D*B.D, A.S*B.S};} FC Divis (FC A, FC B) {return(FC) {A.N*B.D, A.D*B.N, A.S*B.S};}intMain () {FC a = {0,0,1}, B = {0,0,1}, (*fig[]) (FC, FC) = {Plus, sub, Multi, Divis};Charc[5] ="+-*/";scanf("%ld/%ld%ld/%ld", &AMP;A.N, &AMP;A.D, &AMP;B.N, &AMP;B.D);//Read scores A, B     for(inti =0; I <4; i++) prt_eq (Hand (a), hand (b), C[i], fig[i]);//The processed fractions, calculation symbols, calculation functions, passing to the calculation output function    return 0;}

Click here to enter the question page

Pat-b 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.