码迷,mamicode.com
首页 > 其他好文 > 详细

A.1088 Rational Arithmetic (20)

时间:2018-06-06 00:55:02      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:ace   TE   name   style   def   test   oid   abs   not   

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:

2/3 -4/2

Sample Output 1:

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

Sample Input 2:

5/3 0/6

Sample Output 2:

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;

struct Fraction {
    ll up, down;
}f1, f2, result;

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}

Fraction Sum(Fraction f1, Fraction f2) {
    Fraction f;
    f.up = f1.up * f2.down + f2.up * f1.down;
    f.down = f1.down * f2.down;
    return f;
}

Fraction Difference(Fraction f1, Fraction f2) {
    Fraction f;
    f.up = f1.up * f2.down - f2.up * f1.down;
    f.down = f1.down * f2.down;
    return f;
}

Fraction Product(Fraction f1, Fraction f2) {
    Fraction f;
    f.up = f1.up * f2.up;
    f.down = f1.down * f2.down;
    return f;
}

Fraction Quotient(Fraction f1, Fraction f2) {
    Fraction f;
    f.up = f1.up * f2.down;
    f.down = f1.down * f2.up;
    return f;
}

Fraction Reduction(Fraction result) {
    if (result.down < 0) {
        result.up = -result.up;
        result.down = -result.down;
    }
    if (result.up == 0) result.down = 1;
    else {
        ll d = gcd(abs(result.up), abs(result.down));
        result.up /= d;
        result.down /= d;
    }
    return result;
}

void showResult(Fraction result) {
    result = Reduction(result);
    if (result.up < 0) printf("(");
    if (result.down == 1) printf("%lld", result.up);
    else if (abs(result.up) > result.down) printf("%lld %lld/%lld", result.up / result.down, abs(result.up % result.down), result.down);
    else printf("%lld/%lld", result.up, result.down);
    if (result.up < 0) printf(")");
}

int main() {
    scanf("%lld/%lld %lld/%lld", &f1.up, &f1.down, &f2.up, &f2.down);
    //加法
    showResult(f1);
    printf(" + ");
    showResult(f2);
    printf(" = ");
    showResult(Sum(f1, f2));
    printf("\n");
    //减法
    showResult(f1);
    printf(" - ");
    showResult(f2);
    printf(" = ");
    showResult(Difference(f1, f2));
    printf("\n");
    //乘法
    showResult(f1);
    printf(" * ");
    showResult(f2);
    printf(" = ");
    showResult(Product(f1, f2));
    printf("\n");
    //除法
    showResult(f1);
    printf(" / ");
    showResult(f2);
    printf(" = ");
    if (f2.up == 0) printf("Inf");
    else showResult(Quotient(f1, f2));
    printf("\n");
    return 0;
}

 

A.1088 Rational Arithmetic (20)

标签:ace   TE   name   style   def   test   oid   abs   not   

原文地址:https://www.cnblogs.com/Yaxadu/p/9142520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!