这道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
#include "pch.h"
#include <iostream>
#include<string.h>
#include<stdio.h>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
long int gys(long int a, long int b)
{
a = abs(a);
b = abs(b);
long int c = b;
while (a%b != 0)
{
c = a % b;
a = b;
b = c;
}
return c;
}
long int gbs(long int a, long int b, long int c)
{
return a * b / c;
}
long int huajian(long int a, long int b)
{
if (b == 0)
{
cout << 0;
return 0;
}
long int m = a / b;
a -= m * b;
long int yz = gys(a, b);
if (m != 0)
{
if (m < 0)
cout << "(";
cout << m;
if (a == 0 && m < 0)
cout << ")";
}
if (m != 0 && a != 0)
cout << " ";
if (a != 0)
{
if (m == 0 && a < 0)
cout << "(";
if (m != 0 && a < 0)
cout << -a / yz << "/" << b / yz;
else
cout<< a / yz << "/" << b / yz;
if (a < 0)
cout << ")";
}
if (m == 0 && a == 0)
{
cout << 0;
return 0;
}
return 1;
}
void add(long int n1, long int d1, long int n2, long int d2)
{
long int gong = gys(d1, d2);
long int tongfen = gbs(d1, d2, gong);
long int sum = tongfen / d1 * n1 + tongfen / d2 * n2;
huajian(n1, d1);
cout << " + ";
huajian(n2, d2);
cout << " = ";
huajian(sum, tongfen);
}
void jian(long int n1, long int d1, long int n2, long int d2)
{
long int gong = gys(d1, d2);
long int tongfen = gbs(d1, d2, gong);
long int cha= tongfen / d1 * n1 - tongfen / d2 * n2;
huajian(n1, d1);
cout << " - ";
huajian(n2, d2);
cout << " = ";
huajian(cha, tongfen);
}
void cheng(long int n1, long int d1, long int n2, long int d2)
{
long int fenzi = n1 * n2;
long int fenmu = d1 * d2;
huajian(n1, d1);
cout << " * ";
huajian(n2, d2);
cout << " = ";
huajian(fenzi, fenmu);
}
void chu(long int n1, long int d1, long int n2, long int d2)
{
huajian(n1, d1);
cout << " / ";
long int m=huajian(n2, d2);
cout << " = ";
if (m == 0)
cout << "Inf" << endl;
else
{
long int fenzi = n1 * d2;
long int fenmu = d1 * n2;
if (fenmu < 0)
{
fenmu = -fenmu;
fenzi = -fenzi;
}
huajian(fenzi, fenmu);
}
}
int main()
{
long int n1, d1, n2, d2;
char c;
cin >> n1 >> c >> d1;
cin >> n2 >> c >> d2;
add(n1,d1,n2,d2);
cout << endl;
jian(n1, d1, n2, d2);
cout << endl;
cheng(n1, d1, n2, d2);
cout << endl;
chu(n1, d1, n2, d2);
}