用c++实现分数(fraction)类

这个想法已经有3周,于是今天将它实现了。

Step 1基础:

用c++实现分数(fraction)类

我们需要定义一个class——fraction,全部属性定义为public

class fraction{
public:

};

现在,让我们添加2个元素,分子和分母——fz和fw

Step 1.1添加分子分母:

class fraction{
public:
	int fz,fw;
};

Step 2重载运算符:

首先是约分

static fraction teasy(fraction ans){
	int yf=__gcd(ans.fz,ans.fw);
	ans.fz/=yf;
	ans.fw/=yf;
	return ans;
}

接着是lcm(最小公倍数)

static int lcm(int x,int y){
	return x*y/__gcd(x,y);
}

其次是加减乘除,很简单


friend fraction operator+(fraction x,fraction y){
	fraction ans;
	ans.fz=x.fz*y.fw+y.fz*x.fw;
	ans.fw=x.fw*y.fw;
	return teasy(ans);
}
friend fraction operator-(fraction x,fraction y){
	fraction ans;
	ans.fz=x.fz*y.fw-y.fz*x.fw;
	ans.fw=x.fw*y.fw;
	return teasy(ans);
}
friend fraction operator*(fraction x,fraction y){
	fraction ans;
	ans.fz=x.fz*y.fz;
	ans.fw=x.fw*y.fw;
	return teasy(ans);
}
friend fraction operator/(fraction x,fraction y){
	fraction ans;
	swap(y.fz,y.fw);	
    ans.fz=x.fz*y.fz;
	ans.fw=x.fw*y.fw;
		return teasy(ans);
}
friend bool operator>(fraction x,fraction y){
	int thard=lcm(x.fw,y.fw);
	x.fz=x.fz*thard/x.fw;
	y.fz=y.fz*thard/y.fw;
	if(x.fz>y.fz)return 1;
	else return 0;
}

当然,逻辑运算符也不能忘记

friend bool operator>(fraction x,fraction y){
	int thard=lcm(x.fw,y.fw);
	x.fz=x.fz*thard/x.fw;
	y.fz=y.fz*thard/y.fw;
	if(x.fz>y.fz)return 1;
	else return 0;
}
friend bool operator==(fraction x,fraction y){
	int thard=lcm(x.fw,y.fw);
	x.fz=x.fz*thard/x.fw;
	y.fz=y.fz*thard/y.fw;
	if(x.fz==y.fz)return 1;
	else return 0;
}
friend bool operator<(fraction x,fraction y){
	int thard=lcm(x.fw,y.fw);
	x.fz=x.fz*thard/x.fw;
	y.fz=y.fz*thard/y.fw;
	if(x.fz=(fraction x,fraction y){
	int thard=lcm(x.fw,y.fw);
	x.fz=x.fz*thard/x.fw;
	y.fz=y.fz*thard/y.fw;
	if(x.fz>=y.fz)return 1;
	else return 0;
}

输出函数


static void print(fraction x){
	if(x.fz==0)cout<<"0\n";
	else if(x.fw==1)cout<

n次方函数

static fraction fipow(fraction x,int y){
	fraction ans=x;
	for(int i=0;i

绝对值

static fraction frabs(fraction x){
	if(x.fz*x.fw<1){
		x.fz=abs(x.fz);
		x.fw=abs(x.fw);
	}
	return x;
}

完整code:

class fraction{
public:
	int fz,fw;
	static fraction teasy(fraction ans){
		int yf=__gcd(ans.fz,ans.fw);
		ans.fz/=yf;
		ans.fw/=yf;
		return ans;
	}
	static int lcm(int x,int y){
		return x*y/__gcd(x,y);
	}
	friend fraction operator+(fraction x,fraction y){
		fraction ans;
		ans.fz=x.fz*y.fw+y.fz*x.fw;
		ans.fw=x.fw*y.fw;
		return teasy(ans);
	}
	friend fraction operator-(fraction x,fraction y){
		fraction ans;
		ans.fz=x.fz*y.fw-y.fz*x.fw;
		ans.fw=x.fw*y.fw;
		return teasy(ans);
	}
	friend fraction operator*(fraction x,fraction y){
		fraction ans;
		ans.fz=x.fz*y.fz;
		ans.fw=x.fw*y.fw;
		return teasy(ans);
	}
	friend fraction operator/(fraction x,fraction y){
		fraction ans;
		swap(y.fz,y.fw);
		ans.fz=x.fz*y.fz;
		ans.fw=x.fw*y.fw;
		return teasy(ans);
	}
	friend bool operator>(fraction x,fraction y){
		int thard=lcm(x.fw,y.fw);
		x.fz=x.fz*thard/x.fw;
		y.fz=y.fz*thard/y.fw;
		if(x.fz>y.fz)return 1;
		else return 0;
	}
	friend bool operator==(fraction x,fraction y){
		int thard=lcm(x.fw,y.fw);
		x.fz=x.fz*thard/x.fw;
		y.fz=y.fz*thard/y.fw;
		if(x.fz==y.fz)return 1;
		else return 0;
	}
	friend bool operator<(fraction x,fraction y){
		int thard=lcm(x.fw,y.fw);
		x.fz=x.fz*thard/x.fw;
		y.fz=y.fz*thard/y.fw;
		if(x.fz=(fraction x,fraction y){
		int thard=lcm(x.fw,y.fw);
		x.fz=x.fz*thard/x.fw;
		y.fz=y.fz*thard/y.fw;
		if(x.fz>=y.fz)return 1;
		else return 0;
	}
	static fraction fipow(fraction x,int y){
		fraction ans=x;
		for(int i=0;i

测试:

test1:

test2:

test3:

test4:

总结:

代码难度不大,码量不大,作用不大(3“不大”代码)

版权声明:如无特殊标注,文章均来自网络,本站编辑整理,转载时请以链接形式注明文章出处,请自行分辨。

本文链接:https://www.shbk5.com/dnsj/74703.html