第八周任务三
2012-04-11 20:42
246 查看
#include <iostream> using namespace std; class CFraction { private: int nume; int deno; public: CFraction(int nu=0,int de=1):nume(nu),deno(de){} void simplify(); void display(); CFraction operator +(CFraction &c1); CFraction operator -(CFraction &c1); CFraction operator *(CFraction &c1); CFraction operator /(CFraction &c1); bool operator >(CFraction &c1); bool operator <(CFraction &c1); bool operator ==(CFraction &c1); bool operator !=(CFraction &c1); bool operator >=(CFraction &c1); bool operator <=(CFraction &c1); CFraction operator +(); CFraction operator -(); }; void CFraction::simplify() { int x; int max; if(nume>deno) x=nume; else x=deno; for(int i=2;i<x;i++) if(nume%i==0&&deno%i==0) { deno=deno/i; nume=nume/i; max=i; } } void CFraction::display() { cout<<"("<<nume<<"/"<<deno<<")"<<endl; } CFraction CFraction::operator +(CFraction &c1) { CFraction c2; c2.deno=deno*c1.deno; c2.nume=nume*c1.deno+c1.nume*deno; c2.simplify(); return (c2); } CFraction CFraction::operator -(CFraction &c1) { CFraction c2; c2.deno=deno*c1.deno; c2.nume=nume*c1.deno-c1.nume*deno; c2.simplify(); return (c2); } CFraction CFraction::operator *(CFraction &c1) { CFraction c2; c2.deno=deno*c1.deno; c2.nume=nume*c1.nume; c2.simplify(); return (c2); } CFraction CFraction::operator /(CFraction &c1) { CFraction c2; c2.deno=deno*c1.nume; c2.nume=nume*c1.deno; c2.simplify(); return (c2); } bool CFraction::operator >(CFraction &c1) { CFraction c2,c3; c2.nume=nume*c1.deno; c3.nume=c1.nume*deno; if(c2.nume>c3.nume) return true; else return false; } bool CFraction::operator <(CFraction &c1) { CFraction c2,c3; c2.nume=nume*c1.deno; c3.nume=c1.nume*deno; if(c2.nume<c3.nume) return true; else return false; } bool CFraction::operator ==(CFraction &c1) { if( operator >(c1)!=1&& operator <(c1)!=1) return true; else return false; } bool CFraction::operator !=(CFraction &c1) { if(operator >(c1)==1|| operator <(c1)==1) return true; else return false; } bool CFraction::operator >=(CFraction &c1) { if( operator <(c1)!=1) return true; else return false; } bool CFraction::operator <=(CFraction &c1) { if(operator >(c1)!=1) return true; else return false; } CFraction CFraction::operator +() { CFraction c2; c2.nume=nume; c2.deno=deno; return (c2); } CFraction CFraction::operator -() { CFraction c2; c2.nume=-nume; c2.deno=deno; return (c2); } int main() { 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; cout<<endl; y.display(); system("pause"); return 0; }
相关文章推荐
- 第八周实验指导任务1--实现复数类中的运算符重载
- 第八周任务一(运算符重载函数作为类成员函数和有员函数及其他)
- 第八周上机任务1-2
- 第八周任务-项目一(任务一成员函数的运算符重载)
- 第八周任务-项目一(任务三)
- 第八周任务3
- 第八周任务三 运算符重载 分数
- 第八周任务
- 第八周任务二
- 第八周任务—关于“园”的计算
- C#--第八周实验--任务8--使用移位运算来避免乘法运算。
- 第八周 任务一 方案一
- 第八周任务(一)
- 《第八周实验报告任务3》
- 第八周实验报告任务3 实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除,求反,比较6种关系的运算。
- 第八周项目一:用类的成员函数完成运算符的重载(任务一)
- 第八周任务(三)
- 第八周任务二——关于时间函数运算符的重载
- 第八周 任务三
- 第八周任务3