第9周任务3
来源:互联网 发布:淘宝收货时间最迟 编辑:程序博客网 时间:2024/05/20 00:48
#include <iostream> using namespace std; #include "jun.h"class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(){nume=0;deno=0;} CFraction(int n,int d) :nume(n),deno(d){} CFraction operator +(CFraction &x); CFraction operator -(CFraction &x); CFraction operator *(CFraction &x); CFraction operator /(CFraction &x); bool operator >(CFraction &x); bool operator <(CFraction &x); //构造函数及运算符重载的函数声明 bool operator =(CFraction &x); bool operator >=(CFraction &x); bool operator <=(CFraction &x); bool operator !=(CFraction &x);//死记住开头的形式, //CFraction operator-();///////////////////////////////////////////////// int f(int,int); int g(int,int); void display(); friend ostream& operator << (ostream&,CFraction&);friend istream& operator >> (istream&,CFraction&);}; CFraction CFraction::operator +(CFraction &x) { CFraction c; int t=f(deno,x.deno); c.nume=nume*(t/deno)+(t/x.deno)*x.nume; c.deno=t; t=g(c.nume,c.deno); c.nume=c.nume/t; c.deno=c.deno/t; return c; } CFraction CFraction::operator -(CFraction &x) { CFraction c; int t=f(deno,x.deno); c.nume=nume*(t/deno)-(t/x.deno)*x.nume; c.deno=t; t=g(c.nume,c.deno); c.nume=c.nume/t; c.deno=c.deno/t; return c; } CFraction CFraction::operator *(CFraction &x) { CFraction c; int t; c.nume=nume*x.nume; c.deno=deno*x.deno; t=g(c.nume,c.deno); c.nume=c.nume/t; c.deno=c.deno/t; return c; } CFraction CFraction::operator /(CFraction &x) { CFraction c; int t; c.nume=nume*x.deno; c.deno=deno*x.nume; t=g(c.nume,c.deno); c.nume=c.nume/t; c.deno=c.deno/t; return c; } bool CFraction::operator >(CFraction &x) { //bool flag=true; CFraction c; int t=f(deno,x.deno); c.nume=nume*(t/deno)-(t/x.deno)*x.nume; if(c.nume>0) { return true; } else { //flag=false; return false; } } bool CFraction::operator <(CFraction &x) { //bool flag=true; int t=f(deno,x.deno); CFraction c; c.nume=nume*(t/deno)-(t/x.deno)*x.nume; if(c.nume<0) { return true; } else { //flag=false; return false; } } bool CFraction::operator =(CFraction &x) { //bool flag=true; int t=f(deno,x.deno); CFraction c; c.nume=nume*(t/deno)-(t/x.deno)*x.nume; if(c.nume=0) { return true; } else { //flag=false; return false; } } bool CFraction::operator >=(CFraction &x) { //bool flag=false; int t=f(deno,x.deno); CFraction c; c.nume=nume*(t/deno)-(t/x.deno)*x.nume; if(c.nume<0) { return false; } else { //flag=true; return true; } } bool CFraction::operator <=(CFraction &x) { //bool flag=false; int t=f(deno,x.deno); CFraction c; c.nume=nume*(t/deno)-(t/x.deno)*x.nume; if(c.nume>0) { return false; } else { //flag=true; return true; } } bool CFraction::operator !=(CFraction &x)//注意开头的形式 { //bool flag=false; int t=f(deno,x.deno); CFraction c; c.nume=nume*(t/deno)-(t/x.deno)*x.nume; if(c.nume==0) { return false; } else { //flag=true; return true; } } /*CFraction CFraction:: operator -() { //*this=this*(-1); return(0- *this); }*/ int CFraction::f(int x,int y) { int t,p,r; if(x<y) { t=x; x=y; y=t; } p=x*y; while(y!=0) { r=x%y; x=y; y=r; } return p/x; } int CFraction:: g(int x,int y) { int t,p,r; if(x<y) { t=x; x=y; y=t; } p=x*y; while(y!=0) { r=x%y; x=y; y=r; } return x; } void CFraction:: display() { cout<<nume<<'/'<<deno; } void main() { //CFraction x1(3,12),x2(1,3); CFraction x1,x2; cin>>x1>>x2; cout<<"x1="; cout<<x1<<endl; x1.display(); cout<<endl; cout<<"x2="; cout<<x2<<endl; x2.display(); cout<<endl; cout<<"x1+x2=:"; cout<<(x1+x2); cout<<endl; cout<<"x1-x2=:"; cout<<(x1-x2); cout<<endl; cout<<"x1*x2=:"; cout<<(x1*x2); cout<<endl; cout<<"x1/x2=:"; cout<<(x1/x2); cout<<endl; if(x1<x2) cout<<"x1<x2"; cout<<endl; if(x1!=x2) cout<<"x1!=x2"; }ostream& operator << (ostream& output,CFraction&x){ output<<x.nume<<'/'<<x.deno; return output;}istream& operator >> (istream& input,CFraction&x){ cout<<"请输入数值:"; input>>x.nume>>x.deno; return input;}
感悟:经过定义“<<"和”》“之后发现,这样可以使程序更加通俗易懂简单,且不用再定义输出函数。
- 第9周任务3
- 第9周任务3
- 第9周任务
- 第9周任务1
- 第9周任务2
- 第9周任务1
- 第9周任务2
- 第9周任务2
- 第9周任务5
- 第9周任务4
- 第4周任务3
- 第6周任务3
- 第7周任务3
- 第13周任务3
- 第13周任务3
- 第16周任务3
- 第9周-任务5- 一元一次方程类
- 第9周任务2-个人所得税计算器
- ogg不能实施初始化数据加载_OCI Error ORA-01031: insufficient privileges (status = 1031)
- ubuntu在进行算数运算时提示Syntax error: Bad for loop variable解决办法
- file_exists中文名失败问题
- java synchronized
- Facebook 对 Google 的威胁在哪?
- 第9周任务3
- 使用Playfair密码变种加密字符串
- Android高手进阶教程(二十二)之---Android中几种图像特效处理的集锦!!
- 用jpcap读取pcap抓包文件并做简单的过滤显示
- 微软企业库缓存使用
- 无题
- 过去的一年
- 夺命的中国IT互联网行业
- 留着