pat 1088 Rational Number
来源:互联网 发布:照片恢复软件免费版 编辑:程序博客网 时间:2024/06/18 11:25
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/32/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/31 2/3 - 0 = 1 2/31 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
- Pat(Advanced Level)Practice--1088(Rational Arithmetic)
- PAT 1088 1088. Rational Arithmetic (模拟四则运算)
- PAT 1081. Rational Sum
- PAT 1088. Rational Arithmetic
- PAT Rational Arithmetic (20)
- 【PAT】1081. Rational Sum
- 【PAT】1088. Rational Arithmetic
- PAT--1081. Rational Sum
- PAT A1081. Rational Sum
- Rational Number Tree
- Rational Number Tree
- Rational Number.c
- PAT (Advanced Level) Practise 1088 Rational Arithmetic (20)
- PAT (Advanced Level) Practise 1088 Rational Arithmetic (20)
- PAT 1081. Rational Sum (20)
- PAT 1081. Rational Sum (20)
- PAT 1081. Rational Sum(20)
- 安装Linux虚拟机
- 新手上路,初学java,关于String的对象
- c语言大世界————06一时瑜亮(中)
- C++调用MATLAB实例
- linux网络命令
- pat 1088 Rational Number
- C++ sizeof 使用规则及陷阱分析
- 用正则表达式提取网页中的链接
- 【转】 cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- 半年学习小结
- 网站收藏
- 互联网协议入门(一)
- 2014年11月30日——80后程序猿的感慨
- HttpServletRequest