1088 Rational Arithmetic

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

代码长度限制

16 KB

时间限制

200 ms

内存限制

64 MB

栈限制

8192 KB

被恶心到,加法减法放一起,乘法除法放一起,中间要用long long,

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
string s1,s2;
long long int a1,a2,b1,b2;
long long gcb(long long a,long long b){
    return a%b==0?b:gcb(b,a%b);
}
void printupgrade(long long a1,long long a2,int mark)
{
    if(mark==-1){
        cout << "(-";
    }
    long d = 0;
    if(a1>a2)
    {
        d = a1/a2;
        a1 -= d*a2;
    }
    if(d!=0){
        if(a1!=0)
        cout << d << " ";
        else cout << d;
    }
    if(a1==0)
        a2 = 1;
    if(a1!=0){
        if(a2==1)
        cout << a1;
        else cout << a1 << "/" << a2;
    }else if(d==0&&a1==0) cout << 0;
    if(mark==-1)
        cout << ")";
}
void printfresualt(long long int a1,long long int a2,long long int b1,long long int b2,int mark1,int mark2,int mark3,char c,long long k1,long long k2){
    printupgrade(a1,a2,mark1);
    cout << " " <> s1 >>s2;
    string str1,str2,str3,str4;
    stringstream ss1(s1);
    stringstream ss2(s2);
    getline(ss1,str1,'/');
    getline(ss1,str2,'/');
    a1 = stoll(str1);
    a2 = stoll(str2);
    getline(ss2,str3,'/');
    getline(ss2,str4,'/');
    b1 = stoll(str3);
    b2 = stoll(str4);
    int mark1,mark2;
    if(a1<0)
        mark1 = -1;
    else if(a1>0)
        mark1=1;
    else mark1 = 0;
    if(b1<0)
        mark2 = -1;
    else if(b1>0)
        mark2=1;
    else mark2 = 0;
    a1 = abs(a1);
    b1 = abs(b1);
    long long c1 = gcb(a1,a2);
    a1 /= c1;
    a2 /= c1;
    c1 = gcb(b1,b2);
    b1 /= c1;
    b2 /= c1;
    addcal(a1,a2,b1,b2,mark1,mark2,'+');
    mark2 = 0-mark2;
    addcal(a1,a2,b1,b2,mark1,mark2,'-');
    mark2 = 0-mark2;
    product(a1,a2,b1,b2,mark1,mark2,'*');
    product(a1,a2,b1,b2,mark1,mark2,'/');
    return 0; 
}

你可能感兴趣的:(c++,算法,开发语言)