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

思路:这题是算法笔记中分数四则运算中的实际运用,参考:
算法笔记-分数的四则运算
注意输出,负数一定要带括号,所以showResult()函数和上述笔记中的略有不同;还有一定要用long long存储分子分母,不然测试点3无法通过

#include <iostream>
#include <cstdio>
using namespace std;
struct Fraction
{
	long long up, down;
};
int gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
Fraction reduction(Fraction result);
Fraction add(Fraction f1, Fraction f2);
Fraction minu(Fraction f1, Fraction f2);
Fraction multi(Fraction f1, Fraction f2);
Fraction divid(Fraction f1, Fraction f2);
void showResult(Fraction r);
int main()
{
	Fraction a;
	Fraction b;
	scanf("%lld/%lld %lld/%lld", &a.up, &a.down, &b.up, &b.down);
	for (int i = 0; i < 4; i++)
	{
		showResult(a);
		switch (i)
		{
		case 0:
			cout << " + ";
			showResult(b);
			cout << " = ";
			showResult(add(a, b));
			break;
		case 1:
			cout << " - ";
			showResult(b);
			cout << " = ";
			showResult(minu(a, b));
			break;
		case 2:
			cout << " * ";
			showResult(b);
			cout << " = ";
			showResult(multi(a, b));
			break;
		case 3:
			{
			cout << " / ";
			showResult(b);
			cout << " = ";
			showResult(divid(a, b));
			break;
			}
		}
		cout << endl;
	}
	return 0;
}
Fraction reduction(Fraction result)
{
	if (result.down < 0)
	{
		result.up = -result.up;
		result.down = -result.down;
	}
	if (result.up == 0)
		result.down = 1;
	else
	{
		int d = gcd(abs(result.up), abs(result.down));
		result.up /= d;
		result.down /= d;
	}
	return result;
}
Fraction add(Fraction f1, Fraction f2)
{
	Fraction result;
	result.up = f1.up * f2.down + f2.up * f1.down;
	result.down = f1.down * f2.down;
	return reduction(result);
}
Fraction minu(Fraction f1, Fraction f2)
{
	Fraction result;
	result.up = f1.up * f2.down - f2.up * f1.down;
	result.down = f1.down * f2.down;
	return reduction(result);
}
Fraction multi(Fraction f1, Fraction f2)
{
	Fraction result;
	result.up = f1.up * f2.up;
	result.down = f1.down * f2.down;
	return reduction(result);
}
Fraction divid(Fraction f1, Fraction f2)
{
	Fraction result;
	result.up = f1.up * f2.down;
	result.down = f1.down * f2.up;
	return reduction(result);
}
void showResult(Fraction r)
{
	r = reduction(r);
	if (r.down == 1)
	{
		if (r.up >= 0)
			printf("%lld", r.up);
		else
			printf("(%lld)", r.up);
	}
	else if (abs(r.up) > abs(r.down))
	{
		if (r.up > 0 && r.down != 0)
			printf("%lld %lld/%lld", r.up / r.down, abs(r.up) % r.down, r.down);
		else if (r.up < 0 && r.down != 0)
			printf("(%lld %lld/%lld)", r.up / r.down, abs(r.up) % r.down, r.down);
		else if (r.down == 0) //如果分母为0,则输出Inf
			printf("Inf");
	}
	else
	{
		if (r.up >= 0)
			printf("%lld/%lld", r.up, r.down);
		else
			printf("(%lld/%lld)", r.up, r.down);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值