标签:namespace iostream class 类 c++
要求:
(2)在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。
/* *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=0,int d=1):nume(n),deno(d) {}; void setCFraction(int n,int d); void display(); CFraction operator+(); //取正一目运算 CFraction operator-(); //取反一目运算 friend CFraction change(CFraction &c); friend CFraction operator+(const CFraction &c1, const CFraction &c2); friend CFraction operator-(const CFraction &c1, const CFraction &c2); friend CFraction operator*(const CFraction &c1, const CFraction &c2); friend CFraction operator/(const CFraction &c1, const CFraction &c2); friend CFraction operator+(const int &a, const CFraction &c2); friend CFraction operator-(const int &a, const CFraction &c2); friend CFraction operator*(const int &a, const CFraction &c2); friend CFraction operator/(const int &a, const CFraction &c2); friend CFraction operator+(const CFraction &c1, const int &a); friend CFraction operator-(const CFraction &c1, const int &a); friend CFraction operator*(const CFraction &c1, const int &a); friend CFraction operator/(const CFraction &c1, const int &a); friend CFraction operator+=( CFraction &c1, const CFraction &c2); friend CFraction operator-=( CFraction &c1, const CFraction &c2); friend CFraction operator*=( CFraction &c1, const CFraction &c2); friend CFraction operator/=( CFraction &c1, const CFraction &c2); friend bool operator > (CFraction &t,CFraction &c); friend bool operator < (CFraction &t,CFraction &c); friend bool operator >= (CFraction &t,CFraction &c); friend bool operator <= (CFraction &t,CFraction &c); friend bool operator == (CFraction &t,CFraction &c); friend bool operator != (CFraction &t,CFraction &c); //赋值运算符的重载 friend bool operator > (const int &a, const CFraction &c); friend bool operator < (const int &a, const CFraction &c); friend bool operator >= (const int &a, const CFraction &c); friend bool operator <= (const int &a, const CFraction &c); friend bool operator == (const int &a, const CFraction &c); friend bool operator != (const int &a, const CFraction &c); friend bool operator > ( const CFraction &c,const int &a); friend bool operator < (const CFraction &c,const int &a); friend bool operator >= (const CFraction &c,const int &a); friend bool operator <= (const CFraction &c,const int &a); friend bool operator == (const CFraction &c,const int &a); friend bool operator != (const CFraction &c,const int &a); }; int gcd(int a,int b) { return a%b==0?b:gcd(b,a%b); } 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+() //取正一目运算 { return *this; } CFraction CFraction ::operator-() //取反一目运算 { CFraction p(-this->nume,this->deno); return p; } CFraction operator+(const CFraction &c1, const CFraction &c) { CFraction p; p.nume=c1.nume*c.deno+c1.deno*c.nume; p.deno=c1.deno*c.deno; return change(p); } CFraction operator-(const CFraction &c1, const CFraction &c) { CFraction p; p.nume=c1.nume*c.deno-c1.deno*c.nume; p.deno=c1.deno*c.deno; return change(p); } CFraction operator*(const CFraction &c1, const CFraction &c) { CFraction p; p.nume=c1.nume*c.nume; p.deno=c1.deno*c.deno; return change(p); } CFraction operator/(const CFraction &c1, const CFraction &c) { CFraction p; p.nume=c1.nume*c.deno; p.deno=c1.deno*c.nume; return change(p); } CFraction operator+(const int &a, const CFraction &c2) { CFraction p(a,1); return p+c2; } CFraction operator-(const int &a, const CFraction &c2) { CFraction p(a,1); return p-c2; } CFraction operator*(const int &a, const CFraction &c2) { CFraction p(a,1); return p*c2; } CFraction operator/(const int &a, const CFraction &c2) { CFraction p(a,1); return p/c2; } CFraction operator+(const CFraction &c1, const int &a) { CFraction p(a,1); return c1+p; } CFraction operator-(const CFraction &c1, const int &a) { CFraction p(a,1); return c1-p; } CFraction operator*(const CFraction &c1, const int &a) { CFraction p(a,1); return c1*p; } CFraction operator/(const CFraction &c1, const int &a) { CFraction p(a,1); return c1/p; } bool operator > (CFraction &t,CFraction &c) { return (t.nume*c.deno>t.nume*c.deno); } bool operator < (CFraction &t,CFraction &c) { return (t.nume*c.deno<t.nume*c.deno); } bool operator >= (CFraction &t,CFraction &c) { return !(t.nume*c.deno<t.nume*c.deno); } bool operator <= (CFraction &t,CFraction &c) { return !(t.nume*c.deno>t.nume*c.deno); } bool operator == (CFraction &t,CFraction &c) { return !(t>c||t<c); } bool operator != (CFraction &t,CFraction &c) { return (t>c||t<c); } bool operator > (const int &a, const CFraction &c) { return (a*c.deno)>c.nume; } bool operator< (const int &a, const CFraction &c) { return (a*c.deno)<c.nume; } bool operator >= (const int &a, const CFraction &c) { return (a*c.deno)>=c.nume; } bool operator <= (const int &a, const CFraction &c) { return (a*c.deno)<=c.nume; } bool operator == (const int &a, const CFraction &c) { return (a*c.deno)==c.nume; } bool operator != (const int &a, const CFraction &c) { return (a*c.deno)!=c.nume; } bool operator > ( const CFraction &c,const int &a) { return c.nume>(a*c.deno); } bool operator < (const CFraction &c,const int &a) { return c.nume<(a*c.deno); } bool operator >= (const CFraction &c,const int &a) { return c.nume>=(a*c.deno); } bool operator <= (const CFraction &c,const int &a) { return c.nume<=(a*c.deno); } bool operator == (const CFraction &c,const int &a) { return c.nume==(a*c.deno); } bool operator != (const CFraction &c,const int &a) { return c.nume!=(a*c.deno); } //赋值运算符的重载 CFraction operator+=( CFraction &c1, const CFraction &c) { c1.nume=c1.nume*c.deno+c1.deno*c.nume; c1.deno=c1.deno*c.deno; return change(c1); } CFraction operator-=( CFraction &c1, const CFraction &c) { c1.nume=c1.nume*c.deno-c1.deno*c.nume; c1.deno=c1.deno*c.deno; return change(c1); } CFraction operator*=( CFraction &c1, const CFraction &c) { c1.nume=c1.nume*c.nume; c1.deno=c1.deno*c.deno; return change(c1); } CFraction operator/=( CFraction &c1, const CFraction &c) { c1.nume=c1.nume*c.deno; c1.deno=c1.deno*c.nume; return change(c1); } 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(); CFraction x(1,3),y(-5,10),s; cout<<"分数x=1/3 y=-5/10"<<endl; s=+x+y; cout<<"+x+y="; s.display(); s=x-y; cout<<"x-y="; s.display(); s=x*y; cout<<"x*y="; s.display(); s=x/y; cout<<"x/y="; s.display(); s=-x+y; cout<<"-x+y="; s.display(); x.display(); if (x>y) cout<<"大于"<<endl; if (x<y) cout<<"小于"<<endl; if (x==y) cout<<"等于"<<endl; y.display(); cout<<endl; CFraction c1(5,3),c2; //以下建议在调试环境中完成测试, c2=c1+5; c2=5+c1; c2=c1-5; c2=5-c1; c2=c1*5; c2=5*c1; c2=c1/5; c2=5/c1; bool b; b=(c1>2); b=(2>c1); return 0; }
一直在尝试着用自己的方法去完成,一下午就为了做这个就这样过去了有点不甘心,但好久没有这样的感觉了,好爽!
标签:namespace iostream class 类 c++
原文地址:http://blog.csdn.net/wh201458501106/article/details/45581515