本文主要是介绍pat 1088 Rational Number,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1088. 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/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/2Sample 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/6Sample Output 2:
1 2/3 + 0 = 1 2/3 1 2/3 - 0 = 1 2/3 1 2/3 * 0 = 01 2/3 / 0 = Inf
#include <cstdio> #include <cstring> #include <string> #include <cmath> using namespace std;char one[30], two[30]; long long fenzi[3], fenmu[3]; int flag[3];long long swap(long long &a, long long &b) {long long tmp = b;b = a;a = tmp; } long long gcd(long long a, long long b) {if(a < b){long long tmp = b;b = a;a = tmp;}while(b != 0){long long c = a%b;a = b;b = c;}return a; }void getNum(int index, char str[]) {int i;int len = strlen(str);for(i = 0; str[i] != '/'; i++){if(str[i] >= '0' && str[i] <= '9')fenzi[index] = fenzi[index] * 10 + str[i] - '0';}fenzi[index] = fenzi[index]*flag[index];i++;for(; i < len; i++){if(str[i] >= '0' && str[i] <= '9')fenmu[index] = fenmu[index]*10+str[i]-'0';} }void outPut(long long a, long long b) {int tmp = 0;if(a == 0){printf("0");return ;}long long yueshu = gcd(abs(a), abs(b));a = a / yueshu;b = b / yueshu;if(b < 0)a = -a, b = -b;if(a < 0){printf("(-");a = -a;if(a / b > 0)printf("%lld",a/b), tmp = 1;if(a%b == 0)printf(")");else{if(tmp == 1)printf(" %lld/%lld)", a%b, b);elseprintf("%lld/%lld)", a%b, b);}} else {if(a / b > 0)printf("%lld", a/b), tmp = 1;if(a%b != 0){if(tmp == 1)printf(" %lld/%lld", a%b, b);elseprintf("%lld/%lld", a%b, b);}} } int main() {while(scanf("%s %s", one, two) != EOF){memset(fenzi, 0, sizeof(fenzi));memset(fenmu, 0, sizeof(fenmu));flag[1] = 1, flag[2] = 1;int i, j, len1, len2;if(one[0] == '-')flag[1] = -1;if(two[0] == '-')flag[2] = -1;getNum(1, one);getNum(2, two);outPut(fenzi[1], fenmu[1]);printf(" + ");outPut(fenzi[2], fenmu[2]);printf(" = ");outPut(fenzi[1]*fenmu[2]+fenzi[2]*fenmu[1], fenmu[1]*fenmu[2]);printf("\n");outPut(fenzi[1], fenmu[1]);printf(" - ");outPut(fenzi[2], fenmu[2]);printf(" = ");outPut(fenzi[1]*fenmu[2]-fenzi[2]*fenmu[1], fenmu[1]*fenmu[2]);printf("\n");outPut(fenzi[1], fenmu[1]);printf(" * ");outPut(fenzi[2], fenmu[2]);printf(" = ");outPut(fenzi[1]*fenzi[2], fenmu[1]*fenmu[2]);printf("\n");outPut(fenzi[1], fenmu[1]);printf(" / ");outPut(fenzi[2], fenmu[2]);printf(" = ");if(fenmu[1]*fenzi[2] != 0)outPut(fenzi[1]*fenmu[2], fenmu[1]*fenzi[2]);elseprintf("Inf");printf("\n");}return 0; }
这篇关于pat 1088 Rational Number的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!