分数

分数

1.基本运算 (+, -, *, /) (重载运算符)

LL Abs (LL x) { return x > 0 ? x : -x; }

LL gcd (LL x, LL y) {
	x = Abs (x), y = Abs (y);
	if (x > y) swap (x, y);
	
	if (x == 0) return y;
	else return gcd (y % x, x);
}

LL lcm (LL x, LL y) {
	return x * (y / gcd (x, y));
}

struct fraction {
	LL a, b;
	
	void read () {
		scanf ("%lld %lld", &a, &b);
		if (b < 0) a = -a, b = -b;
	}
	
	fraction operator + (const fraction x) const {
		fraction ans;
		ans.b = lcm (b, x.b);
		ans.a = x.a * (ans.b / x.b) + a * (ans.b / b);
		int temp = gcd (ans.a, ans.b);
		ans.a /= temp, ans.b /= temp;
		return ans;
	}
	
	fraction operator * (const fraction x) const {
		fraction ans;
		LL val1 = gcd (a, x.b), val2 = gcd (b, x.a);
		ans.a = a * x.a / val1;
		ans.b = b * x.b / val2;
		LL temp = gcd (ans.a, ans.b);
		ans.a /= temp;
		ans.b /= temp;
		return ans;
	}
	
	fraction operator - (const fraction tem) const {
		fraction x, y; x.a = a, x.b = b;
		y.a = -tem.a; y.b = tem.b;
		return x + y;
	}
	
	fraction operator / (const fraction tem) const {
	    fraction x, y; x.a = a; x.b = b;
	    y.a = tem.b; y.b = tem.a;
	    return x * y;
	}
	
	void write () {
	    if (a % b == 0) printf ("%lld", a / b);
		else printf ("%lld/%lld", a, b);
	}
}; 

2.基本运算 (+ - * /)(运算)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define LL long long

LL Abs (LL x) { return x > 0 ? x : -x; }

LL gcd (LL x, LL y) {
	x = Abs (x), y = Abs (y);
	if (x > y) swap (x, y);
	
	if (x == 0) return y;
	else return gcd (y % x, x);
}

LL lcm (LL x, LL y) {
	return x * (y / gcd (x, y));
}

struct fraction {
	LL a, b;
	
	void read () {
		scanf ("%lld %lld", &a, &b);
		if (b < 0) a = -a, b = -b;
	}
	
	fraction operator + (const fraction x) const {
		fraction ans;
		ans.b = lcm (b, x.b);
		ans.a = x.a * (ans.b / x.b) + a * (ans.b / b);
		int temp = gcd (ans.a, ans.b);
		ans.a /= temp, ans.b /= temp;
		return ans;
	}
	
	fraction operator * (const fraction x) const {
		fraction ans;
		LL val1 = gcd (a, x.b), val2 = gcd (b, x.a);
		ans.a = a * x.a / val1;
		ans.b = b * x.b / val2;
		LL temp = gcd (ans.a, ans.b);
		ans.a /= temp;
		ans.b /= temp;
		return ans;
	}
	
	fraction operator - (const fraction tem) const {
		fraction x, y; x.a = a, x.b = b;
		y.a = -tem.a; y.b = tem.b;
		return x + y;
	}
	
	fraction operator / (const fraction tem) const {
	    fraction x, y; x.a = a; x.b = b;
	    y.a = tem.b; y.b = tem.a;
	    return x * y;
	}
	
	void write () {
	    if (a % b == 0) printf ("%lld", a / b);
		else printf ("%lld/%lld", a, b);
	}
}; 

int main () {
    fraction a, b; char type[100];
    a.read (); scanf ("%s", type + 1); b.read ();
    switch (type[1]) {
        case '+' : {
            a = a + b;
            a.write ();
        }
        case '-' : {
            a = a - b;
            a.write ();
        }
        case '*' : {
            a = a * b;
            a.write ();
        }
        case '/' : {
            a = a / b;
            a.write ();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值