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
分数的加减乘除。
解题的时候,注意求最大公约数和简化分式。
定义struct统一管理。
#include<iostream>
#include<algorithm>
using namespace std;
struct Form {
long int sym;
long int numerator;//分子
long int denominator;//分母
Form():sym(0),numerator(0),denominator(1) {}
};
long int gcd(long int a, long int b) {
return !b? a : gcd(b, a%b);
}
Form reduce(Form f) {
if(f.denominator < 0) {
f.numerator = - f.numerator;
f.denominator = - f.denominator;
}
if(f.numerator == 0) {
f.denominator = 1;
f.sym = 0;
return f;
}
if(f.denominator == 0) {
return f;
}
long int d = gcd(abs(f.numerator), f.denominator);//最大公约数
f.denominator /= d;
f.numerator /= d;
// bool flag = false;
// if(f.numerator < 0) {
// flag = true;
// }
if(abs(f.numerator) >= f.denominator) {
f.sym = f.numerator / f.denominator;//abs()
f.numerator = abs(f.numerator) % f.denominator;
}
return f;
}
Form add(Form f1,Form f2) {
Form f;
f.denominator = f1.denominator * f2.denominator;
f.numerator = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
f = reduce(f);
return f;
}
Form difference(Form f1,Form f2) {
Form f;
f.denominator = f1.denominator * f2.denominator;
f.numerator = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
f = reduce(f);
return f;
}
Form product(Form f1,Form f2) {
Form f;
f.denominator = f1.denominator * f2.denominator;
f.numerator = f1.numerator * f2.numerator;
f = reduce(f);
return f;
}
Form quotient(Form f1,Form f2) {
Form f;
f.denominator = f1.denominator * f2.numerator;
f.numerator = f1.numerator * f2.denominator;
f = reduce(f);
return f;
}
int main() {
Form f[2];
Form opt[4];//f_sum,f_difference,f_product,f_quotient,
bool q = true;
for(int i = 0; i < 2; i++) {
scanf("%ld/%ld",&f[i].numerator,&f[i].denominator);
}
opt[0] = add(f[0],f[1]);
opt[1] = difference(f[0],f[1]);
opt[2] = product(f[0],f[1]);
opt[3] = quotient(f[0],f[1]);
f[0] = reduce(f[0]);
f[1] = reduce(f[1]);
for(int i = 0; i < 4; i++) {
if(f[0].sym < 0 ||f[0].numerator < 0) {
q = false;
cout<<"(";
}
if(!f[0].sym) {
if(!f[0].numerator) {
cout<<0;
if(q){
cout<<" ";
}
} else {
cout<<f[0].numerator<<"/"<<f[0].denominator;
if(q){
cout<<" ";
}
}
} else {
if(!f[0].numerator) {
cout<<f[0].sym;
if(q){
cout<<" ";
}
} else {
cout<<f[0].sym<<" "<<f[0].numerator<<"/"<<f[0].denominator;
if(q){
cout<<" ";
}
}
}
if(f[0].sym < 0 ||f[0].numerator < 0) {
cout<<") ";
}
switch (i) {
case 0:
cout<<"+ ";
break;
case 1:
cout<<"- ";
break;
case 2:
cout<<"* ";
break;
case 3:
cout<<"/ ";
break;
}
if(f[1].sym < 0 ||f[1].numerator < 0) {
cout<<"(";
}
if(!f[1].sym) {
if(!f[1].numerator) {
cout<<0;
} else {
cout<<f[1].numerator<<"/"<<f[1].denominator;
}
} else {
if(!f[1].numerator) {
cout<<f[1].sym;
} else {
cout<<f[1].sym<<" "<<f[1].numerator<<"/"<<f[1].denominator;
}
}
if(f[1].sym < 0||f[1].numerator < 0) {
cout<<") = ";
} else {
cout<<" = ";
}
if(opt[i].denominator == 0) {
cout<<"Inf";
continue;
}
if(opt[i].sym < 0 || opt[i].numerator < 0) {
cout<<"(";
}
if(!opt[i].sym) {
if(!opt[i].numerator) {
cout<<0;
} else {
cout<<opt[i].numerator<<"/"<<opt[i].denominator;
}
} else {
if(!opt[i].numerator) {
cout<<opt[i].sym;
} else {
cout<<opt[i].sym<<" "<<opt[i].numerator<<"/"<<opt[i].denominator;
}
}
if(opt[i].sym < 0 || opt[i].numerator < 0) {
cout<<")";
}
if(i < 3 ) {
cout<<endl;
}
}
return 0;
}
本文介绍了一种使用C++实现分数加减乘除运算的方法。通过定义结构体管理分数的符号、分子和分母,并实现了求最大公约数、简化分数、加法、减法、乘法和除法等关键函数。文章提供了完整的代码示例,展示了如何输入两个分数并计算它们的和、差、积和商。
1492

被折叠的 条评论
为什么被折叠?



