博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
课程设计__分数的计算
阅读量:7213 次
发布时间:2019-06-29

本文共 1998 字,大约阅读时间需要 6 分钟。

1、操作符重载

2、类的封装

#include 
#include
#include
using namespace std;int gcd(int m,int n)///求最大公约数{ if(n==0) return m; else return gcd(n,m%n);}class Fraction{public: Fraction(); Fraction(int a,int b):molecule(a),denominator(b) {} void setdata(); void display(); Fraction operator *(Fraction &a) { Fraction c; int temp1;///分子 int temp2;///分母 int L;///最大公约数 temp1=this->molecule*a.molecule; temp2=this->denominator*a.denominator; L=gcd(temp1,temp2); c.molecule=temp1/L; c.denominator=temp2/L; return c; } Fraction operator +(Fraction &a) { Fraction c; int temp1;///分子 int temp2;///分母 int L;///最大公约数 temp1=this->molecule*a.denominator+this->denominator*a.molecule; temp2=this->denominator*a.denominator; L=gcd(temp1,temp2); c.molecule=temp1/L; c.denominator=temp2/L; return c; } Fraction operator -(Fraction &a) { Fraction c; int temp1;///分子 int temp2;///分母 int L;///最大公约数 temp1=this->molecule*a.denominator-this->denominator*a.molecule; temp2=this->denominator*a.denominator; L=gcd(temp1,temp2); c.molecule=temp1/L; c.denominator=temp2/L; return c; } Fraction operator /(Fraction &a) { Fraction c; int temp1;///分子 int temp2;///分母 int L;///最大公约数 temp1=this->molecule*a.denominator; temp2=this->denominator*a.molecule; L=gcd(temp1,temp2); c.molecule=temp1/L; c.denominator=temp2/L; return c; }private: int molecule;///分子 int denominator;///分母};Fraction::Fraction(){ molecule=0; denominator=1;}void Fraction::setdata(){ cin>>molecule>>denominator;}void Fraction::display(){ cout<
<<"/"<
<

 

转载于:https://www.cnblogs.com/TreeDream/p/5247486.html

你可能感兴趣的文章
单晶组件的平价上网路线
查看>>
路由器LED闪灯泄露数据
查看>>
微软公司将在英国开设三个数据中心
查看>>
解析弱电安防监控管理系统的安装技术重点
查看>>
Oracle新一代数据库机 助所有规模企业迈向云端
查看>>
2017年中国大功率UPS市场需求、市场需求及市场结构占比分析预测
查看>>
三星集团总市值达3220亿美元,高阿里巴巴近千亿
查看>>
高通在物联网领域已经深耕多年
查看>>
广州运营开放式数据交易平台发力大数据业务
查看>>
程维谈智慧交通:我们赶上好时代 走出了自己路
查看>>
中国光伏新增装机容量猛增
查看>>
数据库建立索引的原则
查看>>
林洋能源:布局能源互联网 分布式光伏龙头再扬帆
查看>>
理解 Linux/Unix 登录脚本
查看>>
C++程序设计:原理与实践(进阶篇)15.4 链表
查看>>
《C++面向对象高效编程(第2版)》——3.16 从函数中返回引用
查看>>
《JavaScript精粹(修订版)》——1.6 使用括号和分号结束符(一致的编码方式)...
查看>>
2.4 表单数据的验证
查看>>
《Android游戏开发详解》——第2章,第2.10节使用对象
查看>>
《OpenGL ES 2.0游戏开发(上卷):基础技术和典型案例》一第6章 让场景更逼真——光照效果...
查看>>