A.1088 Rational Arithmetic (20)

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;
}

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

时间: 2024-11-09 02:03:48

A.1088 Rational Arithmetic (20)的相关文章

1088. Rational Arithmetic (20)——PAT (Advanced Level) Practise

题目信息 1088. Rational Arithmetic (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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 fi

1088. Rational Arithmetic (20)

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 number

PAT (Advanced Level) 1088. Rational Arithmetic (20)

简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algor

PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验细节处理,其它没啥好说的. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; long long numerato

pat1088. Rational Arithmetic (20)

1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Spe

A1088. Rational Arithmetic (20)

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 number

1081. Rational Sum (20)【模拟】——PAT (Advanced Level) Practise

题目信息 1081. Rational Sum (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum. Input Specification: Each input file contains one test case. Each case star

pat1081. Rational Sum (20)

1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum. Input Specification: Each input file contains one

SICP ex2-1 modified rational arithmetic

经过十多天煎熬,本宝宝终于顺利进入第二章,傲娇脸 首先,本章章首简要回顾了上一章的基本内容以及总体概述了data abstraction 的原因重要性,以及介绍了本章将会学习的一些东西 2.1.1通过一个rational arithmetic的例子来引出data abstraction 并且介绍基础pair 语法 ex2-1要求修改书中所给的make-rat使之能够在给定参数为-时,将符号放在分子出输出,两种思路 一 修改gcd 使之能够处理负数 二 利用abs 将gcd 参数约束为正数,并保留