实现分数类类中的运算符重载
来源:互联网 发布:弗兰克赫兹实验数据 编辑:程序博客网 时间:2024/04/30 02:40
/** 程序的版权和版本声明部分:* Copyright (c) 2013, 烟台大学计算机学院* All rights reserved.* 文件名称:test.cpp* 作 者:周经纬* 完成日期:2014年 4月 16日* 版 本 号:v12.1* 实现分数类类中的运算符重载,* 输入描述:无* 问题描述:。* 程序输出:* 问题分析:略* 算法设计:略*/#include<iostream>#include<Cmath>#include<cstdlib>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+(const CFraction &c); CFraction operator-(const CFraction &c); CFraction operator*(const CFraction &c); CFraction operator/(const CFraction &c); bool operator>(const CFraction &c); bool operator<(const CFraction &c); bool operator==(const CFraction &c); bool operator!=(const CFraction &c); bool operator>=(const CFraction &c); bool operator<=(const CFraction &c);};void CFraction::simplify(){ int m,n,r; n=deno; m=nume; while(r=m%n) { m=n; n=r; } deno/=n; nume/=n; if (deno<0) { deno=-deno; nume=-nume; }}void CFraction::display(){ cout<<"("<<nume<<"/"<<deno<<")";}CFraction CFraction::operator+(const CFraction &c){ CFraction T; T.nume=nume*c.deno+c.nume*deno; T.deno=deno*c.deno; T.simplify(); return T;}CFraction CFraction::operator-(const CFraction &c){ CFraction T; T.nume=nume*c.deno-c.nume*deno; T.deno=deno*c.deno; T.simplify(); return T;}CFraction CFraction::operator*(const CFraction &c){ CFraction T; T.nume=nume*c.nume; T.deno=deno*c.deno; T.simplify(); return T;}CFraction CFraction::operator/(const CFraction &c){ CFraction T; T.nume=nume*c.deno; T.deno=deno*c.nume; T.simplify(); return T;}bool CFraction::operator>(const CFraction &c){ int this_nume,c_nume,common_deno; this_nume=nume*c.deno; // 计算分数通分后的分子,同分母为deno*c.deno c_nume=c.nume*deno; common_deno=deno*c.deno; if ((this_nume>c_nume&&common_deno>0)||(this_nume<c_nume&&common_deno<0)) return true; // 将通分后的分子比较大小 return false;}bool CFraction::operator<(const CFraction &c){ int this_nume,c_nume,common_deno; this_nume=nume*c.deno; c_nume=c.nume*deno; common_deno=deno*c.deno; if ((this_nume-c_nume)*common_deno<0) return true; return false;}bool CFraction::operator==(const CFraction &c){ if (*this!=c) return false; return true;}bool CFraction::operator!=(const CFraction &c){ if (*this>c || *this<c) return true; return false;}bool CFraction::operator>=(const CFraction &c){ if (*this<c) return false; return true;}bool CFraction::operator<=(const CFraction &c){ if (*this>c) return false; return true;}int main(){ CFraction A(2,3),B(-5,6),s; cout<<"分数A=2/3 B=-5/6"<<endl; s=A+B; cout<<"A+B="; s.display(); cout<<endl; s=A-B; cout<<"A-B="; s.display(); cout<<endl; s=A*B; cout<<"A*B="; s.display(); cout<<endl; s=A/B; cout<<"A/B="; s.display(); cout<<endl; A.display(); if (A>B) cout<<"大于"; if (A<B) cout<<"小于"; if (A==B) cout<<"等于"; B.display(); cout<<endl; return 0;}
现在发现数学逻辑思维不行--------,分数比较卡壳了,老师写的看看思路这么清晰,
bool CFraction::operator<(const CFraction &c){ int this_nume,c_nume,common_deno; this_nume=nume*c.deno; c_nume=c.nume*deno; common_deno=deno*c.deno; if ((this_nume-c_nume)*common_deno<0) return true; return false;}这里的this-time,我以为跟别的参数似的可以改呢,我改了想看看,结果报错了,我说怎么非得这个名呢,是this指针的原因吧,还是不太理解
0 0
- 实现分数类类中的运算符重载
- 8-3 实现分数类中的运算符重载
- 8-3 实现分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- 分数类中的运算符重载
- Java日期工具类
- 素色流年,静守安然
- spring学习笔记(5)--集合注入
- Java内存模型与线程规范(JSR 133)中文版
- 对样式overflow的学习
- 实现分数类类中的运算符重载
- 《黑马程序员》 TreeMap练习分析
- 桃花江是美人窝
- 深入浅出理解聚集索引和非聚集索引
- Android---TextView属性详解
- 编程语言 vs. 二次元美女
- 虚继承
- 2-OMAPL138开发板快速体验
- 计算 SMP IRQ Affinity