有理数四则运算

本题要求编写程序,计算2个有理数的和、差、积、商。

输入格式:

输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0

输出格式:

分别在4行中按照“有理数1运算符有理数2 =结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中没有超过整型范围的整数。

输入样例1

2/3 -4/2

输出样例1

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

输入样例2

5/3 0/6

输出样例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

【C++程序】

---------------

#include <iostream>
#include <stdexcept>
using namespace std;
class Fraction {
public:
	Fraction(long a, long b);
	Fraction(long a);
	Fraction();
	int getNumerator() const;
	int getDenominator() const; 
	Fraction add(const Fraction &other) const;
	Fraction sub(const Fraction &other) const;
	Fraction mul(const Fraction &other) const;
	Fraction div(const Fraction &other) const;
	void input(); 
	void output();
	void reduce(); 
private:
	long numerator;
	long denominator;
	long gcd();
};
Fraction::Fraction(long a, long b) {
	numerator = a;
	denominator = b;
	// if b is negative, the Fraction will change b to be positive. 
	// So, again, 1/-3 will be changed to -1/3
	if(denominator < 0) {
		numerator *= -1;
		denominator *= -1;
	}
	reduce();
}
Fraction::Fraction(long a) {
	numerator = a;
	denominator = 1;
}
Fraction::Fraction() {
	numerator = 0;
	denominator = 1;
}
int Fraction::getNumerator() const {
	return numerator;
}
int Fraction::getDenominator() const {
	return denominator;
}
Fraction Fraction::add(const Fraction &other) const {
	long num = numerator * other.denominator + denominator * other.numerator;
	long den = denominator * other.denominator;
	Fraction result(num, den);
	result.reduce();
	return result;
}
Fraction Fraction::sub(const Fraction &other) const {
	long num = numerator * other.denominator - denominator * other.numerator;
	long den = denominator * other.denominator;
	Fraction result(num, den);
	result.reduce();
	return result;
}
Fraction Fraction::mul(const Fraction &other) const {
	long num = numerator * other.numerator;
	long den = denominator * other.denominator;
	Fraction result(num, den);
	result.reduce();
	return result;
}
Fraction Fraction::div(const Fraction &other) const {
	long num = numerator * other.denominator;
	long den = denominator * other.numerator;
	if(den == 0)
		throw runtime_error("Inf");
	else {
		Fraction result(num, den);
		result.reduce();
		return result;
	}
}
void Fraction::input() {
	char ignore;
	cin >> numerator;
	cin >> ignore;
	cin >> denominator;
	if(denominator < 0) {
		numerator *= -1;
		denominator *= -1;
	}
	reduce();
}
void Fraction::output() {
	long num = numerator;
	long den = denominator;
	long k;
	if(num < 0) {
		num = -num;  
		if(num >= den) {  
			k = num / den;  
			num = num % den;  
			k = -k;  
		}
		else {  
			num = -num;  
			k = 0;  
		}  
	}
	else {  
		if(num >= den) {   
			k = num / den;  
			num = num % den;  
		}
		else {  
			k = 0;  
		}  
	}  
	if(k < 0) {  
		cout << "(" << k;  
		if(num == 0)
           cout << ")";  
		else  
           cout << " " << num << "/" << den << ")";  
	}
	else if(k > 0) {  
		if(num > 0) 
			cout << k << " " << num << "/" << den;  
		else if(num == 0)
			cout << k;  
	}
	else {  
		if(num > 0) 
			cout << num << "/" << den;  
		else if(num == 0) 
			cout << "0";  
		else 
			cout << "(" << num << "/" << den << ")";  
	}  
}
void Fraction::reduce() {
	long g = gcd();
	if(g != 1) {
		numerator /= g;
		denominator /= g;
	}
}
long Fraction::gcd() {
	long a = numerator;
	long b = denominator;
	if(a == 0)
		return b;
	if(a < 0)
		a = -a;
    long r;
    while(true) {
        r = a % b;
        if(r == 0)
            break;
        a = b;
        b = r;
    }
    return b;
}
int main() {
	Fraction f1, f2, f3;
	bool b;
	
	f1.input();
	f2.input();
	
	f1.output();
	cout << " + ";
	f2.output();
	cout << " = ";
	f3 = f1.add(f2);
	f3.output();
	cout << endl;
	
	f1.output();
	cout << " - ";
	f2.output();
	cout << " = ";
	f3 = f1.sub(f2);
	f3.output();
	cout << endl;
	
	f1.output();
	cout << " * ";
	f2.output();
	cout << " = ";
	f3 = f1.mul(f2);
	f3.output();
	cout << endl;
	
	f1.output();
	cout << " / ";
	f2.output();
	cout << " = ";
	try {
		f3 = f1.div(f2);
		f3.output();
	}
	catch(runtime_error &ex) {
		cout << ex.what();
	}
	cout << endl;
	return 0;
}





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值