【项目3-分数类中的运算符重载】
(1)实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。可以在第4周分数类代码的基础上开始工作。
[cpp] view
plaincopyprint?
- class CFraction
- {
- private:
- int nume; // 分子
- int deno; // 分母
- public:
- //构造函数及运算符重载的函数声明
- };
- //重载函数的实现及用于测试的main()函数
/* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *dood luck *文件名称:d.cpp *作 者:张旺华 *完成日期:2015年5月7日 *版 本 号:v1.0 * */ #include <iostream> using namespace std; class CFraction { private: int nume; // 分子 int deno; // 分母 public: //构造函数及运算符重载的函数声明 CFraction(int n=1,int d=1); void setCFraction(int n,int d); void display(); friend CFraction change(CFraction &c); CFraction operator+(CFraction &c); CFraction operator-(CFraction &c); CFraction operator*(CFraction &c); CFraction operator/(CFraction &c); 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 operator+=(CFraction &c); CFraction operator-=(CFraction &c); CFraction operator*=(CFraction &c); CFraction operator/=(CFraction &c); }; int gcd(int a,int b) { return a%b==0?b:gcd(b,a%b); } CFraction::CFraction(int n,int d) { nume=n; deno=d; } void CFraction::setCFraction(int n,int d) { nume=n; deno=d; } CFraction change(CFraction &c) { int t,a,b; a=c.nume; b=c.deno; t=gcd(a,b); c.nume/=t; c.deno/=t; return c; } void CFraction::display() { if(nume!=0&&deno!=0) cout<<nume<<"/"<<deno<<endl; else if(nume==0&&deno!=0) cout<<"0"<<endl; else cout<<"error"<<endl; } CFraction CFraction::operator+(CFraction &c) { CFraction p; p.nume=nume*c.deno+deno*c.nume; p.deno=deno*c.deno; return change(p); } CFraction CFraction::operator-(CFraction &c) { CFraction p; p.nume=nume*c.deno-deno*c.nume; p.deno=deno*c.deno; return change(p); } CFraction CFraction::operator*(CFraction &c) { CFraction p; p.nume=nume*c.nume; p.deno=deno*c.deno; return change(p); } CFraction CFraction::operator/(CFraction &c) { CFraction p; p.nume=nume*c.deno; p.deno=deno*c.nume; return change(p); } bool CFraction::operator > (CFraction &t) { return (nume*t.deno>t.nume*deno); } bool CFraction::operator < (CFraction &t) { return (nume*t.deno<t.nume*deno); } bool CFraction::operator >= (CFraction &t) { return !(nume*t.deno<t.nume*deno); } bool CFraction::operator <= (CFraction &t) { return !(nume*t.deno>t.nume*deno); } bool CFraction::operator == (CFraction &t) { return !(nume*t.deno>t.nume*deno||nume*t.deno<t.nume*deno); } bool CFraction::operator != (CFraction &t) { return (nume*t.deno>t.nume*deno||nume*t.deno<t.nume*deno); } //赋值运算符的重载 CFraction CFraction::operator+=(CFraction &c) { nume=nume*c.deno+deno*c.nume; deno=deno*c.deno; return change(*this); } CFraction CFraction::operator-=(CFraction &c) { nume=nume*c.deno-deno*c.nume; deno=deno*c.deno; return change(*this); } CFraction CFraction::operator*=(CFraction &c) { nume=nume*c.nume; deno=deno*c.deno; return change(*this); } CFraction CFraction::operator/=(CFraction &c) { nume=nume*c.deno; deno=deno*c.nume; return change(*this); } int main() { CFraction t1(7,9),t2(11,9),t; cout<<"t1为:"; t1.display(); cout<<"t2为:"; t2.display(); cout<<"下面比较两个时间大小:\n"; 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; t=t1+t2; t.display(); t=t1-t2; t.display(); t=t1*t2; t.display(); t=t1/t2; t.display(); t1+=t2; t1.display(); t1-=t2; t1.display(); t1*=t2; t1.display(); t1/=t2; t1.display(); return 0; }
运行结果:
学习心得:正在追赶中,好好加油,但不要图块。
时间: 2024-10-01 20:08:51