1088 Rational Arithmetic (20 分)

该博客介绍了一道编程题目,涉及用C++处理两个有理数(分数)的加、减、乘、除运算。代码中定义了分数结构体,并实现了化简、加、减、乘、除等函数,同时考虑了负数、0分母等情况,输出结果遵循特定格式。示例输入和输出展示了代码的正确运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/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

分析:

 这道题目是有关分数的问题,逻辑上并不困难,但是代码有点繁杂,题目的要求就是给两个分数进行加减乘除,按照要求的格式输出即可,这道题我们首先要对分数进行结构体的定义,定义分子和分母,然后写加减乘除的四个函数,同时注意分数化简的要点,我们可以做如下几个规定:

  1. 分母如果是负数,就分子分母同时取相反数
  2. 如果分子是0,那么规定分母是1
  3. 如果不是情况2,进行约分,写一个gcd函数进行寻找最大公约数

 进行输出的时候注意三种情况,分母为1的时候只输出分子即可,是假分数的情况以及是真分数的情况。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct fraction{
	ll up,down;
};
ll gcd(ll a,ll b){
	if(b==0) return a;
	else return gcd(b,a%b);
}
fraction reduce(fraction r){
	if(r.down<0){
		r.up=-r.up;
		r.down=-r.down;
	}
	if(r.up==0) r.down=1;
	else{
		ll d=gcd(abs(r.up),abs(r.down));
		r.up/=d;
		r.down/=d;
	}
	return r;
}
fraction add(fraction a,fraction b){
	fraction r;
	r.down=a.down*b.down;
	r.up=a.up*b.down+a.down*b.up;
	return reduce(r);
}
fraction minu(fraction a,fraction b){
	fraction r;
	r.down=a.down*b.down;
	r.up=a.up*b.down-a.down*b.up;
	return reduce(r);
}
fraction multi(fraction a,fraction b){
	fraction r;
	r.down=a.down*b.down;
	r.up=a.up*b.up;
	return reduce(r);	
}
fraction divide(fraction a,fraction b){
	fraction r;
	r.down=a.down*b.up;
	r.up=a.up*b.down;
	return reduce(r);
}
void show(fraction r){
	r=reduce(r);
	if(r.up<0) printf("(");
	if(r.down==1) printf("%lld",r.up);
	else if(abs(r.up)>abs(r.down)){
		printf("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down);
	}
	else printf("%lld/%lld",r.up,r.down);
	if(r.up<0) printf(")");
}

int main(){
	
	fraction a,b;
	char m,c[4]={'+','-','*','/'};
	cin>>a.up>>m>>a.down>>b.up>>m>>b.down;
	
	
	for(int i=0;i<4;i++){
		show(a);
		cout<<" "<<c[i]<<" ";
		show(b);
		cout<<" = ";
		if(i==0) 
		    show(add(a,b));
		if(i==1) 
			show(minu(a,b));
		if(i==2)
			show(multi(a,b));
		if(i==3){
			if(b.up==0) {
				cout<<"Inf"<<endl;
				continue; 
			}
			show(divide(a,b));
		} 
		    	
		cout<<endl;
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值