PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]

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

 题目大意:给出两个分数,并给出其加减乘除的结果表示,在除法时,如果分子为0,那么输出Inf。

 //虽然从来没做过这样的题目,但是感觉还是比较简单的。

#include <stdio.h>
#include<iostream>
#include<cmath>
using namespace std;
int main() {
    long int a[3],b[3];
    long int re1[3],re2[3],re3[3],re[4];
    scanf("%ld/%ld %ld/%ld",a[1],a[2],b[1],b[2]);
    //那么这个就是求最小公倍数,最大公因数的问题了。
    long int ming=max(a[2],b[2]);
    long long x=a[2]*b[2];
    for(int i=ming;i<=x;i++){
        if(i%a[2]==0&&i%b[2]==0){
             ming=i;break;
        }
    }
    a[0]=a[1]/a[2];
    b[0]=b[1]/b[2];
    a[1]=a[1]*ming/a[2];
    b[1]=b[1]*ming/b[2];
    a[2]=ming;
    b[2]=ming;
    re1[]
    //需要存好多种形式的结果啊!

    return 0;
}
View Code

//自己写着写着就写不下去了。没有底。各种细节,感觉控制不了。 

代码来自:https://www.liuchuo.net/archives/1906

#include <iostream>
#include<stdio.h>
using namespace std;
long long int a, b, c, d;//使用这种方法需要数据的范围很大。

long long int gcd(long long int t1, long long int t2) {
    return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}

void func(long long int m, long long int n) {
    int flag1 = 0;
    int flag2 = 0;
    if (n == 0) {//分母肯定是不能为0得,如果有1/3 0/1这样的输入,在加法中,分母会变成3而不是0.
        cout << "Inf";
        return ;
    }
    if (m == 0) {
        cout << 0;
        return ;
    }

    if (m < 0) {
        m = 0 - m;
        flag1 = 1;
    }
    if (n < 0) {
        n = 0 - n;
        flag2 = 1;
    }
    int flag = 0;
    if (flag1 == 1 && flag2 == 1) {
        flag = 0;
    } else if (flag1 == 1 || flag2 == 1) {
        flag = 1;//这个主要是来确定整个结果包括分子和分母的符号
    }
    if (m == n) {
        if (flag == 1)
            cout << "(-1)";//计算结果为负值,需要加括号
        else
            cout << "1";//计算结果为正值,不需要加。
        return;
    }

    long long int x = m % n;//因为m不可能=0了,之前已经判断过了,所以此处
    //如果x为0,那么肯定就是没有余数。
    long long int y = m / n;
    if (x == 0) {
        if (flag == 0)
            cout << y;
        else
            cout << "(-" << y << ")";
        return ;
    } else {
        long long int t1 = m - y * n;
        long long int t2 = n;
        long long int t = gcd(t1, t2);
        t1 = t1 / t;
        t2 = t2 / t;
        if (flag == 1) {
            cout << "(-";
        if (y != 0)//假分数
            cout << y << " " << t1 << "/" << t2;
            else
                cout << t1 << "/" << t2;
        cout << ")";
        } else {//真分数
            if (y != 0)
                cout << y << " " << t1 << "/" << t2;
            else
                cout << t1 << "/" << t2;
        }
    }
}

void add() {
    long long int m, n;//还没见过这个数据类型
    m = a * d + b * c;//直接这样得出分子,厉害。
    n = b * d;//分母。
    func(a, b);//处理方式都是一样的。
    cout << " + ";
    func(c, d);
    cout << " = ";
    func(m, n);
    cout << endl;
}

void min() {
    long long int m, n;
    m = a * d - b * c;
    n = b * d;
    func(a, b);
    cout << " - ";
    func(c, d);
    cout << " = ";
    func(m, n);
    cout << endl;
}

void multi() {
    long long int m, n;
    m = a * c;
    n = b * d;
    func(a, b);
    cout << " * ";
    func(c, d);
    cout << " = ";
    func(m, n);
    cout << endl;
}

void div() {
    long long int m, n;
    m = a * d;
    n = b * c;
    func(a, b);
    cout << " / ";
    func(c, d);
    cout << " = ";
    func(m, n);
    cout << endl;
}

int main() {
    scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
    add();
    min();
    multi();
    div();
    return 0;
}

 

//学习了!各种细节考虑的都很好。

1.对每一个数都有一个函数进行处理,十分简洁。

2.由于数可能会非常大,所以使用了long long int这种数据类型

3.顺便复习了,如何求两个数得最大公因数。!

转载于:https://www.cnblogs.com/BlueBlueSea/p/9532561.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
8.17 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations: a. Add two Rational numbers: The result of the addition should be stored in reduced form. b. Subtract two Rational numbers: The result of the subtraction should be stored in reduced form. c. Multiply two Rational numbers: The result of the multiplication should be stored in reduced form. d. Divide two Rational numbers: The result of the division should be stored in reduced form. e. Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. f. Print Rational numbers in floating-point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.) – 提示: – 有理数是有分子、分母以形式a/b表示的数,其中a是分子,b是分母。例如,1/3,3/4,10/4。 – 有理数的分母不能为0,分子却可以为0。每个整数a等价于有理数a/1。有理数用于分数的精确计算中。例如1/3=0.0000…,它不能使用数据类型double或float的浮点格式精确表示出来,为了得到准确结果,必须使用有理数。 – Java提供了整数和浮点数的数据类型,但是没有提供有理数的类型。 – 由于有理数与整数、浮点数有许多共同特征,并且Number类是数字包装的根类,因此,把有理数类Rational定义为Number类的一个子类是比较合适的。由于有理数是可比较的,那么Rational类也应该实现Comparable接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值