1088 Rational Arithmetic (20分)【分数的四则运算】

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

解题思路:

这是一道和1081 Rational Sum (20分)类似的一道题目,不过这道题目是给出我们两个分数,要我们分别求出这两个分数的加减乘除四则运算的结果。

我们先来看一看加法四则运算的计算公式:

加法:result = \frac{f1.up*f2.down+f2.up*f1.down}{f1.down*f2.down}

减法:result = \frac{f1.up*f2.down-f2.up*f1.down}{f1.down*f2.down}

乘法:result = \frac{f1.up*f2.up}{f1.down*f2.down}

除法:result = \frac{f1.up*f2.down}{f1.down*f2.up}

有了这么几个计算公式,我们可以直接得到计算结果,只不过我们需要注意的是,要将输入的两个数以及输出的结果转化为指定格式:

1.如果分子为0,输出0;如果分母为0,输出inf;

2.如果为假分数要按照带分数的形式输出,如果为整数则输出整数;

3.如果为负数注意有括号包裹;

#include<iostream>
#include<math.h>
using namespace std;
#define ll long long

ll gcd(ll a, ll b)
{
	return b == 0 ? a : gcd(b, a%b);
}

void fun(ll up, ll down)
{
	if (up == 0 || down == 0)     //如果分子或分母其中一个为0
	{
		if (up == 0)
			cout << "0";
		else cout << "Inf";
		return;
	}
	int flag = 0;   //记录当前分数是否为负数
	if ((up < 0 && down > 0) || (up > 0 && down < 0))
		flag = 1;
	if (flag == 1)         //先输出负号
		cout << "(-";
	up = abs(up);
	down = abs(down);
	ll t = gcd(up, down);     //先约分
	up /= t;
	down /= t;
	if (up%down == 0)
	{
		cout << up / down;
		if(flag==1)
			cout<<")";
		return;
	}
	else
	{
		if (up > down)       //如果为假分数
		{
			cout << up / down << " " << up - up / down*down << "/" << down;
			if(flag==1)
				cout<<")";
		}
		else
		{
			cout << up << "/" << down;
			if(flag==1)
				cout<<")";
		}
	}
}

int main()
{
	ll up1, up2, down1, down2;
	scanf("%lld/%lld %lld/%lld", &up1, &down1, &up2, &down2);
	fun(up1, down1); cout << " + "; fun(up2, down2); cout << " = "; fun(up1 * down2 + up2 * down1, down1 * down2); cout << endl;
	fun(up1, down1); cout << " - "; fun(up2, down2); cout << " = "; fun(up1 * down2 - up2 * down1, down1 * down2); cout << endl;
	fun(up1, down1); cout << " * "; fun(up2, down2); cout << " = "; fun(up1*up2, down1*down2); cout << endl;
	fun(up1, down1); cout << " / "; fun(up2, down2); cout << " = "; fun(up1*down2, down1*up2);  cout << endl;
}

 

posted @ 2020-04-14 12:45  Hu_YaYa  阅读(14)  评论(0)    收藏  举报