zoukankan      html  css  js  c++  java
  • c++运算符重载+实例

    题目来源于:http://blog.csdn.net/sxhelijian/article/details/8723847

    【项目2拓展2(思考)】这个思考题吊一下大家的胃口:设定义了两个分数类的对象,如CFraction c1, c2。如果定义了int i,我们能用cin>>i>>j;在键盘上输入i和j的值,是否期望用cin>>c1>>c2;输入分数呢?同理,用cout<<c1<<c2;进行输出,可以吗?进一步,用c1+c2得到新的一个分数,用c1/c2实现两个分数的除法,以及其他加、减、比较、求倒数等也是理所当然的。实际上,要自定义分数类,这些直观的基本运算应该要实现,这叫运算符的重载。本任务中用amplify()给出了“放大”运算的一种实现方案,更多内容值得期待地。

    首先要说明:有的C++编译系统(如VC++6.0)没有完全实现C++标准,它所提供的后缀.h的头文件不支持把成员函数重载为友元函数,因此在VC++6.0,应把程序的头两行:

    #include <iosteam>

    using namespace std;

    改为一行:#include <iosteam.h>   //C语言的风格

    这是我自己编写的有关运算符重载的程序:

    #include <iostream.h>
    
    enum style_enum{zero, one, two, three };
    
    class CFraction
    {private:
    	int nume;  // 分子
    	int deno;  // 分母
     public:
    	 CFraction(int nu=0,int de=1):nume(nu),deno(de){}   //构造函数,初始化用
    
    	void simplify();				//化简(使分子分母没有公因子)
    
    	//输入输出重载
    	friend ostream& operator<<(ostream &out,const CFraction &cf);
        friend istream& operator>>(istream &in,CFraction &cf);
        
        //加减乘除,结果需要化简
    	friend CFraction operator+(const CFraction &lcf,const CFraction &rcf);
    	friend CFraction operator-(const CFraction &lcf,const CFraction &rcf);
    	friend CFraction operator*(const CFraction &lcf,const CFraction &rcf);
    	friend CFraction operator/(const CFraction &lcf,const CFraction &rcf);
        
    	//关系运算符
    	friend bool operator>(const CFraction &lcf,const CFraction &rcf);
    	friend bool operator<(const CFraction &lcf,const CFraction &rcf);
    	friend bool operator>=(const CFraction &lcf,const CFraction &rcf);
    	friend bool operator<=(const CFraction &lcf,const CFraction &rcf);
    	friend bool operator==(const CFraction &lcf,const CFraction &rcf);
    	friend bool operator!=(const CFraction &lcf,const CFraction &rcf);
    
    
    	//取+、-一目运算符
    	CFraction operator+();
    	CFraction operator-();
    };
    
    
    
    void CFraction::simplify()
    {
    	int v1 = nume;
    	int v2 = deno;
    	while(v2)
    	{
    		int temp = v2;
    		v2 = v1 % v2;
    		v1 = temp;
    	}
    	nume /= v1;
    	deno /= v1;
    	if(deno < 0)
    	{
    		deno = -deno;
    		nume = -nume;
    	}
    }
    
    //输出重载
    ostream& operator<<(ostream &out,const CFraction &cf)
    {
    	out << cf.nume << '/' << cf.deno;
    	return out;
    }
    
    //输入重载
    istream& operator>>(istream &in,CFraction &cf)
    {
    	char ch;
    	while(1)
    	{
    		in >> cf.nume >> ch >> cf.deno;
    		if(cf.deno == 0)
    			cerr << "分母为0请重新输入\n";
    		else if(ch != '/')
    			cerr << "格式错误(形如m/n)请重新输入\n";
    		else break;
    	}
    	return in;
    }
    //加法重载
    CFraction operator+(const CFraction &lcf,const CFraction &rcf)
    {
    	CFraction cf;
    	cf.nume = lcf.nume*rcf.deno + lcf.deno*rcf.nume;
    	cf.deno = lcf.deno*rcf.deno;
    	cf.simplify();
    	return cf;
    }
    //减法重载
    CFraction operator-(const CFraction &lcf,const CFraction &rcf)
    {
    	CFraction cf;
    	cf.nume = lcf.nume*rcf.deno - rcf.nume*lcf.deno;
    	cf.deno = lcf.deno*rcf.deno;
    	cf.simplify();
    	return cf;
    }
    //乘法重载
    CFraction operator*(const CFraction &lcf,const CFraction &rcf)
    {
    	CFraction cf;
    	cf.nume = lcf.nume*rcf.nume;
    	cf.deno = lcf.deno*rcf.deno;
    	cf.simplify();
    	return cf;
    }
    //除法重载
    CFraction operator/(const CFraction &lcf,const CFraction &rcf)
    {
    	CFraction cf;
    	cf.nume = lcf.nume*rcf.deno;
    	cf.deno = lcf.deno*rcf.nume;
    	cf.simplify();
    	return cf;
    }
    //取正重载
    CFraction CFraction::operator+()
    {
    	simplify();
    	if(nume < 0)
    		nume = -nume;
    	return *this;
    }
    //取负重载
    CFraction CFraction::operator-()
    {
    	simplify();
    	nume = -nume;
    	return *this;
    }
    
    
    //大于号重载
    bool operator>(const CFraction &lcf,const CFraction &rcf)
    {
    	int l_nume = lcf.nume * rcf.deno;
    	int r_nume = rcf.nume * lcf.deno;
    	int common_deno = lcf.deno * rcf.deno;
    	if((l_nume-r_nume) * common_deno > 0) return true;
    	return false;
    }
    
    //小于号重载
    bool operator<(const CFraction &lcf,const CFraction &rcf)
    {
    	return !(lcf > rcf);
    }
    
    //等于重载
    bool operator==(const CFraction &lcf,const CFraction &rcf)
    {
    	return lcf.nume==rcf.nume && lcf.deno == rcf.deno;
    }
    
    //不等于重载
    bool operator!=(const CFraction &lcf,const CFraction &rcf)
    {
    	return !(lcf==rcf);
    }
    
    //
    bool operator>=(const CFraction &lcf,const CFraction &rcf)
    {
    	if(lcf < rcf) return false;
    	return true;
    }
    
    //
    bool operator<=(const CFraction &lcf,const CFraction &rcf)
    {
    	if(lcf > rcf) return false;
    	return true;
    }
    
    int main()
    {
    	CFraction cf1;
    	CFraction cf2;
    	cin >> cf1 >> cf2;
    	cout << "cf1: "<< cf1 << '\t' << "cf2: "<< cf2 << endl
    		 << "cf1 + cf2 : " << cf1+cf2 << endl
    		 << "cf1 - cf2 : " << cf1-cf2 << endl
    		 << "cf1 * cf2 : " << cf1*cf2 << endl
    		 << "cf1 / cf2 : " << cf1/cf2 << endl
    		 << " +cf1 :     " << +cf1    << endl
    		 << " -cf1 :     " << -cf1    << endl
    		 << " +cf2 :   " << +cf2    << endl
    		 << " -cf2 :     " << -cf2    << endl
    		 << " cf1 > cf2? 1/YES 0/NO " << (cf1 > cf2) << endl
    		 << " cf1 < cf2? 1/YES 0/NO " << (cf1 < cf2) << endl
    		 << " cf1 == cf2? 1/YES 0/NO " << (cf1 == cf2) << endl
    		 << " cf1 != cf2? 1/YES 0/NO " << (cf1 != cf2) << endl
    		 << " cf1 >= cf2? 1/YES 0/NO " << (cf1 >= cf2) << endl
    		 << " cf1 <= cf2? 1/YES 
    
       
    	return 0;
    }


    贺老师的c++题目对我学习很有帮助,自己以前也学过,为了巩固一下自己所学,找了一些自己以前还不是很明白的题目来回顾c++方面的知识;话说要再把书拿起来(c++primer)看一遍是不太现实了,幸好有像贺老师这样愿意帮助他人学习的好老师,很感谢。而且他出题也是有讲究的。希望广大的朋友们也能从中受益。

    参考资料:http://blog.csdn.net/sxhelijian/article/details/7467433

                        http://blog.csdn.net/zollty/article/details/6697174

                       http://blog.csdn.net/sxhelijian/article/details/8723847

  • 相关阅读:
    【Java集合】试读LinkedList源码
    【Java集合】试读ArrayList源码
    类加载器ClassLoader的理解
    Spring中Bean的不同配置方式
    Spring中Bean的生命周期
    关于反射的一点理解
    Vector与ArrayList 的理解
    java多态的实现原理(JVM调用过程)(综合多篇文章,参考见文末)
    并发编程的模型分类(转载于https://link.zhihu.com/?target=http%3A//www.54tianzhisheng.cn/2018/02/28/Java-Memory-Model/)强烈推荐!
    Thread线程类
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/2993280.html
Copyright © 2011-2022 走看看