关闭

实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算

157人阅读 评论(0) 收藏 举报
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作    者:郭辉
*完成时间:2016年6月1日
*版 本 号:v1.0
*
*问题描述:实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算
*输入描述:无。
*程序输出:分数。
*/
#include<iostream>
using namespace std;
class CFraction
{
private:
	int nume;//fenzi
	int deno;//fenmu
public:
	CFraction(int a=0,int b=0);

	void show();
	CFraction operator+(CFraction &c);
	CFraction operator-(CFraction &c);
	CFraction operator*(CFraction &c);
	CFraction operator/(CFraction &c);
	bool  operator>(CFraction &c);
	bool  operator<(CFraction &c);
	bool operator==(CFraction &c);
	bool  operator>=(CFraction &c);
	bool  operator<=(CFraction &c);
	bool  operator!=(CFraction &c);
};

bool  CFraction::operator>(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1>zi2)
		return true;
	else
		return false;


}
bool CFraction::operator<(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1< zi2)
		return true;
	else
		return false;


}
bool CFraction::operator==(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1==zi2)
		return true;
	else
		return false;


}
bool  CFraction::operator<=(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1<=zi2)
		return true;
	else
		return false;


}
bool  CFraction::operator>=(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1>=zi2)
		return true;
	else
		return false;
}
bool  CFraction::operator!=(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1!=zi2)
		return true;
	else
		return false;
}
CFraction::CFraction(int a,int b)
{
	nume=a;
	deno=b;
}
CFraction CFraction::operator+(CFraction &c)
{
	int mu,zi;
	mu=deno*c.deno;
	zi=nume*c.deno+c.nume*deno;
	CFraction t(zi,mu);
	return t;
}
CFraction CFraction::operator-(CFraction &c)
{
	int mu,zi;
	mu=deno*c.deno;
	zi=nume*c.deno-c.nume*deno;
	CFraction t(zi,mu);
	return t;
}
CFraction CFraction::operator*(CFraction &c)
{
	int mu,zi;
	mu=deno*c.deno;
	zi=nume*c.nume;
	CFraction t(zi,mu);
	return t;
}
CFraction CFraction::operator/(CFraction &c)
{
	int mu,zi;
	mu=deno*c.nume;
	zi=nume*c.deno;
	CFraction t(zi,mu);
	return t;
}
void CFraction::show()
{int t,m,r,n;
	m=deno;
	n=nume;
	if(deno<nume)
	{
		t=m;
		m=n;
		n=t;
	}

	while(r=m%n)
    {
        m=n;
        n=r;
    }
	deno=deno/n;
	nume=nume/n;
	cout<<nume<<"/"<<deno<<endl;

}
int main()
{
	CFraction  c1(1,2),c2(2,3),c3;
	c3=c1+c2;
	c3.show();
	c3=c1-c2;
	c3.show();
	c3=c1*c2;
	c3.show();
	c3=c1/c2;
	c3.show();
	if(c1>c2)
		cout<<"c1>c2"<<endl;
	if(c1<c2)
		cout<<"c1<c2"<<endl;
	if(c1==c2)
		cout<<"c1==c2"<<endl;
	if(c1>=c2)
		cout<<"c1>=c2"<<endl;
	if(c1<=c2)
		cout<<"c1<=c2"<<endl;
	if(c1!=c2)
		cout<<"c1!=c2"<<endl;




}
运行结果:<img src="http://img.blog.csdn.net/20160601134606896" alt="" />

0
0

  相关文章推荐
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19762次
    • 积分:735
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论