This problem requires a program to calculate the sum, difference, product and quotient of two rational numbers.
Input format:
The input in a row gives two rational numbers in fractional form according to the a1/b1 a2/b2 format. The numerator and denominator are all integers in the integer range. Negative sign can only appear in front of the numerator, and denominator is not 0.
Output format:
The sum, difference, product and quotient of two rational numbers are output in 4 rows according to the format order of rational number 2 = result of rational number 1 operator. Note that each rational number of output must be the simplest form of the rational number k a/b, where k is the integer part and a/b is the simplest fraction part; if it is a negative number, brackets must be added; if the dividing denominator is 0, then the output of Inf. The Title guarantees that there are no integers in the correct output that exceed the integer range.
Input Sample 1:
2/3 -4/2
Output Sample 1:
2/3 + (-2) = (-1 1/3) 2/3 - (-2) = 2 2/3 2/3 * (-2) = (-1 1/3) 2/3 / (-2) = (-1/3)
Input Sample 2:
5/3 0/6
Output Sample 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<stdio.h> #include<stdlib.h> void yue_fen(long long int *fen_zi,long long int *fen_mu,long long int *k) { *k = *fen_zi / *fen_mu; if(*k != 0) { *fen_zi = llabs(*fen_zi % *fen_mu); } else { *fen_zi = *fen_zi % *fen_mu; } *fen_mu = llabs(*fen_mu); for(int i = 10 ; i >= 2; i--) { if(0 == (*fen_zi % i) && 0 == (*fen_mu % i)) { *fen_zi = *fen_zi / i; *fen_mu = *fen_mu / i; i = 11; } } } int main() { long long int a1 = 0, b1 = 0, a2 = 0, b2 = 0,k1 = 0,k2 = 0; long long int resu_k[4]= {},resu_a[4] = {},resu_b[4] = {}; scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2); char symbol[4] = {'+','-','*','/'}; //Sum and difference if(0 == a1) { resu_a[0] = a2; resu_b[0] = b2; resu_a[1] = a2; resu_b[1] = b2; } else if(0 == a2) { resu_a[0] = a1; resu_b[0] = b1; resu_a[1] = a1; resu_b[1] = b1; } else { resu_a[0] = a1 * b2 + a2 * b1; resu_b[0] = b1 * b2; resu_a[1] = a1 * b2 - a2 * b1; resu_b[1] = b1 *b2; } yue_fen(&resu_a[0],&resu_b[0],&resu_k[0]); yue_fen(&resu_a[1],&resu_b[1],&resu_k[1]); //product if(0 == a1 || 0 == a2) { resu_a[2] = 0; resu_k[2] = 0; } else { resu_a[2] = a1 * a2; resu_b[2] = b1 * b2; yue_fen(&resu_a[2],&resu_b[2],&resu_k[2]); } //merchant int mark = 0; if(0 == a1 || 0 == a2) { mark = 1; } else if(a1 < 0 && a2 >0 || (a2 < 0 && a1 > 0)) { resu_a[3] = -1*llabs(a1) * llabs(b2); resu_b[3] = llabs(a2)* llabs(b1); yue_fen(&resu_a[3],&resu_b[3],&resu_k[3]); } else { resu_a[3] = -1 * a1 * b2; resu_b[3] = -1 * a2 * b1; yue_fen(&resu_a[3],&resu_b[3],&resu_k[3]); } //Rationalization of molecular denominator yue_fen(&a1,&b1,&k1); yue_fen(&a2,&b2,&k2); for(int i = 0 ; i < 4 ; i++) //Four operations are output separately { if(0 == a1) //Judge the first rational number { if(k1 < 0) printf("(%lld)",k1); else printf("%lld",k1); } else { if(0 == k1) { if(a1 > 0) printf("%lld/%lld",a1,b1); else printf("(%lld/%lld)",a1,b1); } else if(k1 < 0) printf("(%lld %lld/%lld)",k1,a1,b1); else printf("%lld %lld/%lld",k1,a1,b1); } printf(" %c ",symbol[i]); if(0 == a2) //Judging the Second Rational Number { if(k2 < 0) printf("(%lld)",k2); else printf("%lld",k2); } else { if(0 == k2) { if(a2 > 0) printf("%lld/%lld",a2,b2); else printf("(%lld/%lld)",a2,b2); } else if(k2 < 0) printf("(%lld %lld/%lld)",k2,a2,b2); else printf("%lld %lld/%lld",k2,a2,b2); } printf(" = "); if(3 == i && 1 == mark) { printf("Inf\n"); } else if(0 == resu_a[i]) //Judgement result { if(resu_k[i] < 0) printf("(%lld)\n",resu_k[i]); else printf("%lld\n",resu_k[i]); } else { if(resu_k[i] < 0) printf("(%lld %lld/%lld)\n",resu_k[i],resu_a[i],resu_b[i]); else if(0 == resu_k[i] && resu_a[i] > 0) printf("%lld/%lld\n",resu_a[i],resu_b[i]); else if(0 == resu_k[i] && resu_a[i] < 0) printf("(%lld/%lld)\n",resu_a[i],llabs(resu_b[i])); else printf("%lld %lld/%lld\n",resu_k[i],resu_a[i],resu_b[i]); } } return 0; }