1088 Rational Arithmetic (20 point(s))

1088 Rational Arithmetic (20 point(s))

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> 
#define ll long long
ll gcd(ll a,ll b){
	if(b==0) return a;
	else return gcd(b,a%b);
} 
void output(ll a,ll b){//a 分子 b分母 
	if(a==0){
		printf("0");return;
	} 
	bool isNeg = false;
	if(a>0&&b<0){
		isNeg = true; b = -b;
	}else if(a<0&&b>0){
		isNeg = true; a = -a;
	}else if(a<0&&b<0){
		a=-a;b=-b;
	}
	ll f = gcd(a,b);
	a = a/f; b = b/f;
	ll i = a / b;
	a = a - i * b;
	if(isNeg){
		printf("(-");
		if(i!=0){
			printf("%lld",i);
		}
		if(a!=0){
			if(i!=0) printf(" ");
			printf("%lld/%lld",a,b);
		}
		printf(")");
	}else{
		if(i!=0){
			printf("%lld",i);
		}
		if(a!=0){
			if(i!=0) printf(" ");
			printf("%lld/%lld",a,b);
		}		
	}
}
void sum(ll a1,ll b1,ll a2,ll b2){
	ll b = b1*b2;
	ll a = a1*b2+a2*b1;
	output(a1,b1);printf(" + ");
	output(a2,b2);printf(" = ");
	output(a,b);printf("\n");
}
void difference(ll a1,ll b1,ll a2,ll b2){
	ll b = b1*b2;
	ll a = a1*b2-a2*b1;
	output(a1,b1);printf(" - ");
	output(a2,b2);printf(" = ");
	output(a,b);printf("\n");
}
void product(ll a1,ll b1,ll a2,ll b2){
	ll b = b1*b2;
	ll a = a1*a2;
	output(a1,b1);printf(" * ");
	output(a2,b2);printf(" = ");
	output(a,b);printf("\n");
}
void quotient(ll a1,ll b1,ll a2,ll b2){
	output(a1,b1);printf(" / ");
	output(a2,b2);printf(" = ");
	if(a2==0){
		printf("Inf\n");
	}
	else{
		ll b = b1*a2;
		ll a = a1*b2;
		output(a,b);printf("\n");
	}
} 
int main(void){
	ll a1,a2,b1,b2;
	scanf("%lld/%lld",&a1,&b1);
	scanf("%lld/%lld",&a2,&b2);
	sum(a1,b1,a2,b2);
	difference(a1,b1,a2,b2);
	product(a1,b1,a2,b2);
	quotient(a1,b1,a2,b2);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值