码迷,mamicode.com
首页 > 编程语言 > 详细

分数模板(C++模板)

时间:2018-09-13 20:19:39      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:好的   gic   operation   logo   namespace   sse   gcd   template   type   

貌似没什么用。。。没有多少毒瘤题要输出分数吧。。。

可以套个兼容性比较好的高精度模板进来。

#include<bits/stdc++.h>
using namespace std;
namespace FlashHu{
#define RG register
#define I inline
#define R RG frac
#define OutputFormat 0
    template<typename T>
    T gcd(RG T a,RG T b){
        return b?gcd(b,a%b):a;
    }
    template<typename T>
    struct frac{
        T a,b;
        
        //initializations
        I frac(){
            a=(T)0;b=(T)1;
        }
        I frac(RG T x){
            a=x;b=(T)1;
        }
        I frac(RG T x,RG T y){
            a=x;b=y;
            if(b<0)a=-a,b=-b;
        }
        I frac&operator=(RG T x){
            a=x;b=1;
            return*this;
        }
        I double to_double(){
            return(double)a/b;
        }
        
        //arithmetic operation
        I frac rec(){
            assert(a);
            return(frac){b,a};
        }
        I frac operator-(){
            return(frac){-a,b};
        }
        I frac&operator++(){
            a+=b;
            return*this;
        }
        I frac&operator--(){
            a-=b;
            return*this;
        }
        I frac&operator+=(R x){
            RG T g=gcd(b,x.b);
            a=b/g*x.a+x.b/g*a;(b/=g)*=x.b;
            return*this;
        }
        I frac&operator-=(R x){
            return*this+=-x;
        }
        I frac&operator*=(R x){
            RG T g1=gcd(abs(a),x.b),g2=gcd(abs(x.a),b);
            (a/=g1)*=x.a/g2;(b/=g2)*=x.b/g1;
            return*this;
        }
        I frac&operator/=(R x){
            return*this*x.rec();
        }
        I frac friend operator+(R x,R y){
            RG T g=gcd(x.b,y.b);
            return(frac){x.b/g*y.a+y.b/g*x.a,x.b/g*y.b};
        }
        I frac friend operator-(R x,R y){
            return x+-y;
        }
        I frac friend operator*(R x,R y){
            RG T g1=gcd(abs(x.a),y.b),g2=gcd(abs(y.a),x.b);
            (x.a/=g1)*=y.a/g2;(x.b/=g2)*=y.b/g1;
            return x;
        }
        I frac friend operator/(R x,R y){
            return x*y.rec();
        }
        
        //logic operations
        I bool operator!(){
            return a;
        }
        I bool friend operator&&(R x,R y){
            return x.a&&y.a;
        }
        I bool friend operator||(R x,R y){
            return x.a||y.a;
        }
        I bool friend operator==(R x,R y){
            return x.a==y.a&&x.b==y.b;
        }
        I bool friend operator!=(R x,R y){
            return x.a!=y.a||x.b!=y.b;
        }
#define logop(op)                                                       I bool friend operator op(R x,R y){                                 return (x+-y).a op 0;}
        logop(<);logop(>);logop(<=);logop(>=);
        
        //input and output
        I friend istream&operator>>(RG istream&is,R&x){
            x=0;
            static string s;is>>s;
            RG int pos=s.find_first_of(‘/‘);
            if(~pos)s[pos]=‘ ‘;
            istringstream(s)>>x.a>>x.b;
            if(x.b<0)x.a=-x.a,x.b=-x.b;
            RG T g=gcd(x.a,x.b);
            x.a/=g;x.b/=g;
            return is;
        }
        I friend ostream&operator<<(RG ostream&os,R x){
            os<<x.a;
            if(OutputFormat||x.b!=1)os<<‘/‘<<x.b;
            assert(x.b);
            return os;
        }
    };
#undef logop
#undef RG
#undef I
#undef R
#undef OutputFormat
}

分数模板(C++模板)

标签:好的   gic   operation   logo   namespace   sse   gcd   template   type   

原文地址:https://www.cnblogs.com/flashhu/p/9642158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!