1088. Rational Arithmetic (20)

1088. Rational Arithmetic (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
注意这道题不能用int,否则后两个点会过不了,必须用long,int数据范围不够
#include <cstdio>
#include <cstring>
#include <cmath>

long maxFac(long a, long b){
	long temp;
	if(a < b){
		temp = a;
		a = b;
		b = temp;
	}
	if(a == 0 || b == 0)
		return 1;
	while(b != 0){
		temp = a % b;
		a = b;
		b = temp;
	}
	return a;
}

void add(long a1, long b1, long a2, long b2, long& a3, long& b3){
	a3 = a1 * b2 + a2 * b1;
	b3 = b1 * b2;
}

void minus(long a1, long b1, long a2, long b2, long& a3, long& b3){
	a3 = a1 * b2 - a2 * b1;
	b3 = b1 * b2;
}

void multi(long a1, long b1, long a2, long b2, long& a3, long& b3){
	a3 = a1 * a2;
	b3 = b1 * b2;
}

void divide(long a1, long b1, long a2, long b2, long& a3, long& b3){
	a3 = a1 * b2;
	b3 = b1 * a2;
}

void toStd(long a, long b, char* res){
	bool neg = false;
	char fir_part[50], sec_part[50];

	if (b < 0){
		b = -b;
		a = -a;
	}
	if (a < 0){
		a = -a;
		neg = true;
	}
	if(a == 0){
		strcpy(res, "0");
		return;
	}
	if(b == 0){
		strcpy(res, "Inf");
		return;
	}
	long k, sf;
	k = a / b;
	sf = a % b;
	long maxFacNum = maxFac(sf, b);
	long newa = sf / maxFacNum;
	long newb = b / maxFacNum;

	if(neg){
		if(k != 0)
			k = -k;
		else
			newa = -newa;
	}

	if(k != 0){
		sprintf(fir_part, "%ld", k);
		if(newa != 0)
			strcat(fir_part, " ");
	}
	else{
		sprintf(fir_part, "");
	}
	if(newa != 0){
		sprintf(sec_part, "%ld/%ld", newa, newb);
	}
	else{
		sprintf(sec_part, "");
	}
	strcpy(res, fir_part);
	strcat(res, sec_part);
	if(neg){
		char temp[50];
		strcpy(temp, res);
		strcpy(&res[1], temp);
		res[0] = '(';
		strcat(res, ")");
	}
	return ;
}

void output(char* c1, char* c2, long a3, long b3, char op){
	char c3[50];
	toStd(a3, b3, c3);
	printf("%s %c %s = %s\n", c1, op, c2, c3);
}

int main(){
	char fir[50], sec[50];
	scanf("%s %s", fir, sec);
	long a1, b1, a2, b2, a3, b3;
	char c1[50], c2[50];
	sscanf(fir, "%ld/%ld", &a1, &b1);
	sscanf(sec, "%ld/%ld", &a2, &b2);

	toStd(a1, b1, c1);
	toStd(a2, b2, c2);

	add(a1, b1, a2, b2, a3, b3);
	output(c1, c2, a3, b3, '+');

	minus(a1, b1, a2, b2, a3, b3);
	output(c1, c2, a3, b3, '-');

	multi(a1, b1, a2, b2, a3, b3);
	output(c1, c2, a3, b3, '*');

	divide(a1, b1, a2, b2, a3, b3);
	output(c1, c2, a3, b3, '/');
	// char temp[50];
	// for(long i = 0; i < 100; i++){
	// 	long a, b;
	// 	scanf("%s", temp);
	// 	sscanf(temp, "%ld/%ld", &a, &b);
	// 	toStd(a, b, temp);
	// 	printf("%s\n", temp);
	// }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值