第八周任务3

01./* (程序头部注释开始)   
02.* 程序的版权和版本声明部分   
03.* Copyright (c) 2011, 烟台大学计算机学院学生    
04.* All rights reserved.   
05.* 文件名称:   
06.   
07.* 作    者:        王明星                   
08.* 完成日期:     2012    年4     月    8  日   
09.* 版 本 号:          
10.* 对任务及求解方法的描述部分   
11.* 输入描述:    
12.* 问题描述:    
13.* 程序输出:    
14.* 程序头部的注释结束   
15.*/  
#include<iostream>
using namespace std;
int gcd(int m, int n);  
class CFraction
{
private:
	int nume;  // 分子
	int deno;  // 分母
public://构造函数及运算符重载的函数声明
	CFraction(int nu=0,int de=0);
	void set(int nu=0,int de=0);
	void simplify();
	void output(int style=0);
	CFraction operator+(CFraction &t);
	CFraction operator-(CFraction &t);
	CFraction operator*(CFraction &t);
	CFraction operator/(CFraction &t);
	friend CFraction operator-(CFraction &t);//取反
	bool operator>(CFraction &t);
	bool operator<(CFraction &t);
	bool operator>=(CFraction &t);
	bool operator<=(CFraction &t);
	bool operator==(CFraction &t);
	bool operator!=(CFraction &t);
};
CFraction::CFraction(int nu,int de)
{
	if (de!=0) //如果不合适,改变值操作无效  
	{
		nume=nu;  
		deno=de;  
	}  
}  

void CFraction::simplify() //化简分数 
{ 
	int n=gcd(deno, nume);  
	deno/=n;     // 化简   
	nume/=n;  
}  

// 求m,n的最大公约数   
int gcd(int m, int n)  
{  
	int r;  
	if (m<n){r=m;m=n;n=r;}  
	while(r=m%n)  // 求m,n的最大公约数   
	{  
		m=n;  
		n=r;  
	}  
	return n;  
}  
void CFraction::output(int style)
{
	int n;
	switch(style)
	{
	case 0:
		cout<<nume<<'/'<<deno<<endl;break;
	case 1:
		n=gcd(nume,deno);
		cout<<nume/n<<'/'<<deno/n<<endl;break;
	case 2:
		n=gcd(nume,deno);
		nume=nume/n;
		deno=deno/n;
		cout<<nume/deno<<'('<<nume%deno<<'/'<<deno<<')'<<endl;break;
	default:cout<<nume<<'/'<<deno<<endl;
	}
}


void CFraction::set(int nu,int de)//定义构造函数
{  
	if (de!=0)  
	{  
		nume=nu;  
		deno=de;  
	}  
	else  
	{          
		cerr<<"初始化中发生错误,程序退出\n";  
		system("pause");  
		exit(0);  
	}  
}  
CFraction CFraction::operator+(CFraction &t)//分数的加法运算
{
	CFraction a;
	if(deno==t.deno)
	{
		a.deno=deno;
		a.nume=nume+t.nume;
	}
	else
	{
		a.deno=deno*t.deno;
		a.nume=nume*t.deno+deno*t.nume;
	}
	return a;
}

CFraction CFraction::operator-(CFraction &t)//分数的减法运算
{
	CFraction a;
	if(deno==t.deno)
	{
		a.deno=deno;
		a.nume=nume-t.nume;
	}
	else
	{
		a.deno=deno*t.deno;
		a.nume=nume*t.deno-deno*t.nume;
	}
	return a;
}


CFraction CFraction::operator*(CFraction &t)//分数的乘法运算
{
	CFraction a;
	a.deno=deno*t.deno;
	a.nume=nume*t.nume;
	return a;
}


CFraction CFraction::operator/(CFraction &t)//分数的除法运算
{
	CFraction a;
	a.deno=deno*t.nume;
	a.nume=nume*t.deno;
	return a;
}



CFraction operator-(CFraction &t)//取反
{
	return CFraction(-t.nume,t.deno);
}


bool CFraction::operator>(CFraction &t)//大于
{
	if (nume*t.deno>deno*t.nume)
		return true;
	else
		return false;
}


bool CFraction::operator<=(CFraction &t)//小于等于
{
	if (!operator>(t))
		return true;
	else
		return false;
}

bool CFraction::operator==(CFraction &t)//等于
{
	if (nume*t.deno==deno*t.nume)
		return true;
	else
		return false;
}

bool CFraction::operator<(CFraction &t)//小于
{
	if (nume*t.deno<deno*t.nume)
		return true;
	else
		return false;
}

bool CFraction::operator>=(CFraction &t)//大于等于
{
	if (!operator<(t))
		return true;
	else
		return false;
}

bool CFraction::operator!=(CFraction &t)//不等于
{
	if (!operator==(t))
		return true;
	else
		return false;
}

int main()
{
	CFraction t1(9,4),t2(7,6),t3;
	t3=t1*t2;
	cout<<"t1*t2=";
	t3.output();
	cout<<"约分后:";
	t3.output(1);
	cout<<"带分数形式:";
	t3.output(2);
	t3=t1-t2;
	cout<<"t1-t2=";
	t3.output(1);
	t3=t1+t2;
	cout<<"t1+t2=";
	t3.output(1);
	t3=t1/t2;
	cout<<"t1/t2=";
	t3.output(1);
	t3=-t1;
	t3.output(1);
	if (t1>t2) cout<<"t1>t2"<<endl;
	if (t1<t2) cout<<"t1<t2"<<endl;
	if (t1==t2) cout<<"t1=t2"<<endl; 
	if (t1!=t2) cout<<"t1≠t2"<<endl;
	if (t1>=t2) cout<<"t1≥t2"<<endl;
	if (t1<=t2) cout<<"t1≤t2"<<endl;
	system("pause");
	return 0;


}

上机感言:运算符的重载竟使类的功能如此强大呀!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值