C++走向远洋——55(项目一3、分数类的重载、>>
*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名:text.cpp
* 作者:常轩
* 微信公众号:Worldhello
* 完成日期:2016年5月25日
* 版本号:V1.0
* 问题描述:分数类的重载取倒数+输入输出重载
* 程序输入:无
* 程序输出:见运行结果
*/
#include<iostream>
#include<Cmath>
using namespace std; class CFraction{
private:
int nume; //分子
int deno; //分母
public:
CFraction(int nu=0,int de=0);
//输入输出的重载
friend istream &operator>>(istream &in,CFraction &x);
friend ostream &operator<<(ostream &out,CFraction x);
CFraction operator+(const CFraction &n); //分数相加
CFraction operator-(const CFraction &n); //分数相减
CFraction operator*(const CFraction &n); //分数相乘
CFraction operator/(const CFraction &n); //分数相除
void display(); //输出分数
void simplify(); //分数化简
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);
CFraction operator+(); //取正一目运算
CFraction operator-(); //取反一目运算
CFraction operator~(); //分数取倒数
}; CFraction::CFraction(int nu,int de) //构造函数
{
nume=nu;
deno=de;
}
void CFraction::display() //输出函数
{
cout<<nume<<"/"<<deno<<endl;
}
void CFraction::simplify() //分数化简
{
int m,n,r;
n=fabs(deno);
m=fabs(nume);
if(nume==0)
deno=0;
else{
while(r=m%n) // 求m,n的最大公约数
{
m=n;
n=r;
}
deno/=n; // 化简
nume/=n;
if (deno<0) // 将分母转化为正数
{
deno=-deno;
nume=-nume;
}
}
}
istream &operator>>(istream &in,CFraction &x)
{
char ch;
while(1)
{
cin>>x.nume>>ch>>x.deno;
if (x.deno==0)
cerr<<"分母为0, 请重新输入\n";
else if(ch!='/')
cerr<<"格式错误(形如m/n)! 请重新输入\n";
else
break;
}
return cin;
}
// 重载输出运算符<<
ostream &operator<<(ostream &out,CFraction x)
{
cout<<x.nume<<'/'<<x.deno;
return cout;
} CFraction CFraction::operator +(const CFraction &n) //定义分数相加
{
CFraction t;
t.deno=this->deno*n.deno;
t.nume=this->nume*n.deno+n.nume*this->deno;
t.simplify();//化简
return t;
}
CFraction CFraction::operator -(const CFraction &n) //定义分数相减
{
CFraction t;
t.deno=this->deno*n.deno;
t.nume=this->nume*n.deno-n.nume*this->deno;
t.simplify();//化简
return t;
}
CFraction CFraction::operator *(const CFraction &n) //定义分数相乘
{
CFraction t;
t.deno=n.deno*this->deno;
t.nume=n.nume*this->nume;
t.simplify();//化简
return t;
}
CFraction CFraction::operator /(const CFraction &n) //定义分数相除
{
CFraction t;
t.deno=n.nume*this->deno;
t.nume=n.deno*this->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) 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;
}
// 分数取正号
CFraction CFraction:: operator+()
{
return *this;
} // 分数取负号
CFraction CFraction:: operator-()
{
CFraction x;
x.nume=-this->nume;
x.deno=this->deno;
return x;
}
CFraction CFraction::operator ~()
{
CFraction t;
t.deno=this->nume;
t.nume=this->deno;
return t;
}
int main()
{
CFraction a,b;
CFraction c;
cin>>a>>b;
c=a+b;
cout<<"c=";
c.display();
cout<<" "<<c<<"重载<<后输出"<<endl;
c=a*b;
cout<<"c=";
c.display();
cout<<" "<<c<<endl;
c=a-b;
cout<<"c=";
c.display();
cout<<" "<<c<<endl;
c=a/b;
cout<<"c=";
c.display();
cout<<" "<<c<<endl;
if(a>b)
cout<<"a>b"<<endl;
c=~a;
c.display();
cout<<" "<<c<<endl;
return 0;
}
C++走向远洋——55(项目一3、分数类的重载、>>的更多相关文章
- C++走向远洋——54(项目一2、分数类的重载、取倒数)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- C++走向远洋——53(项目一1、分数类的重载、加减乘除、比较)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- C++走向远洋——(项目二、存储班长信息的学生类、派生)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- 连分数(分数类模板) uva6875
//连分数(分数类模板) uva6875 // 题意:告诉你连分数的定义.求连分数,并逆向表示出来 // 思路:直接上分数类模板.要注意ai可以小于0 #include <iostream> ...
- OC2_分数类
// // Fraction.h // OC2_分数类 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zhangxu ...
- 第十七周oj刷题——Problem B: 分数类的四则运算【C++】
Description 编写分数类Fraction,实现两个分数的加.减.乘和除四则运算.主函数已给定. Input 每行四个数,分别表示两个分数的分子和分母,以0 0 0 0 表示结束. Outpu ...
- Problem F: 分数类的类型转换
Description 封装一个分数类Fract,用来处理分数功能和运算,支持以下操作: 1. 构造:传入两个参数n和m,表示n/m:分数在构造时立即转化成最简分数. 2. show()函数:分数 ...
- Problem E: 分数类的输出
Problem E: 分数类的输出 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2699 Solved: 1227[Submit][Status][ ...
- java的分数类
概述 分数类在算法中非常重要, 而在java中不那么重要,java基础类库提供 了biginteger了,提供类似方式, package 组合数学; public class Fraction { p ...
随机推荐
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- ZEOSDBO控件的安装及使用方法
步骤:1:下载最新版的ZEOSDBO,官网:http://sourceforge.net/projects/zeoslib/ 2:解压文件到文件安装目录下:C:\Program Files\Embar ...
- Linux常见指令x-mind
- Softether使用本地网桥
https://maytalkhao.com/archives/826 以下步骤都是按照上面这篇文章来的,具体细节如下 一.使用Softether VPN Server Manager软件添加本地网 ...
- 论文翻译——Attention Is All You Need
Attention Is All You Need Abstract The dominant sequence transduction models are based on complex re ...
- 记录ionic 最小化应用时所遇的问题
ionic3与ionic4最小化插件安装不一样: ionic3安装方法: $ ionic cordova plugin add cordova-plugin-appminimize $ npm ins ...
- Docker系列八: 数据卷
什么是数据卷 生成环境中使用docker的过程中,往往需要对数据进行持久化,或者需要多个容器之间进行数据共享,这个就涉及到了容器数据管理 容器中管理数据主要有两种方式: 数据卷:容器内数据之间映射到本 ...
- 实现JS脏话筛选替换的几种途径
一.逐个替换用replace 缺点:筛选的脏话集太少 var oSize = $(this).siblings('.flex-text-wrap').find('.comment-input').va ...
- python往mysql数据库中写入数据和更新插入数据
本文链接:https://blog.csdn.net/Mr__lqy/article/details/85719603 1. 连接mysql import pymysql db = pymysql.c ...
- getHibernateTemplate()的find用法大全
一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...