第9周实验报告3

实验目的:实现分数类中的运算符重载
* 程序头部注释开始
* 程序的版权和版本声明部分
* 烟台大学计算机学院学生
* 文件名称:         运算符重载
* 作    者:          胡斌                
* 完成日期:  2012 年 4月17日
* 版本号:     v1.0    

* 程序头部的注释结束(此处也删除了斜杠)

#include<iostream>    
using namespace std; 
class CFraction  
{  
private:  
    int nume; // 分子   
    int deno; // 分母   
public:      
    void Simplify(); //化简(使分子分母没有公因子) 
	CFraction operator + (CFraction  &c);
	CFraction operator - (CFraction  &c);
	CFraction operator * (CFraction  &c);
	CFraction operator / (CFraction  &c);

	CFraction operator + ();
	CFraction operator - ();

	bool operator > (CFraction &c);
	bool operator < (CFraction &c);
	bool operator >= (CFraction &c);
	bool operator <= (CFraction &c);
	bool operator == (CFraction &c);
	bool operator != (CFraction &c);

	friend ostream& operator << (ostream&, CFraction&);
    friend istream& operator >> (istream&, CFraction&);

};

ostream& operator << (ostream& output, CFraction& cf)
{
	cout << cf.nume << "/" << cf.deno << endl;
	return output;
}
istream& operator >> (istream& input, CFraction& cf)
{
	cout << "请输入要置分数的分子和分母:";
	input >> cf.nume >> cf.deno;
	return input;
}
  
void main()  
{  
    CFraction cf1, cf2, cf3;  
    cin >> cf1 >> cf2;
	cout << "cf1 = " << cf1;
    cout << "cf2 = " << cf2;

	cf3 = cf1 + cf2;
	cout << "cf1 + cf2 = " ;
    cf3.Simplify();

	cf3 = cf1 - cf2;
	cout << "cf1 - cf2 = " ;
	cf3.Simplify();

	cf3 = cf1 * cf2;
	cout << "cf1 * cf2 = ";
	cf3.Simplify();

	cf3 = cf1 / cf2;
	cout << "cf1 / cf2 = ";
	cf3.Simplify();

	cout << "对cf1取正得: ";
	cf1 = + cf1;
	cout << "对cf2取反得: ";
	cf2 = - cf2;

	if(cf1 > cf2) cout << "cf1 > cf2" << endl;
	if(cf1 < cf2) cout << "cf1 < cf2" << endl;
	if(cf1 == cf2) cout << "cf1 = cf2" << endl; 
	if(cf1 != cf2) cout << "cf1 ≠ cf2" << endl;
	if(cf1 >= cf2) cout << "cf1 ≥ cf2" << endl;
	if(cf1 <= cf2) cout << "cf1 ≤ cf2" << endl;

        system("pause");
}   
  
void CFraction::Simplify()  
{  
    int a[10], j = 0;  
    for(int i = 1; i <= nume; i++)  
    {  
        if(nume % i == 0)  
        {  
            a[j] = i;  
            j++;  
        }  
    }  
    //定义a[10]数组用于存储nume的因数   
    int b[10], m = 0;  
    for(int n = 1; n <= deno; n++)  
    {  
        if(deno % n == 0)  
        {  
            b[m] = n;  
            m++;  
        }  
    }  
    //定义b[10]数组用于存储deno的因数   
    for(int k = 0; k <= (j - 1); k++)  
    {  
        for(int p = 0; p <= (m - 1); p++)  
        {  
            if(a[k] == b[p])  
            {  
                nume = nume / a[k];  
                deno = deno / a[k];  
            }  
        }  
    }  
        cout << nume << "/" << deno << endl;  
} 

CFraction CFraction::operator + (CFraction  &c)
{
	CFraction C;
	C.deno = deno * c.deno;
	C.nume = nume * c.deno + c.nume * deno;
	return C;
}

CFraction CFraction::operator - (CFraction  &c)
{
	CFraction C;
	C.deno = deno * c.deno;
	if(nume * c.deno < c.nume * deno)
	{
        C.nume = -(c.nume * deno - nume * c.deno);
	}
	else
	    C.nume = nume * c.deno - c.nume * deno;
	return C;
}

CFraction CFraction::operator * (CFraction  &c)
{
	CFraction C;
	C.deno = deno * c.deno;
	C.nume = nume * c.nume;
	return C;
}
CFraction CFraction::operator / (CFraction  &c)
{
	CFraction C;
	C.deno = deno * c.nume ;
	C.nume = nume * c.deno;
	return C;
}

CFraction CFraction::operator - ()
{
	CFraction C;
	C.deno = deno;
	C.nume = nume;
	cout << "-(" << C.nume << "/" << C.deno << ")" << endl;
	return C;
}

CFraction CFraction::operator + ()
{
	CFraction C;
	C.deno = deno;
	C.nume = nume;
	cout << "+(" << C.nume << "/" << C.deno << ")" << endl;
	return C;
}

bool CFraction::operator > (CFraction &c)
{
	deno = deno * c.deno;
	c.deno = deno;
	if((nume * c.deno) > (c.nume * deno))
		return true;
	else
		return false;
}
bool CFraction::operator < (CFraction &c)
{
	deno = deno * c.deno;
	c.deno = deno;
	if((nume * c.deno) < (c.nume * deno))
		return true;
	else
		return false;
}
bool CFraction::operator == (CFraction &c)
{
	deno = deno * c.deno;
	c.deno = deno;
	if((nume * c.deno) == (c.nume * deno))
		return true;
	else
		return false;
}
bool CFraction::operator != (CFraction &c)
{
	deno = deno * c.deno;
	c.deno = deno;
	if((nume * c.deno) == (c.nume * deno))
		return false;
	else
		return true;
}
bool CFraction::operator >= (CFraction &c)
{
	if((nume * c.deno) >= (c.nume * deno))
		return true;
	else
		return false;
}
bool CFraction::operator <= (CFraction &c)
{
	if((nume * c.deno) <= (c.nume * deno))
		return true;
	else
		return false;
}


截图:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值