shunfurh
编程介的小学生
2018-11-15 04:52
采纳率: 92.7%
浏览 672
已采纳

关系运算, 用一个C语言实现?

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • caozhy
    已采纳
    点赞 评论
  • fenghua2010nb
    fenghua2010nb 2018-11-15 05:17

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    long long gcd(long long a,long long b){//不能保证返回的符号,但能保证返回的绝对值大小
    if(b==0){
    if(a<0){
    a=-a;
    }
    return a;
    }
    return gcd(b,a%b);
    }
    long long com;
    void output(long long fz1,long long fm1){
    if(fz1==0){
    printf("0");
    return;
    }
    com=gcd(fz1,fm1);
    fz1/=com;
    fm1/=com;

    //cout<<fz1<<" "<<fm1<<" "<<com<<endl;
    
    if(fz1%fm1==0){
        printf("%lld",fz1/fm1);
    }
    else{
        if(fz1/fm1){
            printf("%lld ",fz1/fm1);
            if(fz1<0){
                fz1=-fz1;
            }
        }
        printf("%lld/%lld",fz1-fz1/fm1*fm1,fm1);
    }
    

    }
    void add(long long fz1,long long fm1,long long fz2,long long fm2){
    if(fz1<0){
    printf("(");
    output(fz1,fm1);
    printf(")");
    }
    else{
    output(fz1,fm1);
    }
    printf(" + ");
    if(fz2<0){
    printf("(");
    output(fz2,fm2);
    printf(")");
    }
    else{
    output(fz2,fm2);
    }
    com=gcd(fm1,fm2);
    fz2*=fm1/com;
    fz1*=fm2/com;
    fm1*=fm2/com;

    //cout<<fz1<<"  "<<fm1<<"  "<<fz2<<"  "<<fm2<<endl;
    
    
    printf(" = ");
    fz1+=fz2;
    if(fz1<0){
        printf("(");
        output(fz1,fm1);
        printf(")");
    }
    else{
        output(fz1,fm1);
    }
    

    }
    void sub(long long fz1,long long fm1,long long fz2,long long fm2){
    if(fz1<0){
    printf("(");
    output(fz1,fm1);
    printf(")");
    }
    else{
    output(fz1,fm1);
    }
    printf(" - ");
    if(fz2<0){
    printf("(");
    output(fz2,fm2);
    printf(")");
    }
    else{
    output(fz2,fm2);
    }
    com=gcd(fm1,fm2);
    fz2*=fm1/com;
    fz1*=fm2/com;
    fm1*=fm2/com;
    printf(" = ");
    fz1-=fz2;
    if(fz1<0){
    printf("(");
    output(fz1,fm1);
    printf(")");
    }
    else{
    output(fz1,fm1);
    }
    }
    void mul(long long fz1,long long fm1,long long fz2,long long fm2){
    if(fz1<0){
    printf("(");
    output(fz1,fm1);
    printf(")");
    }
    else{
    output(fz1,fm1);
    }
    printf(" * ");
    if(fz2<0){
    printf("(");
    output(fz2,fm2);
    printf(")");
    }
    else{
    output(fz2,fm2);
    }
    printf(" = ");
    fz1*=fz2;
    fm1*=fm2;
    if(fz1<0){
    printf("(");
    output(fz1,fm1);
    printf(")");
    }
    else{
    output(fz1,fm1);
    }
    }
    void quo(long long fz1,long long fm1,long long fz2,long long fm2){
    if(fz1<0){
    printf("(");
    output(fz1,fm1);
    printf(")");
    }
    else{
    output(fz1,fm1);
    }
    printf(" / ");
    if(fz2<0){
    printf("(");
    output(fz2,fm2);
    printf(")");
    }
    else{
    output(fz2,fm2);
    }
    printf(" = ");
    fz1*=fm2;
    fm1*=fz2;
    if(fm1==0){
    printf("Inf");
    return;
    }
    if(fm1<0){
    fm1=-fm1;
    fz1=-fz1;
    }
    if(fz1<0){
    printf("(");
    output(fz1,fm1);
    printf(")");
    }
    else{
    output(fz1,fm1);
    }
    }
    int main()
    {
    //freopen("D:\INPUT.txt","r",stdin);
    long long fz1,fm1,inter1,fz2,fm2,inter2;
    scanf("%lld/%lld %lld/%lld",&fz1,&fm1,&fz2,&fm2);

    //cout<<fm2<<endl;
    
    com=gcd(fz1,fm1);
    fz1/=com;
    fm1/=com;
    com=gcd(fz2,fm2);
    
    //cout<<com<<endl;
    //cout<<fm2<<endl;
    fz2/=com;
    fm2/=com;
    //cout<<fm2<<endl;
    //计算
    //cout<<fz1<<"  "<<fm1<<"  "<<fz2<<"  "<<fm2<<endl;
    add(fz1,fm1,fz2,fm2);
    printf("\n");
    sub(fz1,fm1,fz2,fm2);
    printf("\n");
    mul(fz1,fm1,fz2,fm2);
    printf("\n");
    quo(fz1,fm1,fz2,fm2);
    printf("\n");
    return 0;
    

    }

    点赞 评论
  • qq_40946921
    Italink 2018-11-15 14:50
    
    #include<iostream>
    #include<string>
    using namespace std;
    long long max(long long a, long long b) {       //获取最大公约数
        return b == 0 ? a : max(b, a % b);
    }
    long long classify(long long a[4]) {        //化简分数
        a[0] = 0;   //初始化整数部分
        a[3] = 1;   //初始化符号
        if (a[1] < 0) {
            a[1] = abs(a[1]);
            a[3] *= -1;  //符号累乘
        }
        if (a[2] < 0) {
            a[2] = abs(a[2]);
            a[3] *= -1;
        }
        long long i = max(a[1], a[2]);
        a[1] /= i;
        a[2] /= i;
        if (a[2]) {         //确保分母不为0,化简为带分数。
            a[0] = a[1] / a[2];
            a[1] %= a[2];
        }
        return 0;
    }
    string show(long long a[4]) {           //按题目要求返回该分数
        string s;
        if (a[2] == 0)         //分母为0,报错
            return "Inf"; 
        if (a[0] == 0 && a[1] == 0)   //整数部分和分子同时为0,返回‘0’
            return "0";
        if (a[0])    //整数部分存在,则加入
            s += (to_string(a[0]));
        if (a[0] && a[1])   //如果还存在分数部分,中间有' '(空格)
            s += ' ';
        if (a[1])       //分子存在,则加入分数部分
            s += (to_string(a[1]) + '/' + to_string(a[2]));
        if (a[3] == -1)      //如果为负数,需要加负号和括号
            s = "(-" + s + ")";
        return s;
    }
    int main()
    {
        long long a[4], b[4], c[4][4];  //数组分别存储整数部分、分子、分母和符号。
        char ch;
        cin >> a[1] >> ch >> a[2] >> b[1] >> ch >> b[2];
        c[0][1] = a[1] * b[2] + a[2] * b[1];
        c[0][2] = a[2] * b[2];
        c[1][1] = a[1] * b[2] - a[2] * b[1];
        c[1][2] = a[2] * b[2];
        c[2][1] = a[1] * b[1];
        c[2][2] = a[2] * b[2];
        c[3][1] = a[1] * b[2];
        c[3][2] = a[2] * b[1];
        classify(a);
        classify(b);
        classify(c[0]);
        classify(c[1]);
        classify(c[2]);
        classify(c[3]);
        cout << show(a) << " + " << show(b) << " = " << show(c[0]) << endl;
        cout << show(a) << " - " << show(b) << " = " << show(c[1]) << endl;
        cout << show(a) << " * " << show(b) << " = " << show(c[2]) << endl;
        cout << show(a) << " / " << show(b) << " = " << show(c[3]) << endl;
        return 0;
    }
    
    
    点赞 评论

相关推荐