项目三(选做)
来源:互联网 发布:法院网络司法拍卖 编辑:程序博客网 时间:2024/05/17 03:14
程序:
//实现分数类中的运算符重载,在分数类中可以完成和整型数的加减乘除(运算后再化简)、比较(6种关系)的运算#include <iostream>using namespace std;class CFraction;int tong(CFraction c1,CFraction c2); //通分void hua(int *de,int *nu); //化简class CFraction{private: int nume; // 分子 int deno; // 分母public: CFraction(int n=0,int d=0); friend int tong(CFraction c1,CFraction c2); //友元函数 friend void hua(int *de,int *nu); //为了保留计算所得值,用指针 void display() { cout<<nume<<"/"<<deno<<endl; } //构造函数及运算符重载的函数声明 CFraction operator+(CFraction &c); CFraction operator-(CFraction &c); CFraction operator*(CFraction &c); CFraction operator/(CFraction &c); CFraction operator+(int n); CFraction operator-(int ); CFraction operator*(int ); CFraction operator/(int ); bool operator > (CFraction &c); bool operator < (CFraction &c); bool operator >= (CFraction &c); bool operator <= (CFraction &c); bool operator == (CFraction &c); bool operator != (CFraction &c); //与int型的运算函数声明 friend CFraction operator+(int i,CFraction &c );//这里想了很多种方法,不知道该怎么写,看了贺老师的答案,才知道直接给一个CFraction类型的就可以 friend CFraction operator-(int i,CFraction &c ); friend CFraction operator*(int i,CFraction &c ); friend CFraction operator/(int i,CFraction &c ); friend bool operator > (int ,CFraction &c); friend bool operator < (int ,CFraction &c); friend bool operator >= (int ,CFraction &c); friend bool operator <= (int ,CFraction &c); friend bool operator == (int ,CFraction &c); friend bool operator != (int ,CFraction &c);};CFraction::CFraction(int n,int d){ nume=n; deno=d;}//重载函数的实现及用于测试的main()函数CFraction CFraction::operator+(CFraction &c){ int g,n; g=tong(*this,c); n=nume*(g/deno)+c.nume*(g/c.deno); hua(&g,&n); return CFraction(n,g); //直接返回对象并赋值}CFraction CFraction::operator-(CFraction &c){ int g,n; g=tong(*this,c); n=nume*(g/deno)-c.nume*(g/c.deno); hua(&g,&n); return CFraction(n,g);}CFraction CFraction::operator*(CFraction &c){ int d,n; d=deno*c.deno; n=nume*c.nume; hua(&d,&n); return CFraction(n,d);}CFraction CFraction::operator/(CFraction &c){ CFraction c1; int n; c1=c; n=c1.deno; c1.deno=c1.nume; c1.nume=n; return (*this)*c1;}int tong(CFraction c1,CFraction c2) //通分{ int t[8]={2,3,5,7,11,13,17,19},de1=c1.deno,de2=c2.deno,g=1; for(int i=0;i<8;i++) { while(de1%t[i]==0&&de2%t[i]==0) { de1/=t[i]; de2/=t[i]; g*=t[i]; } } g*=(de1*de2); if(g<0) { g=-g; //如果分数是负数 } return g;}void hua(int *de,int *nu){ int t[8]={2,3,5,7,11,13,17,19}; for(int i=0;i<8;i++) { while(*de%t[i]==0&&*nu%t[i]==0) { *de/=t[i]; *nu/=t[i]; } }}bool CFraction::operator > (CFraction &c){ bool g=false; double c1,c2; c1=nume/deno; c2=c.nume/c.deno; if(c1<0) { c1=-c1; } if(c2<0) { c2=-c2; } if(c1>c2) { g=true; } return g;}bool CFraction::operator < (CFraction &c){ bool g=false; double c1,c2; c1=nume/deno; c2=c.nume/c.deno; if(c1<c2) { g=true; } return g;}bool CFraction::operator >= (CFraction &c){ bool g=false; double c1,c2; c1=nume/deno; c2=c.nume/c.deno; if(c1>c2) { g=true; } return g;}bool CFraction::operator <= (CFraction &c){ bool g=false; double c1,c2; c1=nume/deno; c2=c.nume/c.deno; if(c1<c2) { g=true; } return g;}bool CFraction::operator == (CFraction &c){ bool g=false; double c1,c2; c1=nume/deno; c2=c.nume/c.deno; if(c1==c2) { g=true; } return g;}bool CFraction::operator != (CFraction &c){ bool g=false; double c1,c2; c1=nume/deno; c2=c.nume/c.deno; if(c1!=c2) { g=true; } return g;}//与int型的运算函数定义CFraction CFraction ::operator+(int n){ CFraction c1(this->deno*n,this->deno); return *this+c1;}CFraction CFraction ::operator-(int n){ CFraction c1(this->deno*n,this->deno); return *this-c1;}CFraction CFraction ::operator*(int n){ CFraction c1(this->deno*n,this->deno); return *this*c1;}CFraction CFraction ::operator/(int n){ CFraction c1(this->deno*n,this->deno); return *this/c1;}CFraction operator+(int n,CFraction &c){ CFraction c1(c.deno*n,c.deno); return c1+c;}CFraction operator-(int n,CFraction &c){ CFraction c1(c.deno*n,c.deno); return c1-c;}CFraction operator*(int n,CFraction &c){ CFraction c1(c.deno*n,c.deno); return c1*c;}CFraction operator/(int n,CFraction &c){ CFraction c1(c.deno*n,c.deno); return c1/c;}bool operator > (int n,CFraction &c){ bool g=false; CFraction c1(c.deno*n,c.deno); if(c1>c) { g=true; }}bool operator < (int n,CFraction &c){ bool g=false; CFraction c1(c.deno*n,c.deno); if(c1<c) { g=true; }}bool operator >= (int n,CFraction &c){ bool g=false; CFraction c1(c.deno*n,c.deno); if(c1>=c) { g=true; }}bool operator <= (int n,CFraction &c){ bool g=false; CFraction c1(c.deno*n,c.deno); if(c1<=c) { g=true; }}bool operator == (int n,CFraction &c){ bool g=false; CFraction c1(c.deno*n,c.deno); if(c1==c) { g=true; }}bool operator != (int n,CFraction &c){ bool g=false; CFraction c1(c.deno*n,c.deno); if(c1!=c) { g=true; }}int main(){ CFraction c1(3,4),c2(11,-6),c3; int n=5; cout<<"c1:"; c1.display(); cout<<"c2:"; c2.display(); cout<<endl; cout<<"c1+c2="; c3=c1+c2; c3.display(); cout<<"c1-c2="; c3=c1-c2; c3.display(); cout<<"c1*c2="; c3=c1*c2; c3.display(); cout<<"c1/c2="; c3=c1/c2; c3.display(); cout<<endl; cout<<"compare c1 and c2:"<<endl; if(c1>c2) { cout<<"c1>c2"<<endl; } if(c1<c2) { cout<<"c1<c2"<<endl; } if(c1>=c2) { cout<<"c1>=c2"<<endl; } if(c1<=c2) { cout<<"c1<=c2"<<endl; } if(c1==c2) { cout<<"c1==c2"<<endl; } if(c1!=c2) { cout<<"c1!=c2"<<endl; } cout<<endl; //与int型的运算 cout<<"num="<<n<<endl; cout<<"c1+n"; c3=c1+n; c3.display(); cout<<"c1-n="; c3=c1-n; c3.display(); cout<<"c1*n="; c3=c1*n; c3.display(); cout<<"c1/n="; c3=c1/n; c3.display(); cout<<endl; cout<<"n+c1="; c3=n+c1; c3.display(); cout<<"n-c1="; c3=n-c1; c3.display(); cout<<"n*c1="; c3=n*c1; c3.display(); cout<<"n/c1="; c3=n/c1; c3.display(); cout<<endl; cout<<"compare c1 and n:"<<endl; if(n>c1) { cout<<"c1<n"<<endl; } if(n<c1) { cout<<"c1>n"<<endl; } if(n>=c1) { cout<<"c1<=n"<<endl; } if(n<=c1) { cout<<"n>=c1"<<endl; } if(n==c1) { cout<<"c1==n"<<endl; } if(n!=c1) { cout<<"c1!=n"<<endl; } cout<<endl; return 0;}
结果:
体会:好麻烦,一开始想用模板做,可是发现不对,模板似乎做不成类和类,类和整数的共同处理。做整型那里时,不知道该怎么声明函数类型,看了贺老师的,原来一个类返回值的就可以搞定。
0 0
- 项目三(选做)
- 项目需求该怎么做(三)
- 做项目难,做产品更难(三)
- 补做-第六次实验-项目三
- 【结构体】项目四(选做)。
- 项目一(选做)。矩形
- 项目二(选做)深复制
- 2013第二周上机任务项目三(用结构体数组做计算)
- 第四周(项目四扩展三)——数组做数据成员<数据存入文件>
- 第三周 项目五-数组做数据成员(三)文件操作-数组
- 从零开始,跟我一起做jblog项目(三)从Maven到Gradle
- 委托--事件--基础以及在现在做的项目上的实际应用(三)
- SprintNBA项目模仿笔记(三)如何做一个优秀的tab标签-ViewPager联动
- 怎样做研究(三)
- RDLC做报表(三)
- 怎么做研究(三)
- 运用Scrum做项目管理真实案例之三
- 第二周 项目三 用结构体数据做运算
- javaIO流-1
- C++学习及总结
- Longest Substring Without Repeating Characters
- 网页前端开发:配色学的4张宝图
- The STL Vector Class 学习笔记
- 项目三(选做)
- 项目四。
- Add Two Numbers
- LA 4728 Squares 旋转卡壳
- mac 隐藏文件快捷键创建过程(图文并茂)
- DirectX与VS2010的配置
- 复数模板
- JQuery的Ajax跨域请求的解决方案
- 网站二级域名到底好不好