PTA 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/bis 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 <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include <numeric>
#include <climits>//INT_MAX
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
//#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b){
    return !b?a:gcd(b, a%b);
}
void f(ll t, ll a, ll b) {
	if (b==0) {
		printf("Inf");
		return;
	}
	else if(b<0) {
		a=-a;
		b=-b;
	}
	if (abs(a)>=b) {
		t=a/b;
		a=abs(a%b);
	}
	ll k=gcd(abs(a), b);
	a/=k, b/=k;
	if (t>0){
		printf("%lld", t);
		if(a)
			printf(" %lld/%lld", a, b);
	}
	else if(t<0){
		if(a)
			printf("(%lld %lld/%lld)", t, a, b);
		else
			printf("(%lld)", t);
	}
	else{
		if(a>0)
			printf("%lld/%lld", a, b);
		else if(a < 0)
			printf("(%lld/%lld)", a, b);
		else
			printf("0");
	}
}
int main() {
	ll a1, b1, a2, b2, t1=0,t2=0;
	scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);
	f(t1, a1, b1);
	printf(" + ");
	f(t2, a2, b2);
	printf(" = ");
	f(0, a1*b2+a2*b1, b1*b2);
	printf("\n");
	f(t1, a1, b1);
	printf(" - ");
	f(t2, a2, b2);
	printf(" = ");
	f(0, a1*b2-a2*b1, b1*b2);
	printf("\n");
	f(t1, a1, b1);
	printf(" * ");
	f(t2, a2, b2);
	printf(" = ");
	f(0, a1*a2, b1*b2);
	printf("\n");
	f(t1, a1, b1);
	printf(" / ");
	f(t2, a2, b2);
	printf(" = ");
	f(0, a1*b2, b1*a2);
	printf("\n");
	return 0;
}

 

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页