zoukankan      html  css  js  c++  java
  • C++走向远洋——54(项目一2、分数类的重载、取倒数)

    */
     * Copyright (c) 2016,烟台大学计算机与控制工程学院
     * All rights reserved.
     * 文件名:text.cpp
     * 作者:常轩
     * 微信公众号:Worldhello
     * 完成日期:2016年5月25日
     * 版本号:V1.0
     * 问题描述:分数类的重载取倒数
     * 程序输入:无
     * 程序输出:见运行结果
     */
    #include<iostream>
    #include<Cmath>
    using namespace std;
    
    class CFraction{
    private:
    	int nume;   //分子
    	int deno;   //分母
    public:
    	CFraction(int nu=0,int de=0);
    	CFraction operator+(const CFraction &n);  //分数相加
    	CFraction operator-(const CFraction &n);  //分数相减
    	CFraction operator*(const CFraction &n);  //分数相乘
    	CFraction operator/(const CFraction &n);  //分数相除
    	void display();                           //输出分数
    	void simplify();                          //分数化简
        bool operator>(const CFraction &c);
        bool operator<(const CFraction &c);
        bool operator==(const CFraction &c);
        bool operator!=(const CFraction &c);
        bool operator>=(const CFraction &c);
        bool operator<=(const CFraction &c);
    	CFraction operator+();  //取正一目运算
        CFraction operator-();  //取反一目运算
    	CFraction operator~();  //分数取倒数
    };
    
    CFraction::CFraction(int nu,int de)                     //构造函数
    {
    	nume=nu;
    	deno=de;
    }
    void CFraction::display()                               //输出函数
    {
    	cout<<nume<<"/"<<deno<<endl;
    }
    void CFraction::simplify()                              //分数化简
    {
        int m,n,r;
        n=fabs(deno);
        m=fabs(nume);
    	if(nume==0)
    	  deno=0;
    	else{
        while(r=m%n)    // 求m,n的最大公约数
        {
            m=n;
            n=r;
        }
        deno/=n;       // 化简
        nume/=n;
        if (deno<0)    // 将分母转化为正数
        {
            deno=-deno;
            nume=-nume;
        }
    	}
    }
    CFraction CFraction::operator +(const CFraction &n)     //定义分数相加
    {
    	CFraction t;
    	t.deno=this->deno*n.deno;
    	t.nume=this->nume*n.deno+n.nume*this->deno;
    	t.simplify();//化简
    	return t;
    }
    CFraction CFraction::operator -(const CFraction &n)     //定义分数相减
    {
    	CFraction t;
    	t.deno=this->deno*n.deno;
    	t.nume=this->nume*n.deno-n.nume*this->deno;
    	t.simplify();//化简
    	return t;
    }
    CFraction CFraction::operator *(const CFraction &n)     //定义分数相乘
    {
    	CFraction t;
    	t.deno=n.deno*this->deno;
    	t.nume=n.nume*this->nume;
        t.simplify();//化简
    	return t;
    }
    CFraction CFraction::operator /(const CFraction &n)     //定义分数相除
    {
    	CFraction t;
    	t.deno=n.nume*this->deno;
    	t.nume=n.deno*this->nume;
    	t.simplify();//化简
    	return t;
    }
    
    //比较运算符重载
    bool CFraction::operator >(const CFraction &c)        //  >重载
    {
     int this_nume,c_nume,common_deno;
        this_nume=nume*c.deno;        // 计算分数通分后的分子,同分母为deno*c.deno
        c_nume=c.nume*deno;
        common_deno=deno*c.deno;
        if ((this_nume-c_nume)*common_deno>0) return true;
        return false;
    }
    bool CFraction::operator<(const CFraction &c)
    {    
        int this_nume,c_nume,common_deno;
        this_nume=nume*c.deno;
        c_nume=c.nume*deno;
        common_deno=deno*c.deno;
        if ((this_nume-c_nume)*common_deno<0) return true;
        return false;
    }
    
    // 分数比较大小
    bool CFraction::operator==(const CFraction &c)
    {
        if (*this!=c) return false;
        return true;
    }
    
    // 分数比较大小
    bool CFraction::operator!=(const CFraction &c)
    {
        if (*this>c || *this<c) return true;
        return false;
    }
    
    // 分数比较大小
    bool CFraction::operator>=(const CFraction &c)
    {
        if (*this<c) return false;
        return true;
    }
    
    // 分数比较大小
    bool CFraction::operator<=(const CFraction &c)
    {
        if (*this>c) return false;
        return true;
    }
    // 分数取正号
    CFraction CFraction:: operator+()
    {
        return *this;
    }
    
    // 分数取负号
    CFraction CFraction:: operator-()
    {
        CFraction x;
        x.nume=-this->nume;
        x.deno=this->deno;
        return x;
    }
    CFraction CFraction::operator ~()
    {
    	CFraction t;
    	t.deno=this->nume;
    	t.nume=this->deno;
    	return t;
    }
    int main()
    {
    	CFraction a(2,3),b(1,3);
    	CFraction c;
    	c=a+b;
    	cout<<"c=";
    	c.display();
    	c=a*b;
    	cout<<"c=";
    	c.display();
    	c=a-b;
        cout<<"c=";
    	c.display();
    	c=a/b;
    	cout<<"c=";
    	c.display();
        if(a>b)
    		cout<<"a>b"<<endl;
    	cout<<"a=";
    	a.display();
    	cout<<"取倒数后a为:";
    	c=~a;
    	c.display();
        return 0;
    }



    运行结果:


  • 相关阅读:
    Android混淆
    Web开发人员应当知道的15个开源项目
    应用开发10种免费推广的方法
    (转载)Comparing C++ and C (Inheritance and Virtual Functions)
    JCTVC 会议输出文档
    HEVC bit depth increasment
    函数指针声明时的形参列表可以没有
    关于链接 Linkage
    二级指针和二维数组
    C 与 C++互相调用函数,变量
  • 原文地址:https://www.cnblogs.com/chxuan/p/8232200.html
Copyright © 2011-2022 走看看