PAT 1034. 有理数四则运算

Posted A-Little-Nut

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 1034. 有理数四则运算相关的知识,希望对你有一定的参考价值。

PAT 1034. 有理数四则运算

本题要求编写程序,计算2个有理数的和、差、积、商。

输入格式:

输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0。

输出格式:

分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中没有超过整型范围的整数。

输入样例1:

2/3 -4/2

输出样例1:

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

输入样例2:

5/3 0/6

输出样例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

分析

自己写的程序如下,测试点2未过,如有大神路过,请指点一二,万分感谢

自己的代码

#include<iostream>
#include<math.h>
using namespace std;
long long int inf=0,flag3=1,int3,son3,mom3;
void print(long long int flag,long long int Int,long long int son,long long int mom){
    if(inf==1){
    cout<<"Inf"; return;
    }
    if(flag==0) 
    cout<<0;
    else if(flag<0){
    if(Int==0) 
    cout<<"("<<-son<<"/"<<mom<<")";
    else if(Int!=0&&son!=0)
    cout<<"("<<-Int<<" "<<son<<"/"<<mom<<")";
    else 
    cout<<"("<<-Int<<")";
    }
    else if(flag>0){
    if(Int==0) cout<<son<<"/"<<mom;
    else if(Int!=0&&son!=0)cout<<Int<<" "<<son<<"/"<<mom;
    else cout<<Int; 
    }   
    flag3=1;
}
long long int gcd(long long int t1,long long int t2) {
    return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
void huajian(long long int &son,long long int &mom,long long int &Int,long long int &flag){
    long long int s=abs(son);
    long long int temp=gcd(s,mom);
    son/=temp; mom/=temp;
    if(son<0) {flag=-1; son=-son;}
    else if(son==0) flag=0;
    Int=son/mom; son=son%mom;
} 
void jia(long long int s1,long long int m1,long long int s2,long long int m2){
    mom3=m1*m2;
    son3=m1*s2+m2*s1;
}
void jian(long long int s1,long long int m1,long long int s2,long long int m2){
    mom3=m1*m2;
    son3=s1*m2-s2*m1;
}
void chen(long long int s1,long long int m1,long long int s2,long long int m2){
    mom3=m1*m2;
    son3=s1*s2;
}
void chu(long long int s1,long long int m1,long long int s2,long long int m2){
    if(s2==0) inf=1;
    else{
        son3=s1*m2;
        mom3=s2*m1;
    }
    if(son3*mom3>0) {son3=abs(son3); mom3=abs(mom3); }
    else if(son3*mom3<0){son3=-abs(son3); mom3=abs(mom3);} 
}
int main(){
    long long int mom1,son1,int1,mom2,son2,int2,flag1=1,flag2=1;
    scanf("%lld/%lld %lld/%lld",&son1,&mom1,&son2,&mom2);
    char fuhao[4]={'+','-','*','/'};
    long long int m1=mom1,s1=son1,m2=mom2,s2=son2;
    huajian(son1,mom1,int1,flag1);
    huajian(son2,mom2,int2,flag2);
    for(int i=0;i<4;i++){
    print(flag1,int1,son1,mom1);
    cout<<" "<<fuhao[i]<<" ";
    print(flag2,int2,son2,mom2);
    cout<<" = ";
    switch(i){
        case 0:jia(s1,m1,s2,m2); break;
        case 1:jian(s1,m1,s2,m2); break;
        case 2:chen(s1,m1,s2,m2); break;
        case 3:chu(s1,m1,s2,m2); break;
        default:;
    }
    huajian(son3,mom3,int3,flag3);
    print(flag3,int3,son3,mom3);
    cout<<endl;
    }
    return 0;
} 

目前看过的本题最好的代码

#include <stdio.h>
long getgcd(long a, long b);
void printfrac(long a, long b);
int main (void) {
    long a1;
    long b1;
    long a2;
    long b2;
    char op[4] = {'+', '-', '*', '/'};
    int i;
    scanf("%ld/%ld %ld/%ld", &a1, &b1, &a2, &b2);
    for(i = 0; i < 4; i++) {
        printfrac(a1, b1);
        printf(" %c ", op[i]);
        printfrac(a2, b2);
        printf(" = ");
        switch (op[i]) {
            case '+':
                printfrac(a1 * b2 + a2 * b1, b1 * b2); 
                break;
            case '-':
                printfrac(a1 * b2 - a2 * b1, b1 * b2);
                break;
            case '*':
                printfrac(a1 * a2, b1 * b2);
                break;
            case '/':
                printfrac(a1 * b2, b1 * a2);
                break;
        }
        printf("\n");
    }
    return 0;
}
long getgcd(long t1,long t2) { // 辗转相除求最大公因子
    return t2 == 0 ? t1 :getgcd(t2, t1 % t2);
}
void printfrac (long a, long b) {
    if (b == 0) {
        printf("Inf");
        return;
    }
    int sign = 1;
    long gcd;
    if (a < 0) {
        a = -a;
        sign = sign * -1;
    }
    if (b < 0) {
        b = -b;
        sign = sign * -1;
    }
    gcd =getgcd(a, b);
    a = a / gcd;
    b = b / gcd;
    if (sign == -1) printf("(-");
    if (b == 1) {
        printf("%ld", a);  
    } else if (a > b) {
        printf("%ld %ld/%ld", a / b, a % b, b);
    } else {
        printf("%ld/%ld", a, b); // a < b
    }
    if (sign == -1) printf(")");
}

以上是关于PAT 1034. 有理数四则运算的主要内容,如果未能解决你的问题,请参考以下文章

PAT Basic 1034

PAT乙级1034

PAT乙级1034. 有理数四则运算(20)

pat 乙级1034

PAT乙级1034 有理数四则运算 (20 分)

1034. 有理数四则运算(20)